Pydantic AI
A Pydantic AI app (Python pydantic-ai 2.x) whose agents run on your own OpenAI- or Anthropic-compatible endpoints, with the right model class, provider, base URL, auth, model profile, output mode, tools and streaming, and a probe that proves it.
Scope: configuring Pydantic AI models against custom endpoints and fixing the failures that follow. Pydantic Logfire, evals and graph design are out of scope. Running the endpoints belongs to vllm, llama-cpp, ollama, the gateway to litellm and llama-swap; other clients are in llm-clients.
Work through the steps in order. Load a reference only when a step names it.
Discover first
Read-only, never prints secret values.
python3 --version; (uv --version || pip --version) 2>/dev/null
pip list 2>/dev/null | grep -Ei '^(pydantic[-_]ai|openai|anthropic|httpx)' || uv pip list 2>/dev/null | grep -Ei '^(pydantic[-_]ai|openai|anthropic)'
grep -rnE "OpenAIChatModel|OpenAIResponsesModel|OpenAIProvider|VLLMProvider|OllamaProvider|LiteLLMProvider|AnthropicProvider|base_url|Agent\(['\"]" --include='*.py' --include='*.env*' --include='*.toml' -l . 2>/dev/null | grep -v -E '\.venv|node_modules' | head
env | cut -d= -f1 | grep -E '^(OPENAI|ANTHROPIC|OLLAMA|VLLM|LLM)_' || true # names only
curl -fsS https://pypi.org/pypi/pydantic-ai/json | jq -r .info.version # newest release
Ask only what discovery cannot show: which API the endpoint serves, whether a gateway sits in front, and the model names GET $BASE/models returns.
Decision flow
- Endpoint speaks Chat Completions (llama-server, llama-swap, LiteLLM, any gateway):
OpenAIChatModel with OpenAIProvider(base_url=...). -> references/custom-endpoints.md
- vLLM:
VLLMProvider. Ollama: OllamaProvider. LiteLLM proxy: LiteLLMProvider. -> references/custom-endpoints.md
- Endpoint speaks Responses:
OpenAIResponsesModel. The bare openai: prefix means Responses. -> references/custom-endpoints.md
- Endpoint speaks Anthropic Messages:
AnthropicModel with an AsyncAnthropic client. -> references/custom-endpoints.md
- Structured output, tools, streaming: pick the output mode and profile flags. ->
references/structured-output-tools-streaming.md
- New major or old code: ->
references/updating.md
- Error or odd behavior: ->
references/troubleshooting.md
Rules
- Ask before changing the system. Package installs and
.env edits need approval.
- Secrets never touch output. Read keys from environment variables or a
0600 file, never hardcode or log them, check presence by name only.
- Pick the model class by API.
'openai:name' is Responses; use OpenAIChatModel (or 'openai-chat:') for chat-only endpoints.
OpenAIProvider assumes OpenAI models. It picks the profile from the model name; for other models configure an OpenAIModelProfile, or use VLLMProvider, OllamaProvider or LiteLLMProvider.
- Tools need server support. vLLM needs
--enable-auto-tool-choice --tool-call-parser <name>; llama-server needs --jinja.
- Test per model alias. Chat working proves nothing about tools or
json_schema; run scripts/probe.sh.
- Verify upstream. Versions and options come from pydantic.dev/docs/ai (send
Accept: text/markdown) and PyPI at use time.
Workflows
Connect an app to a self-hosted endpoint
Debug
Reference index
| File |
Read when |
references/custom-endpoints.md |
Model classes, providers, base URLs per backend, auth, profiles |
references/structured-output-tools-streaming.md |
Output modes, tools, streaming, thinking, system-message limits |
references/updating.md |
Package versions, what to pin, upgrade steps |
references/troubleshooting.md |
Symptom to cause to fix |
references/combos/litellm-and-llama-swap.md |
Gateway in front of engines: aliases, keys, bridging |
Template: templates/env.example. Script: scripts/probe.sh (chat, tools, json_schema, streamed usage, Messages).
Companion skills
Install any with npx skills add ScaleDockHQ/llm-skills --skill <name>.
llm-clients: other SDKs, coding agents and chat UIs against the same endpoint.
litellm: the gateway that gives one key, stable aliases and API bridging.
llama-swap: hot-swapping local models behind one URL.
vllm, llama-cpp, ollama: the engines and their tool-calling flags.
langfuse: tracing agent runs through OpenTelemetry.
1---2name: pydantic-ai3description: Pydantic AI (Python pydantic-ai), the typed agent framework, pointed at self-hosted endpoints. Use to configure OpenAIChatModel with OpenAIProvider, VLLMProvider, OllamaProvider, LiteLLMProvider or AnthropicProvider custom base URLs for vLLM, llama.cpp, Ollama, LiteLLM or llama-swap, handle keys, model profiles, output_type modes (ToolOutput, NativeOutput, PromptedOutput), tools and streaming, or fix 404 on /responses, single-system-message 400s and ignored thinking. Not for gateways (litellm) or other clients (llm-clients).4license: MIT5compatibility: Needs Python 3.11+, pip or uv, and curl and jq for the probe script. Installing packages or editing app config needs the user's approval.6metadata:7 source: https://skillmd.com/skills/scaledockhq/pydantic-ai8 author: ScaleDockHQ9 version: "0.1.0"10---1112# Pydantic AI1314A Pydantic AI app (Python `pydantic-ai` 2.x) whose agents run on your own OpenAI- or Anthropic-compatible endpoints, with the right model class, provider, base URL, auth, model profile, output mode, tools and streaming, and a probe that proves it.1516**Scope:** configuring Pydantic AI models against custom endpoints and fixing the failures that follow. Pydantic Logfire, evals and graph design are out of scope. Running the endpoints belongs to `vllm`, `llama-cpp`, `ollama`, the gateway to `litellm` and `llama-swap`; other clients are in `llm-clients`.1718Work through the steps in order. Load a reference only when a step names it.1920## Discover first2122Read-only, never prints secret values.2324```bash25python3 --version; (uv --version || pip --version) 2>/dev/null26pip list 2>/dev/null | grep -Ei '^(pydantic[-_]ai|openai|anthropic|httpx)' || uv pip list 2>/dev/null | grep -Ei '^(pydantic[-_]ai|openai|anthropic)'27grep -rnE "OpenAIChatModel|OpenAIResponsesModel|OpenAIProvider|VLLMProvider|OllamaProvider|LiteLLMProvider|AnthropicProvider|base_url|Agent\(['\"]" --include='*.py' --include='*.env*' --include='*.toml' -l . 2>/dev/null | grep -v -E '\.venv|node_modules' | head28env | cut -d= -f1 | grep -E '^(OPENAI|ANTHROPIC|OLLAMA|VLLM|LLM)_' || true # names only29curl -fsS https://pypi.org/pypi/pydantic-ai/json | jq -r .info.version # newest release30```3132Ask only what discovery cannot show: which API the endpoint serves, whether a gateway sits in front, and the model names `GET $BASE/models` returns.3334## Decision flow35361. **Endpoint speaks Chat Completions (llama-server, llama-swap, LiteLLM, any gateway):** `OpenAIChatModel` with `OpenAIProvider(base_url=...)`. -> `references/custom-endpoints.md`372. **vLLM:** `VLLMProvider`. **Ollama:** `OllamaProvider`. **LiteLLM proxy:** `LiteLLMProvider`. -> `references/custom-endpoints.md`383. **Endpoint speaks Responses:** `OpenAIResponsesModel`. The bare `openai:` prefix means Responses. -> `references/custom-endpoints.md`394. **Endpoint speaks Anthropic Messages:** `AnthropicModel` with an `AsyncAnthropic` client. -> `references/custom-endpoints.md`405. **Structured output, tools, streaming:** pick the output mode and profile flags. -> `references/structured-output-tools-streaming.md`416. **New major or old code:** -> `references/updating.md`427. **Error or odd behavior:** -> `references/troubleshooting.md`4344## Rules4546- **Ask before changing the system.** Package installs and `.env` edits need approval.47- **Secrets never touch output.** Read keys from environment variables or a `0600` file, never hardcode or log them, check presence by name only.48- **Pick the model class by API.** `'openai:name'` is Responses; use `OpenAIChatModel` (or `'openai-chat:'`) for chat-only endpoints.49- **`OpenAIProvider` assumes OpenAI models.** It picks the profile from the model name; for other models configure an `OpenAIModelProfile`, or use `VLLMProvider`, `OllamaProvider` or `LiteLLMProvider`.50- **Tools need server support.** vLLM needs `--enable-auto-tool-choice --tool-call-parser <name>`; llama-server needs `--jinja`.51- **Test per model alias.** Chat working proves nothing about tools or `json_schema`; run `scripts/probe.sh`.52- **Verify upstream.** Versions and options come from pydantic.dev/docs/ai (send `Accept: text/markdown`) and PyPI at use time.5354## Workflows5556### Connect an app to a self-hosted endpoint5758- [ ] Run discovery; note installed `pydantic-ai` version.59- [ ] Probe outside Pydantic AI: `BASE_URL=... MODEL=... KEY_FILE=... scripts/probe.sh` -> `references/custom-endpoints.md`.60- [ ] `pip install pydantic-ai` (or `pydantic-ai-slim[openai]`); use the starter snippet.61- [ ] `agent.run_sync`, then `run_stream`.62- [ ] Add `output_type` and a tool; choose the output mode per probe results -> `references/structured-output-tools-streaming.md`.6364### Debug6566- [ ] Reproduce with `scripts/probe.sh`; if curl works and Pydantic AI does not, compare model class, provider, profile and env redirects -> `references/troubleshooting.md`.6768## Reference index6970| File | Read when |71| ------------------------------------------------- | --------------------------------------------------------------- |72| `references/custom-endpoints.md` | Model classes, providers, base URLs per backend, auth, profiles |73| `references/structured-output-tools-streaming.md` | Output modes, tools, streaming, thinking, system-message limits |74| `references/updating.md` | Package versions, what to pin, upgrade steps |75| `references/troubleshooting.md` | Symptom to cause to fix |76| `references/combos/litellm-and-llama-swap.md` | Gateway in front of engines: aliases, keys, bridging |7778Template: `templates/env.example`. Script: `scripts/probe.sh` (chat, tools, `json_schema`, streamed usage, Messages).7980## Companion skills8182Install any with `npx skills add ScaleDockHQ/llm-skills --skill <name>`.8384- `llm-clients`: other SDKs, coding agents and chat UIs against the same endpoint.85- `litellm`: the gateway that gives one key, stable aliases and API bridging.86- `llama-swap`: hot-swapping local models behind one URL.87- `vllm`, `llama-cpp`, `ollama`: the engines and their tool-calling flags.88- `langfuse`: tracing agent runs through OpenTelemetry.