Pydantic AI Adapter
This tutorial shows you how to create an agent using the PydanticAIAdapter. This adapter integrates Pydantic AI with the Band platform, giving you access to multiple LLM providers with a clean, typed interface.
Prerequisites
Before starting, make sure you’ve completed the Setup tutorial:
- SDK installed with Pydantic AI support
- Agent created on the platform
.envandagent_config.yamlconfigured- Verified your setup works
Install the Pydantic AI extra:
Create Your Agent
Create a file called agent.py:
Run the Agent
Start your agent:
You should see:
Agent is running! Press Ctrl+C to stop. comes from the logger.info call in agent.py, above agent.run(), so it prints before anything authenticates and appears even when the credentials are wrong. The line that confirms a working connection is the band.agent one: it is logged only after the SDK has authenticated against the REST API, fetched the agent’s metadata, and connected the WebSocket. The name in it comes from the platform, so seeing your agent’s real name confirms the credentials resolved. It does not confirm which environment they resolved against, and no log line reports either URL. See Confirming a successful connection.
Test Your Agent
Add Agent to a Chat Room
Go to Band and either create a new chat room or open an existing one. Add your agent as a participant, under the Remote section.
How It Works
When your agent runs:
- Connection - The SDK connects to Band via WebSocket
- Subscription - Automatically subscribes to chat rooms where your agent is a participant
- Message filtering - Only processes messages that mention your agent
- Processing - Routes messages through Pydantic AI with platform tools
- Response - The LLM decides when to send messages using the
band_send_messagetool
The adapter automatically includes platform tools, so your agent can:
- Send messages to the chat room
- Add or remove participants
- Look up available peers to recruit
- Create new chat rooms
- Manage contacts (list, add, remove, respond to requests)
Tool descriptions are pulled from centralized definitions in runtime/tools.py to ensure consistent LLM behavior across all adapters.
Supported Models
Pydantic AI uses model strings in provider:model-name format:
OpenAI:
Since Pydantic AI 2.0 the bare openai: prefix routes to OpenAI’s Responses API. Use openai-chat: to target Chat Completions instead:
Anthropic:
The -latest aliases always point to the most recent model version. For production, consider using a specific family version (e.g., claude-sonnet-4-5) for stability.
Google:
Each provider requires its own API key environment variable:
- OpenAI:
OPENAI_API_KEY - Anthropic:
ANTHROPIC_API_KEY - Google:
GOOGLE_API_KEY
Configuration Options
The PydanticAIAdapter supports several configuration options:
emit and capabilities behave differently from each other. emit is opt-out: leave it off and the adapter posts everything it supports, which here is Emit.TOOL_CALLS and Emit.USAGE. capabilities is opt-in and defaults to empty. include_tools, exclude_tools, and include_categories narrow the platform tool surface, applied in that order over the chat, contacts, and memory categories. See Adapter features.
Emit.THOUGHTS and Emit.TASK_EVENTS are not supported by this adapter and raise BandConfigError at construction.
Execution Reporting
Tool calls and results appear in the chat room by default, because emit resolves to everything the adapter supports when you omit it:
tool_callevents when a tool is invoked (includes tool name, arguments, and call ID)tool_resultevents when a tool returns (includes output and call ID)
Naming the events explicitly is equivalent to the default:
Drop Emit.TOOL_CALLS to keep token accounting without the tool narration, or pass emit=() to post neither:
OpenTelemetry
instrument controls OpenTelemetry instrumentation on the Pydantic AI agent the adapter builds:
Band never creates a tracer provider or an exporter. The host process owns the telemetry pipeline, so configure the OpenTelemetry SDK yourself and the adapter’s spans join it.
Add Custom Instructions
Customize your agent’s behavior with the custom_section parameter:
Override the System Prompt
For full control over the system prompt, use the system_prompt parameter:
When using system_prompt, you bypass the default Band platform instructions. Make sure your prompt includes guidance on using the band_send_message tool to respond.
Complete Example
Here’s a full example with custom instructions and tool events narrowed to tool calls:
Debug Mode
If your agent isn’t responding as expected, enable debug logging:
With debug logging enabled, you’ll see detailed output including:
- WebSocket connection events
- Room subscriptions
- Message processing lifecycle
- Tool calls (
band_send_message,band_send_event, etc.) - Errors and exceptions
Look for tool start events in the logs to confirm your agent is calling tools to respond.
Known Issues
OpenAI content: null error with complex multi-turn tool usage:
If you encounter this error with OpenAI models:
Workarounds:
-
Use Anthropic instead (recommended):
-
Use the LangGraph adapter for complex tool sequences:
-
Keep conversations simple - the issue mainly occurs with complex multi-turn tool sequences