Skip to content

Support full Python objects in agent state (dataclasses, custom types, etc.) beyond JSON-serializable objects #19

@afarntrog

Description

@afarntrog

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

  1. No serialization boilerplate - Use Python objects directly without conversion code
  2. Richer type system - Leverage dataclasses, Pydantic models, enums, datetime, Decimal, etc.
  3. Complex state - Store database connections, API clients, file handles
  4. Better type safety - Full IDE support and type checking for state objects
  5. 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 works

References

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions