Python SDK for Kovernance AI Agent Governance Platform
Project description
Kovernance SDK for Python
The official Python SDK for Kovernance - the AI Agent Governance Platform.
Track, audit, and govern your AI agents with ease. Kovernance provides observability, compliance, and risk management for AI systems.
Installation
pip install kovernance
Quick Start
from kovernance import KovernanceClient, trace, span
# Initialize the client
kov = KovernanceClient(
api_key="kov_your_api_key",
agent_id="my-ai-agent",
)
# Option 1: Log events manually
kov.log_event(
event_type="decision",
action="approve_request",
input_data={"request_id": "req-123"},
output={"approved": True, "reason": "All criteria met"},
metadata={"user_id": "user-456"},
)
# Option 2: Use the @trace decorator for automatic function tracing
@trace(kov)
def process_customer_request(request: dict) -> dict:
# Your logic here
return {"status": "processed"}
# Option 3: Use span for block-level tracing
with span(kov, "database_query") as s:
results = db.query("SELECT * FROM users")
s.set_output({"rows_returned": len(results)})
Features
Manual Event Logging
Log any event with full control over the data:
kov.log_event(
event_type="llm_call", # Type of event
action="generate_response", # Specific action
input_data={"prompt": "..."}, # Input data
output={"response": "..."}, # Output/result
metadata={"model": "gpt-4"}, # Additional metadata
session_id="session-123", # Group related events
parent_event_id="evt-parent", # Link to parent event
)
Batch Ingestion
Send multiple events efficiently:
events = [
{"event_type": "step", "action": "start", "metadata": {"step": 1}},
{"event_type": "step", "action": "process", "metadata": {"step": 2}},
{"event_type": "step", "action": "complete", "metadata": {"step": 3}},
]
kov.batch_ingest(events)
Function Tracing with @trace
Automatically trace function calls:
@trace(kov, event_type="tool_call", name="web_search")
def search_web(query: str) -> list:
# Function is automatically traced
# Captures: arguments, return value, duration, errors
return perform_search(query)
Options:
event_type: Custom event type (default: "function_call")name: Custom function name for loggingcapture_args: Whether to capture arguments (default: True)capture_result: Whether to capture return value (default: True)capture_exceptions: Whether to capture exceptions (default: True)
Block Tracing with span
Trace arbitrary code blocks:
with span(kov, "external_api_call", metadata={"api": "weather"}) as s:
response = requests.get("https://api.weather.com/...")
s.set_output({"status_code": response.status_code})
s.add_metadata("response_time_ms", response.elapsed.total_seconds() * 1000)
Async Support
Full async/await support:
from kovernance import AsyncKovernanceClient, async_span
async with AsyncKovernanceClient(api_key="...", agent_id="...") as kov:
await kov.log_event(event_type="async_op", action="start")
async with async_span(kov, "async_operation") as s:
result = await some_async_function()
s.set_output({"result": result})
Framework Integrations
CrewAI
from crewai import Agent, Crew, Task
from kovernance import KovernanceClient, trace
kov = KovernanceClient(api_key="...", agent_id="research-crew")
@trace(kov, event_type="crew_execution")
def run_research_crew(topic: str):
crew = Crew(agents=[...], tasks=[...])
return crew.kickoff()
See examples/crewai for more details.
LangGraph
from langgraph.graph import StateGraph
from kovernance import KovernanceClient, trace
kov = KovernanceClient(api_key="...", agent_id="support-agent")
@trace(kov, event_type="node_execution")
def classify_intent(state):
# Node logic
return state
See examples/langgraph for more details.
Configuration
Environment Variables
export KOVERNANCE_API_KEY="kov_your_api_key"
export KOVERNANCE_BASE_URL="https://api.kovernance.io" # Optional
Client Options
kov = KovernanceClient(
api_key="kov_xxx", # Required: API key
agent_id="my-agent", # Required: Agent identifier
base_url="https://api.kovernance.io", # Optional: API base URL
timeout=30.0, # Optional: Request timeout
organization_id="org-123", # Optional: Multi-tenant org ID
)
Error Handling
from kovernance import (
KovernanceError, # Base exception
ConfigurationError, # Invalid configuration
AuthenticationError, # Auth failures (401)
APIError, # General API errors
ValidationError, # Input validation errors
RateLimitError, # Rate limit exceeded (429)
)
try:
kov.log_event(event_type="test", action="test")
except AuthenticationError:
print("Invalid API key")
except RateLimitError as e:
print(f"Rate limited. Retry after {e.retry_after} seconds")
except APIError as e:
print(f"API error: {e.status_code} - {e.response_body}")
Development
Local Setup
# Clone the repository
git clone https://github.com/kovernance/kovernance-sdk-python.git
cd kovernance-sdk-python
# Install in development mode
pip install -e ".[dev]"
# Run tests
pytest
# Run linting
ruff check src tests
mypy src
Testing Against Local Backend
kov = KovernanceClient(
api_key="your-local-key",
agent_id="test-agent",
base_url="http://localhost:8000", # Local Kovernance backend
)
License
MIT License - see LICENSE for details.
Links
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kovernance-0.1.0.tar.gz.
File metadata
- Download URL: kovernance-0.1.0.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb8d3ace64e83ab800649a90cb3f8b3d79764cf951b90342f58e89d3e2fc2644
|
|
| MD5 |
262efc5b022a19e24b0af59d3f942946
|
|
| BLAKE2b-256 |
bb7a26deeab5d5bd20cf9b418977760f53fa1296e8baf3fd037dd642edb0acee
|
File details
Details for the file kovernance-0.1.0-py3-none-any.whl.
File metadata
- Download URL: kovernance-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
75e695728d321704981c4abfd94ecac5c948f8ef8908ef57d12c7d3fa8981fe5
|
|
| MD5 |
9a9399b4605e9a1d43ef731d6e22562f
|
|
| BLAKE2b-256 |
12b2f7dcfbbb39b99911792df45c483cd44c2cf97703d32126612d2efcfc6326
|