Add Metorial tools to a Pydantic AI agent in Python
Connect with metorial.connect(adapter=metorial_pydantic_ai(), providers=[...]) and pass session.tools() to a Pydantic AI Agent. Python only.
This guide takes you from an empty folder to a Pydantic AI agent that searches the web through Metorial. Connect with metorial.connect(adapter=metorial_pydantic_ai(), providers=[...]) and pass session.tools() to Agent.
What you'll learn
- How to create an API key and a provider deployment from the CLI
- How to pass Metorial tools to a Pydantic AI
Agent - How to see the tool calls the agent made
Before you start
- Install the CLI and sign in
- An Anthropic API key
- Python 3.10 or later
Set up Metorial with the CLI
Find your instance
An instance is one environment of a project. List yours and note the ID of the one the agent should use:
Create an API key
Copy the secret from the output. It starts with metorial_sk_ and is shown only once.
Deploy a provider
Metorial Search is a built-in web search provider that needs no credentials.
Copy the deployment ID from the output. It starts with pde_.
Export the values
Build the agent
Create the project
Write the agent
Save this as agent.py. The adapter ships with the metorial package and turns every tool in the session into a Pydantic AI Tool. To add your own tools, append them to the list.
Run it
Note
Call the agent with await agent.run(...). Pydantic AI's run_sync cannot be used inside async code, and metorial.connect is async.
Check it worked
The first line lists the tools, then the agent prints its summary. Metorial prefixes each tool with the provider name and adds metorial_connection_status, a diagnostic tool, to every session.
List the tool calls Metorial recorded for the deployment:
Use other apps and each user's own account
Find a provider and deploy it the same way, then add its deployment ID to providers. One session can hold several.
Apps such as Slack need a sign-in. Pass the user's provider auth config as provider_auth_config_id next to provider_deployment_id. Let each user connect their own account shows how to get one for each user.
Troubleshooting
ImportError: pydantic-ai is required for PydanticAI integration. Install it with: pip install pydantic-ai
The adapter could not import pydantic_ai. Install pydantic-ai in the same virtual environment as metorial.
result.data raises AttributeError
Older examples read the answer from result.data. Current Pydantic AI versions return it as result.output.
Invalid API key status=401
The SDK raises this AuthenticationError when the key is wrong. Check that METORIAL_API_KEY holds the full secret and belongs to the instance where you created the deployment.
ValueError: api_key is required
METORIAL_API_KEY is empty in the shell that runs the script.
Provider requires an auth config to be provided
A provider deployment in the session needs a sign-in and got no provider auth config. Add the user's provider_auth_config_id to that entry.
Next steps
- Let each user connect their own account: per-user OAuth for Slack, Gmail, GitHub and other apps
- Limit the tools an agent can use: serve only some of a provider's tools
- Python SDK: install the SDK and make your first request
- The official example:
metorial-admin example create metorial-pydanticai