This guide will walk you through integrating PydanticAI with APIpie, enabling you to build type-safe AI applications with structured outputs.
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:
By connecting PydanticAI to APIpie, you unlock access to powerful language models while maintaining type safety and structured outputs.
pip install 'pydantic-ai-slim[openai]'
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/v1",
api_key="your-APIpie-key-here",
)
agent = Agent(model)
result = await agent.run("Why is the sky blue?")
print(result)
| 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 |
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/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)
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/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}")
agent.stream() method.For more information, see the PydanticAI documentation or the GitHub repository.
If you encounter any issues during the integration process, please reach out on APIpie Discord for assistance.
Pixeltable Integration Guide
Learn how to integrate Pixeltable with APIpie for building powerful multimodal AI applications with declarative data infrastructure.
Microsoft Semantic Kernel Integration Guide
Learn how to integrate Microsoft Semantic Kernel with APIpie for building intelligent AI agents and multi-agent systems.