PydanticAI Integration Guide

This guide will walk you through integrating PydanticAI with APIpie, enabling you to build type-safe AI applications with structured outputs.
What is PydanticAI?
PydanticAI is a Python agent framework designed to make it easier to build production grade applications with Generative AI. Created by the team behind Pydantic, it brings the same level of type safety and validation to AI applications that Pydantic brings to Python data validation.
Key features include:
- Type-safe interfaces for working with LLMs
- Structured validation of model outputs
- Support for tools and agents
- Dependency injection for modular design
- Built-in support for popular LLM providers
By connecting PydanticAI to APIpie, you unlock access to powerful language models while maintaining type safety and structured outputs.
Integration Steps
1. Create an APIpie Account
- Register here: APIpie Registration
- Complete the sign-up process.
2. Add Credit
- Add Credit: APIpie Subscription
- Add credits to your account to enable API access.
3. Generate an API Key
- API Key Management: APIpie API Keys
- Create a new API key for use with PydanticAI.
4. Install PydanticAI
pip install 'pydantic-ai-slim[openai]'
5. Configure PydanticAI for APIpie
You can use PydanticAI with APIpie through its OpenAI-compatible interface:
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
model = OpenAIModel(
"openai/gpt-4o-mini", # or any other APIpie model
base_url="https://apipie.ai/api/v1",
api_key="your-APIpie-key-here",
)
agent = Agent(model)
result = await agent.run("Why is the sky blue?")
print(result)
Key Features
- Type Safety: Build AI applications that are statically type-checked
- Structured Outputs: Guarantee that LLM responses follow your defined schemas
- Tool Framework: Create tools that LLMs can use, with automatic parameter validation
- Dependency Injection: Easily provide context and services to your agents
- Python-First Design: Use familiar Python patterns and practices with AI
Example Workflows
Application Type | What PydanticAI Helps You Build |
---|---|
Type-Safe Assistants | Assistants that return validated, structured data |
Function-Calling Agents | Agents that can invoke functions and validate their parameters |
Business Logic Integration | AI systems that safely integrate with your existing systems |
Multi-Agent Systems | Complex workflows with multiple specialized agents |
Data Processing Pipelines | Pipelines that extract, transform, and validate data with LLMs |
Using PydanticAI with APIpie
Basic Agent
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
# Configure the model
model = OpenAIModel(
"openai/gpt-4o-mini",
base_url="https://apipie.ai/api/v1",
api_key="your-APIpie-key-here",
)
# Create a simple agent
agent = Agent(
model,
system_prompt="You are a helpful assistant. Be concise and clear."
)
# Run the agent synchronously
result = agent.run_sync("What are three interesting facts about the moon?")
print(result.output)
Using Structured Output
from pydantic import BaseModel, Field
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
# Define a structured output model
class MoonFacts(BaseModel):
fact1: str = Field(description="First interesting fact about the moon")
fact2: str = Field(description="Second interesting fact about the moon")
fact3: str = Field(description="Third interesting fact about the moon")
# Configure the model
model = OpenAIModel(
"openai/gpt-4o-mini",
base_url="https://apipie.ai/api/v1",
api_key="your-APIpie-key-here",
)
# Create an agent with structured output
facts_agent = Agent(
model,
output_type=MoonFacts,
system_prompt="You are a space expert."
)
# Run the agent
result = facts_agent.run_sync("Tell me about the moon")
# Access the structured data
print(f"Fact 1: {result.output.fact1}")
print(f"Fact 2: {result.output.fact2}")
print(f"Fact 3: {result.output.fact3}")
Troubleshooting & FAQ
-
Which models are supported?
Any model available via APIpie's OpenAI-compatible endpoint. -
How do I persist my API key and endpoint?
Use environment variables or a .env file to store your credentials securely. -
Can I use streaming with PydanticAI?
Yes, through theagent.stream()
method.
For more information, see the PydanticAI documentation or the GitHub repository.
Support
If you encounter any issues during the integration process, please reach out on APIpie Discord for assistance.