Skip to main content
You can repoint Pydantic AI through Butter using its AsyncOpenAI class, which accepts the necessary base URL and headers. See it in the example below.
import os
from openai import AsyncOpenAI
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai import OpenAIProvider

# Create custom OpenAI client pointing to Butter
openai_client = AsyncOpenAI(
    base_url="https://proxy.butter.dev/v1",
    default_headers={"Butter-Auth": f"Bearer {os.getenv('BUTTER_API_KEY')}"},
    api_key=os.getenv("OPENAI_API_KEY")
)

provider = OpenAIProvider(openai_client=openai_client)

model = OpenAIChatModel(
    model_name="gpt-4",
    provider=provider
)

agent = Agent(
    model,
    instructions="You are a helpful translation agent."
)

if __name__ == "__main__":
    result = agent.run_sync("What is the English word for mantequilla?")
    print(result.output)