Skip to content

pydantic_ai.models

Logic related to making requests to an LLM.

The aim here is to make a common interface for different LLMs, so that the rest of the code can be agnostic to the specific LLM being used.

Model

Bases: ABC

Abstract class for a model.

agent_model abstractmethod

agent_model(
    retrievers: Mapping[str, AbstractToolDefinition],
    allow_text_result: bool,
    result_tools: Sequence[AbstractToolDefinition] | None,
) -> AgentModel

Create an agent model.

Parameters:

Name Type Description Default
retrievers Mapping[str, AbstractToolDefinition]

The retrievers available to the agent.

required
allow_text_result bool

Whether a plain text final response/result is permitted.

required
result_tools Sequence[AbstractToolDefinition] | None

Tool definitions for the final result tool(s), if any.

required

Returns:

Type Description
AgentModel

An agent model.

AgentModel

Bases: ABC

Model configured for a specific agent.

request abstractmethod async

request(
    messages: list[Message],
) -> tuple[ModelAnyResponse, Cost]

Make a request to the model.

request_stream async

request_stream(
    messages: list[Message],
) -> AsyncIterator[EitherStreamedResponse]

Make a request to the model and return a streaming response.

StreamTextResponse

Bases: ABC

Streamed response from an LLM when returning text.

__aiter__

__aiter__() -> AsyncIterator[None]

Stream the response as an async iterable, building up the text as it goes.

This is an async iterator that yields None to avoid doing the work of validating the input and extracting the text field when it will often be thrown away.

__anext__ abstractmethod async

__anext__() -> None

Process the next chunk of the response, see above for why this returns None.

get abstractmethod

get(*, final: bool = False) -> Iterable[str]

Returns an iterable of text since the last call to get() — e.g. the text delta.

Parameters:

Name Type Description Default
final bool

If True, this is the final call, after iteration is complete, the response should be fully validated and all text extracted.

False

cost abstractmethod

cost() -> Cost

Return the cost of the request.

NOTE: this won't return the ful cost until the stream is finished.

timestamp abstractmethod

timestamp() -> datetime

Get the timestamp of the response.

StreamStructuredResponse

Bases: ABC

Streamed response from an LLM when calling a tool.

__aiter__

__aiter__() -> AsyncIterator[None]

Stream the response as an async iterable, building up the tool call as it goes.

This is an async iterator that yields None to avoid doing the work of building the final tool call when it will often be thrown away.

__anext__ abstractmethod async

__anext__() -> None

Process the next chunk of the response, see above for why this returns None.

get abstractmethod

get(*, final: bool = False) -> ModelStructuredResponse

Get the ModelStructuredResponse at this point.

The ModelStructuredResponse may or may not be complete, depending on whether the stream is finished.

Parameters:

Name Type Description Default
final bool

If True, this is the final call, after iteration is complete, the response should be fully validated.

False

cost abstractmethod

cost() -> Cost

Get the cost of the request.

NOTE: this won't return the full cost until the stream is finished.

timestamp abstractmethod

timestamp() -> datetime

Get the timestamp of the response.

infer_model

infer_model(model: Model | KnownModelName) -> Model

Infer the model from the name.

AbstractToolDefinition

Bases: Protocol

Abstract definition of a function/tool.

This is used for both retrievers and result tools.

name instance-attribute

name: str

The name of the tool.

description instance-attribute

description: str

The description of the tool.

json_schema instance-attribute

json_schema: ObjectJsonSchema

The JSON schema for the tool's arguments.

outer_typed_dict_key instance-attribute

outer_typed_dict_key: str | None

The key in the outer [TypedDict] that wraps a result tool.

This will only be set for result tools which don't have an object JSON schema.

cached_async_http_client cached

cached_async_http_client() -> AsyncClient

Cached HTTPX async client so multiple agents and calls can share the same client.

There are good reasons why in production you should use a httpx.AsyncClient as an async context manager as described in encode/httpx#2026, but when experimenting or showing examples, it's very useful not to, this allows multiple Agents to use a single client.