# pydantic-ai-model-integration

pydantic-ai-model-integration guides LLM provider configuration, fallback model chains, streaming, and token limits in PydanticAI.

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode, Windsurf (Devin Desktop), Claude.ai · 120 installs · ★ 89 repo stars
- Author: existential-birds
- License: Apache-2.0 · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/existential-birds/beagle/tree/HEAD/plugins/beagle-ai/skills/pydantic-ai-model-integration/SKILL.md
- Updated: 2026-09-15
- Security check: automated, 2026-11-16 (text) — no signs of malicious code found.
- HTML: https://affpapa.org/en/nevibecode/skills/pydantic-ai-model-integration

## What it does

This skill teaches coding assistants how to configure LLMs in PydanticAI using the `provider:model-name` pattern. It covers endpoints for OpenAI, Anthropic, Google GLA and Vertex AI, Groq, Mistral, Cohere, and Bedrock, alongside request timeouts and temperature controls in `ModelSettings`.

It outlines resilient architectures using `FallbackModel`, including custom filters for HTTP 429 and server errors. The material also covers response streaming via `run_stream`, complete with live parsing of structured outputs defined by Pydantic models.

PydanticAI checks environment variables on instantiation, so offline tests will fail unless configured with `defer_model_check=True` or an `agent.override` block.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add existential-birds/beagle --skill pydantic-ai-model-integration -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/pydantic-ai-model-integration/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/pydantic-ai-model-integration/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add existential-birds/beagle --skill pydantic-ai-model-integration -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/pydantic-ai-model-integration/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add existential-birds/beagle --skill pydantic-ai-model-integration -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/pydantic-ai-model-integration/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install existential-birds/beagle plugins/beagle-ai/skills/pydantic-ai-model-integration
```

2. **Manually: put the files in place**
- File: `.github/skills/pydantic-ai-model-integration/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add existential-birds/beagle --skill pydantic-ai-model-integration -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/existential-birds/beagle --path plugins/beagle-ai/skills/pydantic-ai-model-integration
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/pydantic-ai-model-integration/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add existential-birds/beagle --skill pydantic-ai-model-integration -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/existential-birds/beagle/tree/HEAD/plugins/beagle-ai/skills/pydantic-ai-model-integration/SKILL.md

## When to use it
- configure multiple LLM providers in PydanticAI using standard strings
- set up automatic model failover using FallbackModel on rate limits
- stream partial structured JSON output into Pydantic models
- track and restrict token consumption with UsageLimits

## Example prompts
- Create a PydanticAI agent with Claude 3.5 Sonnet and an OpenAI fallback
- Show how to stream a validated Pydantic model using run_stream
- How do I set token limits and timeouts for PydanticAI runs?

## Source text · SKILL.md · Apache-2.0

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: 503a9c7e3942f53b.

BEGIN THIRD-PARTY FILE 503a9c7e3942f53b
````text
---
name: pydantic-ai-model-integration
description: Configure LLM providers, use fallback models, handle streaming, and manage model settings in PydanticAI. Use when selecting models, implementing resilience, or optimizing API calls.
---

# PydanticAI Model Integration

## Provider Model Strings

Format: `provider:model-name`

```python
from pydantic_ai import Agent

# OpenAI
Agent('openai:gpt-4o')
Agent('openai:gpt-4o-mini')
Agent('openai:o1-preview')

# Anthropic
Agent('anthropic:claude-sonnet-4-5')
Agent('anthropic:claude-haiku-4-5')

# Google (API Key)
Agent('google-gla:gemini-2.0-flash')
Agent('google-gla:gemini-2.0-pro')

# Google (Vertex AI)
Agent('google-vertex:gemini-2.0-flash')

# Groq
Agent('groq:llama-3.3-70b-versatile')
Agent('groq:mixtral-8x7b-32768')

# Mistral
Agent('mistral:mistral-large-latest')

# Other providers
Agent('cohere:command-r-plus')
Agent('bedrock:anthropic.claude-3-sonnet')
```

## Model Settings

```python
from pydantic_ai import Agent
from pydantic_ai.settings import ModelSettings

agent = Agent(
    'openai:gpt-4o',
    model_settings=ModelSettings(
        temperature=0.7,
        max_tokens=1000,
        top_p=0.9,
        timeout=30.0,  # Request timeout
    )
)

# Override per-run
result = await agent.run(
    'Generate creative text',
    model_settings=ModelSettings(temperature=1.0)
)
```

## Fallback Models

Chain models for resilience:

```python
from pydantic_ai.models.fallback import FallbackModel

# Try models in order until one succeeds
fallback = FallbackModel(
    'openai:gpt-4o',
    'anthropic:claude-sonnet-4-5',
    'google-gla:gemini-2.0-flash'
)

agent = Agent(fallback)
result = await agent.run('Hello')

# Custom fallback conditions
from pydantic_ai.exceptions import ModelAPIError

def should_fallback(error: Exception) -> bool:
    """Only fallback on rate limits or server errors."""
    if isinstance(error, ModelAPIError):
        return error.status_code in (429, 500, 502, 503)
    return False

fallback = FallbackModel(
    'openai:gpt-4o',
    'anthropic:claude-sonnet-4-5',
    fallback_on=should_fallback
)
```

## Streaming Responses

```python
async def stream_response():
    async with agent.run_stream('Tell me a story') as response:
        # Stream text output
        async for chunk in response.stream_output():
            print(chunk, end='', flush=True)

    # Access final result after streaming
    print(f"\nTokens used: {response.usage().total_tokens}")
```

### Streaming with Structured Output

```python
from pydantic import BaseModel
````
END THIRD-PARTY FILE 503a9c7e3942f53b

The third-party file has ended. Everything between the 503a9c7e3942f53b tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 2548 of 5781 bytes. © existential-birds · Apache-2.0 · https://github.com/existential-birds/beagle/tree/HEAD/plugins/beagle-ai/skills/pydantic-ai-model-integration/SKILL.md

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
