forked from strands-agents/sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Currently, Strands agents require agent.state to be JSON-serializable, limiting it to basic Python types (str, int, dict, list, etc.). This prevents using:
- Python
dataclasses - Pydantic models
- Custom objects with complex behavior
- Rich types like
datetime,Decimal,UUID - Objects with non-serializable data (connections, file handles)
Motivation
Pydantic AI supports arbitrary Python objects in state, enabling cleaner, more Pythonic code:
from dataclasses import dataclass
from datetime import datetime
@dataclass
class AgentState:
db_connection: DatabaseConnection
created_at: datetime
user_id: UUID
cache: dict[str, Any]
agent = Agent('openai:gpt-4', deps_type=AgentState)Benefits
- No serialization boilerplate - Use Python objects directly without conversion code
- Richer type system - Leverage dataclasses, Pydantic models, enums, datetime, Decimal, etc.
- Complex state - Store database connections, API clients, file handles
- Better type safety - Full IDE support and type checking for state objects
- Cleaner code - Business logic without serialization/deserialization
Example Use Cases
@dataclass
class AgentState:
db: DatabaseConnection # Complex object
api_client: APIClient # Stateful client
created_at: datetime # Rich type
user_id: UUID # Rich type
config: dict # Still worksReferences
- Pydantic AI dependencies: https://ai.pydantic.dev/dependencies/
- Related issue: [BUG] Strands agent calls tool with parameter as dictionary instead of pydantic object strands-agents/sdk-python#1090 (tool parameters as dicts vs Pydantic objects)
Copilot