OhMyGPT
SDK Integrations

PydanticAI

Use OhMyGPT with PydanticAI.

PydanticAI is a Python agent framework designed to make it easy to build production-grade applications with generative AI. It supports OpenAI-compatible APIs.

Installation

pip install pydantic-ai

Configuration

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

model = OpenAIModel(
    'gpt-4o',
    base_url='https://api.ohmygpt.com/v1',
    api_key='<OHMYGPT_API_KEY>',
)

agent = Agent(model)

Usage

Basic usage

result = agent.run_sync('What is the capital of France?')
print(result.data)

With structured output

from pydantic import BaseModel

class CityInfo(BaseModel):
    name: str
    country: str
    population: int

agent = Agent(model, result_type=CityInfo)
result = agent.run_sync('Tell me about Tokyo')
print(result.data.name)  # Tokyo
print(result.data.population)  # 13960000

With tools

from pydantic_ai import Agent, RunContext

agent = Agent(model)

@agent.tool
def get_weather(ctx: RunContext, location: str) -> str:
    return f"The weather in {location} is sunny, 72°F"

result = agent.run_sync('What is the weather in Paris?')

For more details, see the PydanticAI documentation.

How is this guide?

Last updated on

On this page