Skip to content

pydantic_ai.models.openai

OpenAIModel dataclass

Bases: Model

A model that uses the OpenAI API.

Internally, this uses the OpenAI Python client to interact with the API.

Apart from __init__, all methods are private or match those of the base class.

__init__

__init__(
    model_name: ChatModel,
    *,
    api_key: str | None = None,
    openai_client: AsyncOpenAI | None = None,
    http_client: AsyncClient | None = None
)

Initialize an OpenAI model.

Parameters:

Name Type Description Default
model_name ChatModel

The name of the OpenAI model to use. List of model names available here (Unfortunately, despite being ask to do so, OpenAI do not provide .inv files for their API).

required
api_key str | None

The API key to use for authentication, if not provided, the OPENAI_API_KEY environment variable will be used if available.

None
openai_client AsyncOpenAI | None

An existing AsyncOpenAI client to use, if provided, api_key and http_client must be None.

None
http_client AsyncClient | None

An existing httpx.AsyncClient to use for making HTTP requests.

None

OpenAIAgentModel dataclass

Bases: AgentModel

Implementation of AgentModel for OpenAI models.

OpenAIStreamTextResponse dataclass

Bases: StreamTextResponse

Implementation of StreamTextResponse for OpenAI models.

OpenAIStreamStructuredResponse dataclass

Bases: StreamStructuredResponse

Implementation of StreamStructuredResponse for OpenAI models.