pydantic-ai
pydantic-ai with OpenAIChatModel. For Claude through the Messages API, see pydantic-ai (Anthropic).
Connect
python
import os
from openai import AsyncOpenAI
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai import OpenAIProvider
client = AsyncOpenAI(base_url="http://127.0.0.1:4010/v1", api_key=os.environ["PLUGBOARD_KEY"])
model = OpenAIChatModel("fast", provider=OpenAIProvider(openai_client=client))"fast" is an alias from your aliases option; any allowed provider/model[#variant] works too. OpenAIResponsesModel works the same way on the Responses API.
Chat
py
agent = Agent(OpenAIChatModel(model, provider=OpenAIProvider(openai_client=client), settings=ModelSettings(max_tokens=1024)))
result = await agent.run("Reply with exactly: pong")Tool calls
pydantic-ai runs the tool and makes the follow-up request itself.
py
agent = Agent(OpenAIChatModel(model, provider=OpenAIProvider(openai_client=client), settings=ModelSettings(max_tokens=1024)))
@agent.tool_plain
def get_weather(city: str) -> dict:
"""Get the current weather for a city."""
return {"temp_c": 18, "sky": "cloudy"}
result = await agent.run(PROMPT)Structured output
Use the default tool mode (Agent(model, output_type=Meal)) or NativeOutput(Meal). PromptedOutput sends no schema to the model, so a weak model may return numbers as strings.
py
agent = Agent(OpenAIChatModel(model, provider=OpenAIProvider(openai_client=client), settings=ModelSettings(max_tokens=1024)), output_type=Meal)
result = await agent.run(PROMPT)
meal = result.output # a Meal, or pydantic-ai raises after its retriesLimits
- No model-list call. Use the provider's client:
provider.client.models.list().