AICredits logo
Integrations

PydanticAI

Use AICredits with PydanticAI for type-safe AI agents with validated outputs. Connect via the OpenAI-compatible provider.

Use this page with an AI assistant

Opens a new chat with this docs URL and the correct AICredits base URLs.

PydanticAI is a type-safe Python framework for building AI agents with Pydantic model validation. Connect it to AICredits via the OpenAI-compatible provider for access to all models.

Overview

PydanticAI lets you define agents with typed input/output contracts and automatic validation. Because it supports OpenAI-compatible endpoints, AICredits works as a drop-in provider.

Setup

pip install pydantic-ai

Basic Agent

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

model = OpenAIModel(
    "openai/gpt-4o-mini",
    base_url="https://api.aicredits.in/v1",
    api_key="sk-your-key-here",
)

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

result = agent.run_sync("What is the population of Mumbai?")
print(result.data)

Structured Output

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

class CityInfo(BaseModel):
    name: str
    country: str
    population_millions: float
    known_for: list[str]

model = OpenAIModel(
    "openai/gpt-4o-mini",
    base_url="https://api.aicredits.in/v1",
    api_key="sk-your-key-here",
)

agent = Agent(model, result_type=CityInfo)

result = agent.run_sync("Tell me about Bengaluru.")
city = result.data

print(f"{city.name}, {city.country}")
print(f"Population: {city.population_millions}M")
print(f"Known for: {', '.join(city.known_for)}")

Agent Tools

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

model = OpenAIModel(
    "anthropic/claude-sonnet-4.5",
    base_url="https://api.aicredits.in/v1",
    api_key="sk-your-key-here",
)

agent = Agent(model, system_prompt="You are a helpful assistant with web access.")

@agent.tool_plain
def get_exchange_rate(from_currency: str, to_currency: str) -> float:
    """Get the current exchange rate between two currencies."""
    rates = {"USD": 1.0, "INR": 91.0, "EUR": 0.92}
    return rates.get(to_currency, 1.0) / rates.get(from_currency, 1.0)

result = agent.run_sync("How many INR is 100 USD worth?")
print(result.data)

PydanticAI also supports async agents via agent.run() for use in FastAPI, Django async views, and other async frameworks.

On this page