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.1.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.1.0
File Size Uploaded
pydantic_ai_app_factory-0.1.0.tar.gz 73.2 kB Details

Built distribution (wheel)

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

Total release size: 113.6 kB

Release files / pydantic_ai_app_factory-0.1.0.tar.gz

Download URL pydantic_ai_app_factory-0.1.0.tar.gz
Size 73.2 kB
Tags Source
SHA-256 checksum
How to use checksums
99bb31dfc67d10767b3fce98dfc9a63e80e3bc9ed56a09d2f8a83737fe96fb61
BLAKE2b-256 checksum
How to use checksums
6e85149e6de70b0f91f2783e5b4a595af5b17f2caed57570fb526280d26ed8f9
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 6, 2026.

Transparency log

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

Download URL pydantic_ai_app_factory-0.1.0-py3-none-any.whl
Size 40.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d36274635e12100f7ece14a8ae43b81301a6bec480a602163cb59c80b420c28a
BLAKE2b-256 checksum
How to use checksums
34cefdd86381c5870887fc2406b49bbffc051b7b8f711131a25f50fb3887a094
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 6, 2026.

Transparency log

Release history Release notifications | RSS feed

0.2.0

2 release files

This release

0.1.0 This release

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