Skip to main content
This cookbook provides comprehensive examples for integrating Pydantic AI with Maxim, covering simple agents, advanced agents with complex tools, and streaming capabilities.

Prerequisites

Before starting, ensure you have:
  • Python 3.10+
  • A Maxim account (sign up here)
  • Maxim API key and repository ID
  • OpenAI API key (for Pydantic AI)

Installation

Environment Setup

Create a .env file in your project root:

Simple Agent Example

This example demonstrates basic Pydantic AI integration with Maxim using a simple math agent.

Setup and Instrumentation

Create a Simple Agent

Run the Agent

Advanced Agent Example

This example demonstrates a more complex agent with multiple sophisticated tools.

Create an Advanced Agent

Run the Advanced Agent

Streaming Example

This example demonstrates how to use Pydantic AI’s streaming capabilities with Maxim integration.

Create a Streaming Agent

Run Streaming Examples

Additional Example

Complete Example with All Features

Here’s a comprehensive example that combines all the features:
pydantic-ai.gif

Best Practices

1. Environment Variables

Always use environment variables for API keys and sensitive configuration.

2. Error Handling

Wrap agent calls in try-catch blocks for robust error handling:

3. Tool Documentation

Provide clear docstrings for your tools to help the agent understand their purpose:

4. Async vs Sync

  • Use await agent.run() for async operations
  • Use agent.run() for synchronous operations
  • Use async with agent.run_stream() for streaming

Troubleshooting

Common Issues

  1. Import Errors: Ensure all packages are installed correctly
  2. API Key Issues: Verify environment variables are set correctly
  3. Tool Not Working: Check tool function signatures and docstrings
  4. No Traces: Ensure Maxim instrumentation is called before creating agents

Debug Mode

Enable debug mode for detailed logging:
This cookbook provides a comprehensive foundation for integrating Pydantic AI with Maxim. You can extend these examples with your own custom tools and use cases.
For more details, see the Maxim Python SDK documentation.

Resources

Cookbook Code

Python Notebook for Pydantic AI & Maxim AI