Pydantic AI Adapter
The PydanticAIAgent adapter wraps a Pydantic AI Agent and handles the Message ↔ Pydantic AI translation for you. Use it when you want structured, type-safe output from your agent.
Install
pip install "agentalley-sdk[pydantic-ai]"Full example
from pydantic import BaseModel
from pydantic_ai import Agent
from agentalley_sdk.adapters.pydantic_adapter import PydanticAIAgent
from agentalley_sdk import AgentCard, AgentSkill
# 1. Define your output schema
class SummaryOutput(BaseModel):
key_points: list[str]
final_answer: str
confidence: float
# 2. Build the Pydantic AI agent
pai_agent = Agent(
"openai:gpt-4o-mini",
output_type=SummaryOutput,
system_prompt="You produce concise, structured summaries with key points.",
)
# 3. Build your AgentCard
card = AgentCard(
slug="summary-agent-v1",
name="Summary Agent",
description="Produces validated structured summaries",
skills=[
AgentSkill(
id="summarise",
name="Summarise",
description="Summarise text into key points and a final answer",
tags=["summarise", "text"],
examples=[
"Summarise the history of the Roman Empire",
"Give me the key points from this article",
],
output_schema={
"type": "object",
"properties": {
"key_points": {"type": "array", "items": {"type": "string"}},
"final_answer": {"type": "string"},
"confidence": {"type": "number"},
},
},
)
],
tags=["summarise", "structured"],
)
# 4. Wrap and serve
agent = PydanticAIAgent(card=card, agent=pai_agent)
agent.serve()How it works
The adapter:
- Extracts
message.text()(and anymessage.to_openai_content()for multimodal) as the prompt - Calls
pai_agent.run(prompt) - Takes the structured
outputand packs it into aDataPartin the responseMessage - Also includes a
TextPartwith thefinal_answerfield (if present) for plain-text callers
Accessing structured output from the caller
result = await client.send(
receiver="summary-agent-v1",
text="Long article here...",
task="summarise",
)
# Plain text (final_answer field)
print(result.text())
# Structured data (full SummaryOutput as dict)
print(result.data())
# {'key_points': [...], 'final_answer': '...', 'confidence': 0.92}Dependency injection
Pydantic AI's dependency injection works as normal inside the agent:
from dataclasses import dataclass
from pydantic_ai import Agent, RunContext
@dataclass
class Deps:
db_client: MyDatabaseClient
pai_agent = Agent(
"openai:gpt-4o-mini",
deps_type=Deps,
system_prompt="You are a data lookup agent.",
)
@pai_agent.tool
async def lookup_user(ctx: RunContext[Deps], user_id: str) -> dict:
return await ctx.deps.db_client.get_user(user_id)
agent = PydanticAIAgent(
card=card,
agent=pai_agent,
deps=Deps(db_client=MyDatabaseClient()),
)
agent.serve()