PydanticAI Integration

PydanticAI provides a high-level interface for building Python applications with LLMs.

Installation

pip install 'pydantic-ai-slim[openai]'

Basic Usage

from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel

model = OpenAIModel(
    "anthropic/claude-opus-4.5",
    base_url="https://api.langmart.ai/v1",
    api_key="your-langmart-api-key",
)

agent = Agent(model)
result = await agent.run("What is the meaning of life?")
print(result.data)

Structured Output

from pydantic import BaseModel
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel

class Recipe(BaseModel):
    name: str
    ingredients: list[str]
    instructions: list[str]
    prep_time_minutes: int

model = OpenAIModel(
    "openai/gpt-5.2",
    base_url="https://api.langmart.ai/v1",
    api_key="your-langmart-api-key",
)

agent = Agent(model, result_type=Recipe)

result = await agent.run("Give me a simple pasta recipe")
recipe: Recipe = result.data

print(f"Recipe: {recipe.name}")
print(f"Prep time: {recipe.prep_time_minutes} minutes")

With Tools

from pydantic_ai import Agent, RunContext

model = OpenAIModel(
    "openai/gpt-5.2",
    base_url="https://api.langmart.ai/v1",
    api_key="your-langmart-api-key",
)

agent = Agent(model)

@agent.tool
def get_weather(ctx: RunContext, city: str) -> str:
    """Get the current weather for a city."""
    return f"The weather in {city} is sunny and 72°F"

result = await agent.run("What's the weather like in Tokyo?")
print(result.data)

System Prompts

agent = Agent(
    model,
    system_prompt="You are a helpful cooking assistant.",
)

result = await agent.run("How do I make scrambled eggs?")
print(result.data)

Learn More