Skip to main content

Sessions API Reference

Session management and context provider system for conversation state and context engineering.

AgentSession

A lightweight conversation session with an agent.

Constructor

str | None
Unique identifier for this session. If None, a UUID will be generated.
str | None
Service-managed session ID (e.g., thread ID from Assistants API). If using service-side storage.

Properties

str
The unique identifier for this session.
str | None
Service-managed session ID. Can be updated after creation.
dict[str, Any]
Mutable state dictionary shared with all providers. Used to store conversation history, context, and provider-specific data.

Methods

to_dict()

Serialize session to a plain dict for storage/transfer.
dict[str, Any]
Serialized session dictionary. Values in state that implement to_dict()/from_dict() are automatically serialized.

from_dict() (classmethod)

Restore session from a previously serialized dict.
dict[str, Any]
required
Dictionary from a previous to_dict() call.
AgentSession
Restored AgentSession instance.

Example: Basic Session Usage


SessionContext

Per-invocation state passed through the context provider pipeline.

Attributes

str | None
The ID of the current session.
str | None
Service-managed session ID.
list[Message]
The new messages being sent to the agent (set by caller).
dict[str, list[Message]]
Dictionary mapping source_id -> messages added by that provider. Maintains insertion order (provider execution order).
list[str]
Additional instructions added by providers.
list[Any]
Additional tools added by providers.
AgentResponse | None
After invocation, contains the full AgentResponse. Read-only for providers.
dict[str, Any]
Options passed to agent.run() - read-only, for reflection only.
dict[str, Any]
Shared metadata dictionary for cross-provider communication.

Methods

extend_messages()

Add context messages from a specific source.
str | object
required
Either a plain source_id string, or an object with a source_id attribute (e.g., a context provider).
Sequence[Message]
required
The messages to add. Messages are copied before attribution is added.

extend_instructions()

Add instructions to be prepended to the conversation.
str
required
The provider source_id adding these instructions.
str | Sequence[str]
required
A single instruction string or sequence of strings.

extend_tools()

Add tools to be available for this invocation.
str
required
The provider source_id adding these tools.
Sequence[Any]
required
The tools to add. Tools are attributed with source metadata.

get_messages()

Get context messages, optionally filtered and including input/response.
set[str] | None
If provided, only include context messages from these sources.
set[str] | None
If provided, exclude context messages from these sources.
bool
default:"False"
If True, append input_messages after context.
bool
default:"False"
If True, append response.messages at the end.
list[Message]
Flattened list of messages in conversation order.

BaseContextProvider

Base class for context providers that participate in the context engineering pipeline.

Constructor

str
required
Unique identifier for this provider instance. Used for message/tool attribution.

Methods

before_run()

Called before model invocation.
SupportsAgentRun
required
The agent running this invocation.
AgentSession
required
The current session.
SessionContext
required
The invocation context - add messages/instructions/tools here.
dict[str, Any]
required
The provider-scoped mutable state dict. This is a reference to session.state[provider.source_id].

after_run()

Called after model invocation.
SupportsAgentRun
required
The agent that ran this invocation.
AgentSession
required
The current session.
SessionContext
required
The invocation context with response populated.
dict[str, Any]
required
The provider-scoped mutable state dict.

Example: RAG Context Provider


BaseHistoryProvider

Base class for conversation history storage providers.

Constructor

str
required
Unique identifier for this provider instance.
bool
default:"True"
Whether to load messages before invocation. When False, the agent skips calling before_run entirely.
bool
default:"True"
Whether to store input messages.
bool
default:"False"
Whether to store context from other providers.
set[str] | None
If set, only store context from these source_ids.
bool
default:"True"
Whether to store response messages.

Abstract Methods

get_messages()

Retrieve stored messages for this session.
str | None
required
The session ID to retrieve messages for.
Any
Additional arguments (e.g., state for in-memory providers).
list[Message]
List of stored messages.

save_messages()

Persist messages for this session.
str | None
required
The session ID to store messages for.
Sequence[Message]
required
The messages to persist.
Any
Additional arguments.

Example: Database History Provider


InMemoryHistoryProvider

Built-in history provider that stores messages in session.state.

Constructor

str | None
default:"in_memory"
Unique identifier for this provider instance.
bool
default:"True"
Whether to load messages before invocation.
bool
default:"True"
Whether to store input messages.
bool
default:"False"
Whether to store context from other providers.
set[str] | None
If set, only store context from these source_ids.
bool
default:"True"
Whether to store response messages.

Notes

  • Messages are stored in state["messages"] as a list of Message objects
  • Serialization to/from dicts is handled by AgentSession.to_dict()/from_dict()
  • This provider holds no instance state - all data lives in the session’s state dict
  • Auto-added by the agent for local sessions when no providers are configured and service-side storage is not requested

Example


register_state_type()

Register a type for automatic deserialization in session state.

Usage

type
required
The type to register. Types with to_dict/from_dict methods or Pydantic BaseModel subclasses are handled automatically.

Notes

  • Pydantic models are auto-registered on first serialization
  • Pre-registering ensures deserialization works even if the model hasn’t been serialized in this process yet
  • The type identifier defaults to cls.__name__.lower() but can be overridden by defining a _get_type_identifier classmethod