ChatContext
Mutable container for a conversation’s messages and agent handoffs. Holds the ordered list ofChatMessage items that make up a conversation
along with any AgentHandoff records, and provides helpers for adding
messages, estimating size, truncating history, and converting to and from
transport message formats.
Constructor
Messages to seed the context with. Copied into a new list; defaults to empty.
Handoff records to seed the context with. Copied into a new list; defaults to empty.
add_attributed_message
The role of the message author.
The message text. Non-string values are coerced to
str.Identifier of the agent that produced the message.
Identifier for the message. A new UUID is generated when not provided.
Optional image attachments for the message.
The newly created and appended
ChatMessage.add_handoff
Identifier of the agent receiving control.
Identifier of the agent relinquishing control.
Optional explanation for the handoff.
The newly created and appended
AgentHandoff with a generated id.add_message
The role of the message author.
The message text. Non-string values are coerced to
str.Identifier for the message. A new UUID is generated when not provided.
Optional image attachments for the message.
Optional creation timestamp (currently unused).
Reserved flag (currently unused).
The newly created and appended
ChatMessage.copy
A new
ChatContext sharing the current messages and handoffs.empty
A new
ChatContext with no messages or handoffs.estimated_tokens
The estimated total token count.
from_context_messages
ChatMessage entries, including any images and tool
calls. Unknown or invalid roles fall back to ChatRole.USER.
An iterable of message mappings or objects, or
None.A new
ChatContext populated with the reconstructed messages.handoffs
A new list containing the current
AgentHandoff records.last_handoff
The last
AgentHandoff recorded, or None when there are none.messages
A new list containing the current
ChatMessage objects.to_context_messages
message_id, images, tool_calls, and tool_call_id when
present.
A list of dictionaries representing the messages in the SDK’s transport format.
to_openai_messages
Reserved flag for reasoning-model formatting (currently unused).
A list of
{'role', 'content'} dictionaries, one per message.truncate
max_items is given, only the most recent messages are kept.
When max_tokens is given, the oldest messages are dropped (always
retaining at least one) until the estimated token total fits, using the
same two-tokens-per-word heuristic as estimated_tokens.
Maximum number of messages to retain.
Maximum estimated token budget to retain.
A new
ChatContext containing the retained messages.turn_count
The number of messages whose role is
ChatRole.USER.ChatMessage
A single message within a conversation.Fields
Role of the message author.
The message text.
Unique identifier for the message.
Tool invocations requested by this message.
Identifier of the tool call this message responds to.
Image attachments associated with the message.
Identifier of the agent that produced the message, if any.
ChatContent
Textual content part of a chat message.Fields
Content type discriminator;
'text' for this part.The text payload.
ChatRole
Roles a message can take in a chat conversation.FunctionCall
A tool invocation requested by the model.Fields
Name of the tool to invoke.
Arguments for the call, encoded as a JSON string.
Identifier correlating this call with its output.
FunctionCallOutput
The result of executing a tool call.Fields
Name of the tool that was invoked.
The tool’s output, encoded as a string.
Identifier correlating this output with its originating call.
Whether the output represents an error.
ImageContent
Image content part of a chat message.Fields
Content type discriminator;
'image' for this part.The image location, either an
http(s) URL or a data: URL embedding the image bytes.Requested level of detail for the image (e.g.
'auto', 'low', 'high').from_base64
The base64-encoded image data.
MIME type of the image (e.g.
'image/jpeg').Requested level of detail for the image.
An
ImageContent with a data: URL built from the inputs.from_bytes
The raw image bytes.
MIME type of the image (e.g.
'image/jpeg').Requested level of detail for the image.
An
ImageContent with a base64 data: URL built from the bytes.from_data_url
data: URL.
A
data: URL embedding the image bytes.Requested level of detail for the image.
An
ImageContent referencing the given data URL.from_file
Filesystem path to the image file.
MIME type of the image. When omitted, it is inferred from the file extension, defaulting to
'image/jpeg'.Requested level of detail for the image.
An
ImageContent with a base64 data: URL of the file contents.from_frame
data and an optional MIME type, or an
arbitrary frame object that is coerced to JPEG bytes.
The source frame. May be a mapping with a
data key (and optional mime_type/mimeType) or any object accepted by coerce_image_to_jpeg_bytes.Requested level of detail for the image.
An
ImageContent with a base64 data: URL of the frame image.from_url
http(s) URL.
The image URL.
Requested level of detail for the image.
An
ImageContent referencing the given URL.ContextWindow
Configuration governing how conversation context is bounded and truncated.Fields
Maximum number of tokens to retain in the context, or
None for no token-based limit.Maximum number of context items to retain, or
None for no item-based limit.Number of most recent turns to always preserve.
Maximum number of tool calls allowed within a single turn.
Optional language model used to summarize older context when it is trimmed.
EncodeOptions
Options controlling how an image frame is encoded.Fields
Output image format, either
'JPEG' or 'PNG'.Dimensions to resize the image to before encoding. When
None, the image is encoded at its original size; defaults to the legacy default resolution.JPEG quality (0-100); ignored for PNG output.
ResizeOptions
Target dimensions for resizing an image.Fields
Target width in pixels.
Target height in pixels.
encode
Source image. Accepted types include raw
bytes-like data, a PIL.Image.Image, a numpy array, or an object exposing a to_image method (such as an av.VideoFrame).Encoding options controlling format, resize dimensions, and JPEG quality. Defaults to
EncodeOptions() when not provided.The encoded image as bytes.
coerce_image_to_jpeg_bytes
Source image. Accepted types include raw
bytes-like data, a PIL.Image.Image, a numpy array, or an object exposing a to_image method (such as an av.VideoFrame).Encoding options used when the frame must be encoded. Defaults to
DEFAULT_REALTIME_ENCODE_OPTIONS when not provided.The image as bytes, either passed through unchanged or freshly encoded.