Skip to main content

Add an AI Agent to your FastAPI application. The agent knows how to interact with your endpoints within a chat interface.

Project description

🤖 FastAPI Agent

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

FastAPI Agent integrates an AI Agent into your FastAPI application.
It allows interaction with your API endpoints through a chat interface or directly via the /agent/query API route.

⚙️ Installation:

To install the package, run:

pip install 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 app 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,
)

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() methode - Example

🧩 Additional Arguments:

If your application routes use Depends (e.g., an API key), you can pass a dictionary of headers.
The agent will use them to call your routes and apply the same dependencies to the /agent/query route.

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

FastAPIAgent(
    app,
    model="openai:gpt-4.1-mini",
    base_url="https://localhost:8000",
    deps={"api-key": api_key},
    include_router=True,
)

You can also pass the ignore_routes argument to prevent the agent from accessing specific routes in your application:

FastAPIAgent(
    app,
    model="openai:gpt-4.1-mini",
    base_url="https://localhost:8000",
    ignore_routes=["/user/delete/{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 Depends in your routes, make sure to pass the required headers when calling the /agent/query endpoint like the examples below:

python

import requests

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

curl

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

📜 License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_agent-0.1.5.tar.gz (121.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_agent-0.1.5-py3-none-any.whl (26.0 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_agent-0.1.5.tar.gz.

File metadata

  • Download URL: fastapi_agent-0.1.5.tar.gz
  • Upload date:
  • Size: 121.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.1

File hashes

Hashes for fastapi_agent-0.1.5.tar.gz
Algorithm Hash digest
SHA256 c08bda11fb7b41085d84b9934781083405b23e91b96803afe12ca79b7d057c0f
MD5 c311b0d8b92f8cf2a80915e0a5d542e3
BLAKE2b-256 1eebb35ebefdc60aaf3a973211a2d82e16ad091f88298c3ff56c960be0e4f5fe

See more details on using hashes here.

File details

Details for the file fastapi_agent-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: fastapi_agent-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 26.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.1

File hashes

Hashes for fastapi_agent-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 e413ebbf92d48dbaa073444eb4999e5858f3257224fe6ae85fb99be45b47331c
MD5 429bc247cd942f68203852df911267d0
BLAKE2b-256 04450a8776ced9da2a468c91eb002e056a4a5b759be1185275dd16df2cf987d2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page