Skip to main content

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:

  1. Single Source of Truth: Models are defined once and used everywhere
  2. Type Safety: Ensures server and SDK use identical data structures
  3. Versioning: Models can be versioned independently
  4. Easier Maintenance: Changes propagate automatically to both server and SDK
  5. 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 dictionary
  • to_json(): Convert to JSON string
  • from_dict(data): Create from dictionary
  • from_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 status
  • version (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 safe
  • confidence (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__(): Enables if 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

  1. Create a new file in src/agent_control_models/
  2. Define models extending BaseModel
  3. Export in __init__.py
  4. 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

  1. Always extend BaseModel: Get free JSON/dict conversion
  2. Use Field for validation: Add constraints and descriptions
  3. Keep models simple: No business logic, just data
  4. Version carefully: Model changes affect both server and SDK
  5. Document fields: Use Field's description parameter
  6. Use Optional appropriately: Mark optional fields clearly

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agent_control_models-1.1.4.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agent_control_models-1.1.4-py3-none-any.whl (25.6 kB view details)

Uploaded Python 3

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

Hashes for agent_control_models-1.1.4.tar.gz
Algorithm Hash digest
SHA256 6466bac0fa5c8fa4fa8a19522384b5fb20b69f6e063444fbc457362b83583c08
MD5 7712d1141d1b2bd1a61a28894b9accd4
BLAKE2b-256 dc2c4fbcd9911bf864994daf649c8b37b1ceae288011817e148af8a9a76c836e

See more details on using hashes here.

File details

Details for the file agent_control_models-1.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for agent_control_models-1.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e7ac11d79efab4c78405b95ab03ab11ddc5fb51e91018e1bc39b89eff3ceed81
MD5 72f206ed35727699537bc8ca82deb974
BLAKE2b-256 9afdb60b0e6b7f7b677db7377886606c6e0ba7a41645c569924c4a14f99fe4e7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page