Shared data models for Agent Control server and SDK
Project description
Agent Protect Models
Shared data models for Agent Protect server and SDK. This package contains all the Pydantic models used for API requests, responses, and data validation.
Why Shared Models?
Having a separate models package provides several benefits:
- Single Source of Truth: Models are defined once and used everywhere
- Type Safety: Ensures server and SDK use identical data structures
- Versioning: Models can be versioned independently
- Easier Maintenance: Changes propagate automatically to both server and SDK
- Clear Contract: API contract is explicitly defined
Common Patterns in Popular Python Packages
This design follows patterns used by popular packages:
1. Shared Models (Our Approach)
- Google APIs (
google-api-core): Separate proto/model definitions - Stripe (
stripe-python): Models package shared across components - PySpark: Shared types and schemas
2. JSON/Pydantic Hybrid
- FastAPI: Pydantic models with JSON serialization
- Anthropic SDK: Pydantic models with
.to_dict()and.from_dict() - OpenAI SDK: Typed models with JSON compatibility
Installation
This package is typically installed as a dependency:
# Server depends on it
cd server
uv add agent-protect-models
# SDK depends on it
cd sdk
uv add agent-protect-models
Usage
Basic Usage
from agent_control_models import ProtectionRequest, ProtectionResponse
# Create a request
request = ProtectionRequest(
content="Hello, world!",
context={"source": "user_input"}
)
# Serialize to JSON
json_str = request.to_json()
print(json_str)
# {"content": "Hello, world!", "context": {"source": "user_input"}}
# Deserialize from JSON
request_copy = ProtectionRequest.from_json(json_str)
Dictionary Conversion
from agent_control_models import ProtectionResponse
# Create from dict
response = ProtectionResponse.from_dict({
"is_safe": True,
"confidence": 0.95,
"reason": "Content appears safe"
})
# Convert to dict
data = response.to_dict()
print(data)
# {'is_safe': True, 'confidence': 0.95, 'reason': 'Content appears safe'}
Client-Side Result
from agent_control_models import ProtectionResult
result = ProtectionResult(
is_safe=True,
confidence=0.92,
reason="All checks passed"
)
# Boolean evaluation
if result:
print("Content is safe!")
# Confidence check
if result.is_confident(threshold=0.9):
print("High confidence result")
# String representation
print(result)
# [SAFE] Confidence: 92% - All checks passed
Models
BaseModel
Base class for all models with common utilities:
to_dict(): Convert to Python dictionaryto_json(): Convert to JSON stringfrom_dict(data): Create from dictionaryfrom_json(json_str): Create from JSON string
Configuration:
- Accepts both snake_case and camelCase fields
- Ignores extra fields (forward compatibility)
- Validates on assignment
- Excludes None values in serialization
HealthResponse
Health check response.
Fields:
status(str): Health statusversion(str): Application version
ProtectionRequest
Request for content protection analysis.
Fields:
content(str): Content to analyze (required, min length 1)context(Optional[Dict[str, str]]): Optional context information
ProtectionResponse
Server response from protection analysis.
Fields:
is_safe(bool): Whether content is safeconfidence(float): Confidence score (0.0 to 1.0)reason(Optional[str]): Explanation for the decision
ProtectionResult
Client-side result extending ProtectionResponse.
Additional Methods:
is_confident(threshold=0.8): Check if confidence exceeds threshold__bool__(): Enablesif result:checks__str__(): Human-readable representation
Design Patterns
1. Pydantic + JSON
All models support both Pydantic validation and JSON serialization:
# Pydantic validation
request = ProtectionRequest(content="test") # Validates automatically
# JSON round-trip
json_str = request.to_json()
request_copy = ProtectionRequest.from_json(json_str)
assert request == request_copy
2. Inheritance for Specialization
ProtectionResult extends ProtectionResponse with client-side conveniences:
# Server uses ProtectionResponse
response = ProtectionResponse(is_safe=True, confidence=0.95)
# SDK transforms to ProtectionResult
result = ProtectionResult(**response.to_dict())
# Now has extra methods
if result.is_confident():
print("Confident result!")
3. Forward Compatibility
Models ignore extra fields, allowing server updates without breaking clients:
# Server adds new field in v2
response_data = {
"is_safe": True,
"confidence": 0.95,
"new_field": "future_feature" # Will be ignored by v1 clients
}
# V1 client still works
result = ProtectionResponse.from_dict(response_data)
# No error, new_field is simply ignored
Development
Adding New Models
- Create a new file in
src/agent_control_models/ - Define models extending
BaseModel - Export in
__init__.py - Update both server and SDK to use the new models
Example:
# src/agent_control_models/auth.py
from .base import BaseModel
class AuthRequest(BaseModel):
api_key: str
# src/agent_control_models/__init__.py
from .auth import AuthRequest
__all__ = [..., "AuthRequest"]
Testing
cd models
uv run pytest
Best Practices
- Always extend BaseModel: Get free JSON/dict conversion
- Use Field for validation: Add constraints and descriptions
- Keep models simple: No business logic, just data
- Version carefully: Model changes affect both server and SDK
- Document fields: Use Field's
descriptionparameter - Use Optional appropriately: Mark optional fields clearly
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 agent_control_models-1.1.4.tar.gz.
File metadata
- Download URL: agent_control_models-1.1.4.tar.gz
- Upload date:
- Size: 21.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6466bac0fa5c8fa4fa8a19522384b5fb20b69f6e063444fbc457362b83583c08
|
|
| MD5 |
7712d1141d1b2bd1a61a28894b9accd4
|
|
| BLAKE2b-256 |
dc2c4fbcd9911bf864994daf649c8b37b1ceae288011817e148af8a9a76c836e
|
File details
Details for the file agent_control_models-1.1.4-py3-none-any.whl.
File metadata
- Download URL: agent_control_models-1.1.4-py3-none-any.whl
- Upload date:
- Size: 25.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e7ac11d79efab4c78405b95ab03ab11ddc5fb51e91018e1bc39b89eff3ceed81
|
|
| MD5 |
72f206ed35727699537bc8ca82deb974
|
|
| BLAKE2b-256 |
9afdb60b0e6b7f7b677db7377886606c6e0ba7a41645c569924c4a14f99fe4e7
|