Skip to main content

Per-Request Model Selection for Pydantic AI Apps

pydantic-ai-model-selection is a feature for pydantic-ai-app-factory that lets a client pick the model for one run from a list the operator fixes at startup. It validates the choice at the boundary, can restrict options per user, and serves the list a frontend dropdown reads.

Quick Start

Install the package:

pip install pydantic-ai-model-selection "pydantic-ai-slim[openai]"

Create a file main.py with:

from pydantic_ai_app_factory import UserContext, create_agent, create_agent_app
from pydantic_ai_model_selection import ModelOption, ModelSelection


def auth() -> UserContext:
    return UserContext(user_id='demo')


selection = ModelSelection(
    options={
        'fast': ModelOption('openai:gpt-6-luna', label='Fast'),
        'thorough': ModelOption('openai:gpt-6.1-sol'),
    },
)
agent = create_agent('openai:gpt-6-luna', instructions='You are a helpful assistant.')
app = create_agent_app(agent, auth=auth, features=[selection])

A request that names no model runs on the agent's own model. Set OPENAI_API_KEY in the environment; a missing provider key fails startup.

Launch it like any other FastAPI app:

pip install "fastapi[standard]"
fastapi dev

A request names its model as a top-level field on /chat, or under extras on /run:

curl -N http://127.0.0.1:8000/chat \
  -H 'Content-Type: application/json' \
  -d '{"id": "test-1", "trigger": "submit-message", "model": "thorough", "messages": [{"id": "m1", "role": "user", "parts": [{"type": "text", "text": "Hello"}]}]}'

GET /model-selection/models returns the options this caller may pick, with their labels.

Documentation

Model selection covers the options, per-user restrictions, the listing route, use without create_agent_app, and the reference. It builds on the factory's Features.

Motivation

Pydantic AI can already pick a model per run through a capability's get_model(), and its models share one interface across providers. So the options can mix OpenAI, Google and a self-hosted gateway, and this package only wraps the hook as an app-factory feature.

Contributing

Contributions under Apache-2.0 are welcome; please open an issue first. make install sets up the environment and make all must pass before a commit. Conventions are in AGENTS.md.

License

Apache-2.0

Metadata

Release files for pydantic-ai-model-selection 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-ai-model-selection 0.2.0
File Size Uploaded Provenance Actions
pydantic_ai_model_selection-0.2.0.tar.gz 16.3 kB With provenance Details for pydantic_ai_model_selection-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-ai-model-selection 0.2.0
File Interpreter ABI Platform Actions
pydantic_ai_model_selection-0.2.0-py3-none-any.whl Python 3 none any Details for pydantic_ai_model_selection-0.2.0-py3-none-any.whl

Total release size: 27.4 kB

Release files / pydantic_ai_model_selection-0.2.0.tar.gz

Download URL pydantic_ai_model_selection-0.2.0.tar.gz
Size 16.3 kB
Tags Source
SHA-256 checksum
How to use checksums
e3d5abb036defc32cc75e1e5817ee3fcc3d6cc6c5a69911912bb175b04d644a1
BLAKE2b-256 checksum
How to use checksums
96cc872565f5361b6c6cb9dde2d4dea4636508f44bb3da789b15a0b493f14915
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Release files / pydantic_ai_model_selection-0.2.0-py3-none-any.whl

Download URL pydantic_ai_model_selection-0.2.0-py3-none-any.whl
Size 11.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
efbe64ad41d1b8030f5a5cace39b3f190cfe023482b732aa1a57638f70a99be4
BLAKE2b-256 checksum
How to use checksums
7b1081c07964cab4909035f254ebe6fe56cf11845c1faf8b824bcec864b12117
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Security for pydantic-ai-model-selection 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pydantic_ai_model_selection-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page