> For the complete documentation index, see [llms.txt](https://docs.cortecs.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cortecs.ai/integration-examples/agents-and-automations/pydantic-ai.md).

# Pydantic AI

[Pydantic AI](https://ai.pydantic.dev/) can use a custom OpenAI-compatible provider. Its Responses model is the preferred option for new Cortecs agents.

## 1. Install Pydantic AI

```bash
pip install "pydantic-ai-slim[openai]"
```

## 2. Configure a Responses model

```python
import os

from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIResponsesModel
from pydantic_ai.providers.openai import OpenAIProvider

provider = OpenAIProvider(
    base_url="https://api.cortecs.ai/v1",
    api_key=os.environ["CORTECS_API_KEY"],
)

model = OpenAIResponsesModel(
    os.environ["CORTECS_MODEL"],
    provider=provider,
)

agent = Agent(
    model,
    instructions="Answer with a short, practical explanation.",
)

result = agent.run_sync("How does provider routing improve reliability?")
print(result.output)
```

Choose a model that lists `tools` in `supported_features` before adding Pydantic AI tools. Cortecs does not currently document OpenAI-managed conversation storage, so avoid Pydantic AI features that depend on that service.

## Chat Completions fallback

If a Pydantic AI feature or model requires Chat Completions, change only the model class:

```python
from pydantic_ai.models.openai import OpenAIChatModel

model = OpenAIChatModel(
    os.environ["CORTECS_MODEL"],
    provider=provider,
)
```

Both configurations use the same Cortecs base URL and API key. See [API Compatibility](/api-overview/api-compatibility.md) for endpoint differences and [Advanced Usage](/routing/advanced-usage.md) for routing controls.

See the current [Pydantic AI OpenAI model documentation](https://pydantic.dev/docs/ai/models/openai/) for framework-specific options. Use only options that also appear in the Cortecs API reference.
