Skip to main content

Serve Pydantic AI Agents as FastAPI Apps

pydantic-ai-app-factory lets you serve a Pydantic AI 2.0 agent as a FastAPI application. It includes support for multi-user and multi-tenant environments, capabilities, streaming, and error handling.

Quick Start

Install the package:

pip install pydantic-ai-app-factory

Create a file main.py with:

from pydantic_ai_app_factory import UserContext, create_agent, create_agent_app


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


agent = create_agent('test', instructions='You are a helpful assistant.')
app = create_agent_app(agent, auth=auth)

Launch the app like any other FastAPI app:

pip install "fastapi[standard]"
fastapi dev

With the server running, point any Vercel AI SDK frontend at POST /chat and you have a functioning chat. Or run a quick test with curl:

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

Documentation

The quick start uses a test model, a mock auth function, and creates an app without features. The documentation covers how to build something real.

  1. Building an App

    1. Agent Endpoints
    2. Example app
  2. Features

    1. Request Extras
    2. Example feature
  3. Authentication and Authorization

  4. Error Contract

  5. Testing

  6. API Reference

Motivation

FastAPI gives the Python ecosystem a standardized API server on top of Pydantic, and Pydantic AI provides the same for AI agents. pydantic-ai-app-factory is the missing "glue work" to compose Pydantic AI agents and their capabilities into FastAPI apps.

Serving AI agents via HTTP requires many recurring patterns, including but not limited to logic for:

  • configuring multi-user authentication, request validation, and other middleware
  • storing and retrieving interactions (agent runs) and conversations
  • context building
  • streaming intermediate results and run output to the client
  • error handling

It's wasteful to create this logic from scratch in every project. That's why pydantic-ai-app-factory assembles any Pydantic AI Agent into a production-ready FastAPI app using framework standards, for more maintainable and secure projects. You keep full control of your agent and application, but save yourself the boilerplate code.

Exposing an agent through a server is an established concept, and a web search for "serve Pydantic AI with FastAPI" returns many guides, tutorials, and example projects. Pydantic AI's own documentation contains an exemplary Chat App with FastAPI. Pydantic AI contributor Vstorm has released a Full-Stack AI Agent Template which comes with a FastAPI backend, among many other things. But we are not aware of any projects that provide the path from agent to app via an importable package as pydantic-ai-app-factory does.

This project builds on the contributions of Pydantic, FastAPI, and Pydantic AI. It relies heavily on the concept of capabilities as composable units of agent behavior introduced in Pydantic AI 2.0. It also uses the Vercel AI Data Stream Protocol through Pydantic AI's VercelAIAdapter.

Contributing

We welcome community contributions under Apache-2.0. Please create an issue to discuss an improvement, or submit a pull request. Clone the repository, then run:

make install   # uv sync
make all       # lint, format, typecheck, and tests with 100% branch coverage

Please ensure that make all passes before committing. Coding standards, file layout, and testing patterns can be found in AGENTS.md.

License

Apache-2.0

Metadata

Release files for pydantic-ai-app-factory 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-app-factory 0.2.0
File Size Uploaded
pydantic_ai_app_factory-0.2.0.tar.gz 73.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-ai-app-factory 0.2.0
File Interpreter ABI Platform
pydantic_ai_app_factory-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 110.6 kB

Release files / pydantic_ai_app_factory-0.2.0.tar.gz

Download URL pydantic_ai_app_factory-0.2.0.tar.gz
Size 73.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b71988331a9e47ba8b329f02beefafab5ff966bc024835f1612801355c571c17
BLAKE2b-256 checksum
How to use checksums
45a5459a8c9791c8c69c3d7cedac11962bfa1db5094fcbf15f7cef559aabaa42
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 Oct 12, 2026.

Transparency log

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

Download URL pydantic_ai_app_factory-0.2.0-py3-none-any.whl
Size 36.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e35024995a1011082a08e34fde559aff0a12ecf6c544434603bcf51a72fa94f9
BLAKE2b-256 checksum
How to use checksums
188c077748fc4c1a600c64a37a2cb2c312f29c32bb074775f60be503e46caca2
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 Oct 12, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

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