Skip to main content

GitHub stars GitHub forks GitHub issues GitHub license Last commit PyPI version PyPI downloads

FastAPI Agent Logo


💬 Talk to your FastAPI app like it's a teammate.


FastAPI Agent integrates an AI Agent into your FastAPI application.
It allows you to interact with your API endpoints through a chat interface or directly via an API route using an LLM (Large Language Model).

fastapi screenshot

⚙️ Installation:

To install the package, run:

# install with pip
pip install fastapi_agent

# install with uv
uv add fastapi_agent

🧪 Usage:

To use the FastAPI Agent, initialize it with your FastAPI app and AI model.
You can use the default agent routes or add custom ones to your FastAPI application to interact with the agent via a chat interface or API endpoint.

Here is a simple example of how to use the FastAPI Agent with your FastAPI application:

.env

OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

app.py

import uvicorn
from dotenv import load_dotenv

from fastapi import FastAPI
from fastapi_agent import FastAPIAgent

# load OPENAI_API_KEY from .env
load_dotenv()

# set your FastAPI app
app = FastAPI(
    title="YOUR APP TITLE",
    version="0.1.0",
    description="SOME DESCRIPTION",
)

# add routes
@app.get("/")
async def root():
    """Welcome endpoint that returns basic API information"""
    return {"message": "Welcome to Test API"}

# add the FastAPI Agent + default routes
FastAPIAgent(
    app,
    model="openai:gpt-4.1-mini",
    base_url="http://localhost:8000",
    include_router=True,
)

# run FastAPI
uvicorn.run(app, host="0.0.0.0", port=8000)

🧭 Default Routes

FastAPI Agent provides two default routes:

  1. /agent/query – Ask anything about your API using natural language. 🧠
curl -k -X POST "http://127.0.0.1:8000/agent/query" \
-H "Content-Type: application/json" \
-d '{"query": "show all endpoints"}'
  1. /agent/chat – A simple web-based chat interface to interact with your API. 💬

💡 You can also add custom routes using agent.chat() method - Example

💬 AI Chat - Web UI

When you integrate FastAPI Agent into your FastAPI application, it automatically adds a new endpoint at /agent/chat, which provides a minimal chat interface to interact with your API.

fastapi demo

🧩 Additional Arguments:

If your application routes use Authorizations Depends (e.g. Headers or Query String API key or HTTP_Bearer), you need to pass a dictionary of the authorizations.
The agent will use them to call your routes and also apply authorizations dependencies to /agent/query route. (see Additional Examples)

api_key = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

FastAPIAgent(
    app,
    model="openai:gpt-4.1-mini",
    base_url="https://localhost:8000",
    auth={"api-key": API_KEY} || {'Authorization': "Bearer API_KEY"},
    include_router=True,
)

You can control which routes the agent can access using the ignore_routes or allow_routes arguments:

  • Use ignore_routes to exclude specific routes from being accessible to the agent.
  • Use allow_routes to restrict the agent to only the specified routes.

Both ignore_routes and allow_routes must be a list of strings in the format: ["METHOD:/path"]

FastAPIAgent(
    app,
    model="openai:gpt-4.1-mini",
    base_url="https://localhost:8000",
    ignore_routes=["DELETE:/users/{user_id}"],
    include_router=True,
)

📁 Additional Examples:

Check out our examples for ai_agent, fastapi_discovery, and fastapi_agent.
All examples are available here.


If you're using Authorizations Depends in your routes, make sure to pass the required headers when calling the /agent/query endpoint like in the examples below:

python

import requests

res = requests.post(
    "http://127.0.0.1:8000/agent/query", 
    json={"query": "show all endpoints"},
    headers={"auth": '{"api-key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}'}
    - OR - 
    headers={"auth": '{"Authorization": "Bearer 12345678"}'}
)
print(res.json())

curl

curl -k -X POST "http://127.0.0.1:8000/agent/query" \
  -H 'auth: {"api-key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}' \
  - OR -
  -H 'auth: {"Authorization": "Bearer 12345678"}' \
  -H "Content-Type: application/json" \
  -d '{"query": "show all endpoints"}'

🧪 Development & Testing

FastAPI Agent includes a comprehensive test suite to ensure reliability and quality.

Running Tests

# Install development dependencies
pip install -e ".[dev]"

# Run all tests
pytest

# Run with coverage report
pytest --cov=fastapi_agent --cov-report=term-missing

# Run specific test file
pytest tests/test_auth.py

# Generate HTML coverage report
pytest --cov=fastapi_agent --cov-report=html

Test Coverage

  • 77 tests covering authentication detection, route discovery, and agent orchestration
  • 79% overall coverage with critical components at 80%+ coverage
  • All external dependencies (LLM calls, HTTP requests) are mocked for fast, reliable tests

📜 License

This project is licensed under the MIT License.

Metadata

Release files for fastapi-agent 0.2.8

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

Source distribution (sdist)

Source distribution for fastapi-agent 0.2.8
File Size Uploaded
fastapi_agent-0.2.8.tar.gz 3.4 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-agent 0.2.8
File Interpreter ABI Platform
fastapi_agent-0.2.8-py3-none-any.whl Python 3 none any Details

Total release size: 3.4 MB

Release files / fastapi_agent-0.2.8.tar.gz

Download URL fastapi_agent-0.2.8.tar.gz
Size 3.4 MB
Tags Source
SHA-256 checksum
How to use checksums
eeb6bd4ab1a52f3b320ee57f8c2253e306583bece2a461b5332d69bb6ee7ae8b
BLAKE2b-256 checksum
How to use checksums
bcdeee12068250e3b74872043b51d579ceaa1be2ee4425ee91b2a79664fc1a77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.13.11

Release files / fastapi_agent-0.2.8-py3-none-any.whl

Download URL fastapi_agent-0.2.8-py3-none-any.whl
Size 24.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
deb65b07e06b25c0030ddf41396befa76640afd0f0eb3cfae80ff196f5fa53df
BLAKE2b-256 checksum
How to use checksums
dada34457e5537bc2f80b265aed51f0e1c63f0724a57ae5cb4607164293adc69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.13.11

Release history Release notifications | RSS feed

This release

0.2.8 This release

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

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