Skip to main content

Chat Client API Reference

Chat client classes for interacting with language models.

SupportsChatGetResponse

Protocol defining the interface that all chat clients must implement.

Protocol Properties

dict[str, Any]
required
Dictionary for storing additional client-specific properties.

get_response()

Send input and return the response.
Sequence[Message]
required
The sequence of input messages to send.
bool
default:"False"
Whether to stream the response.
ChatOptions | None
Chat options as a TypedDict including model_id, temperature, max_tokens, etc.
Any
Additional chat options.
Awaitable[ChatResponse] | ResponseStream[ChatResponseUpdate, ChatResponse]
When stream=False: An awaitable ChatResponse from the client.When stream=True: A ResponseStream yielding partial updates.

Example: Custom Chat Client


BaseChatClient

Abstract base class for chat clients without middleware wrapping.

Constructor

dict[str, Any] | None
Additional properties for the client.
Any
Additional keyword arguments merged into additional_properties.

Class Variables

str
default:"unknown"
Provider name for OpenTelemetry tracing. Override in subclasses.
bool
default:"False"
Whether this client stores conversation history server-side by default.Clients that use server-side storage (e.g., OpenAI Responses API) should override to True.

Abstract Methods

_inner_get_response()

Internal method to be implemented by derived classes.
Sequence[Message]
required
The prepared chat messages to send.
bool
required
Whether to stream the response.
Mapping[str, Any]
required
The options dict for the request. Call _validate_options() first.
Any
Additional keyword arguments.
Awaitable[ChatResponse] | ResponseStream[ChatResponseUpdate, ChatResponse]
When stream=False: An Awaitable ChatResponse from the model.When stream=True: A ResponseStream of ChatResponseUpdate instances.

Helper Methods

_validate_options()

Validate and normalize chat options.
Mapping[str, Any]
required
The raw options dict.
dict[str, Any]
The validated and normalized options dict.

_build_response_stream()

Create a ResponseStream with the standard finalizer.
AsyncIterable[ChatResponseUpdate] | Awaitable[AsyncIterable[ChatResponseUpdate]]
required
The async iterable or awaitable async iterable.
type[BaseModel] | None
Optional response format for structured output.
ResponseStream[ChatResponseUpdate, ChatResponse]
A ResponseStream with automatic finalization.

Public Methods

get_response()

Get a response from the chat client.
Sequence[Message]
required
The messages to send to the model.
bool
default:"False"
Whether to stream the response.
ChatOptions | None
Chat options as a TypedDict.
Any
Additional keyword arguments for function-specific parameters.
Awaitable[ChatResponse] | ResponseStream[ChatResponseUpdate, ChatResponse]
When streaming: a response stream of ChatResponseUpdates.Otherwise: an Awaitable ChatResponse.

as_agent()

Create an Agent with this client.
str | None
The unique identifier for the agent.
str | None
The name of the agent.
str | None
A brief description of the agent’s purpose.
str | None
Instructions sent to the chat client as a system message.
ToolTypes | Callable | Sequence[ToolTypes | Callable] | None
The tools to use for the agent.
ChatOptions | None
Default chat options. Enables IDE autocomplete for provider-specific options.
Sequence[BaseContextProvider] | None
Context providers to include during agent invocation.
Sequence[MiddlewareTypes] | None
List of middleware to intercept agent and function invocations.
Any
Additional keyword arguments stored as additional_properties.
Agent
An Agent instance configured with this chat client.

Example: Custom Chat Client Implementation


Tool Support Protocols

Protocols for runtime checking of tool support capabilities.

SupportsCodeInterpreterTool

SupportsWebSearchTool

SupportsImageGenerationTool

SupportsMCPTool

SupportsFileSearchTool


Embedding Clients

SupportsGetEmbeddings

Protocol for embedding generation.

BaseEmbeddingClient

Abstract base class for embedding clients.