Test DemoDocs
GuidesModelsAPI referenceApps
SDKs and frameworks

Pydantic AI

Run Pydantic AI agents in Python on this API with OpenAICompatibleProvider.

Pydantic AI reaches this API through OpenAIChatModel with an OpenAICompatibleProvider: a name for the service, the base URL https://test-demo.pinerouter.ai/v1 and your key.

Install

pip install "pydantic-ai-slim[openai]"
uv add "pydantic-ai-slim[openai]"

Configure API keys

.env
API_KEY=sk-...

Load it into the environment before you run the code (for example with python-dotenv). Keep the key out of your code.

Run

Text

import os
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai_compatible import OpenAICompatibleProvider

model = OpenAIChatModel(
    "gpt-6-luna",
    provider=OpenAICompatibleProvider("ai", base_url="https://test-demo.pinerouter.ai/v1", api_key=os.environ["API_KEY"]),
)
agent = Agent(model, instructions="You are concise.")

print(agent.run_sync("Name three rivers in Europe.").output)

Stream

import asyncio

async def main():
    async with agent.run_stream("Write a haiku about rivers.") as result:
        async for text in result.stream_text(delta=True):
            print(text, end="", flush=True)

asyncio.run(main())

Tool calling

@agent.tool_plain
def get_weather(city: str) -> str:
    """Current weather for a city."""
    return f"Sunny, 21 C in {city}"

print(agent.run_sync("What is the weather in Paris?").output)

Specifics

  • Use OpenAICompatibleProvider, not OpenAIProvider(base_url=...). OpenAIProvider assumes OpenAI's own models and features.
  • Keep the provider name ("ai" above) the same over time. Pydantic AI uses it to decide which data from earlier answers it sends back.
  • If structured output or thinking is ignored or refused by a model, set a model profile as the Pydantic AI docs describe.
  • OpenAIResponsesModel with the same provider uses /v1/responses instead of Chat Completions.
  • For images, video and other media call Jobs with the HTTP helper inside a tool.

Next