Pydantic AI
Pydantic AI integration
The MultiRoute SDK provides a specialized Agent class for Pydantic AI, using the smart LLM/AI router for reliable agentic workflows with automatic high-availability routing.
Installation
Ensure you have the required dependencies:
pip install multiroute["pydantic-ai"]
Basic Usage
The multiroute.pydantic_ai.Agent is a drop-in replacement for the standard Pydantic AI Agent.
import asyncio
from multiroute.pydantic_ai import Agent
async def main():
# Uses Multiroute high-availability routing with OpenAI gpt-4o
agent = Agent("openai:gpt-4o")
result = await agent.run("What is the capital of France?")
print(result.output)
asyncio.run(main())
Advanced Features
Tool Calls
Register tools directly with the agent using @agent.tool or @agent.tool_plain.
agent = Agent("openai:gpt-4o")
@agent.tool_plain
def get_weather(city: str) -> str:
"""Return the current weather for a city."""
return f"The weather in {city} is sunny and 22°C."
result = await agent.run("What is the weather like in Tokyo?")
Streaming
Stream text output incrementally as it is generated by the model.
async with agent.run_stream("Write a short poem.") as stream:
async for chunk in stream.stream_text(delta=True):
print(chunk, end="", flush=True)
Observing Agent steps
Use agent.iter() to observe every internal step, including model requests and tool execution events.
async with agent.iter("Calculate 123 * 456") as agent_run:
async for node in agent_run:
print(f"Current step: {type(node).__name__}")
The MultirouteOpenAIProvider handles routing and failover transparently: it uses the MultiRoute smart LLM/AI router first, then falls back to direct provider access on errors. Agent state, message history, and tool definitions are preserved across failover.