The FastAPI App is used to serve Agents or Teams using a FastAPI server with a rest api interface.

Example Usage

Create an agent, wrap it with FastAPIApp, and serve it:

from agno.agent import Agent
from agno.app.fastapi.app import FastAPIApp
from agno.app.fastapi.serve import serve_fastapi_app
from agno.models.openai import OpenAIChat

basic_agent = Agent(
    name="Basic Agent",
    model=OpenAIChat(id="gpt-4o"), # Ensure OPENAI_API_KEY is set
    add_history_to_messages=True,
    num_history_responses=3,
    add_datetime_to_instructions=True,
    markdown=True,
)

# Async router by default (use_async=True)
app = FastAPIApp(agent=basic_agent).get_app()

# For synchronous router:
# app = FastAPIApp(agent=basic_agent).get_app(use_async=False)

if __name__ == "__main__":
    # Assumes script is `basic_app.py`; update if different.
    serve_fastapi_app("basic_app:app", port=8001, reload=True)

To run:

  1. Set OPENAI_API_KEY environment variable.
  2. API at http://localhost:8001, docs at http://localhost:8001/docs.

Send POST requests to http://localhost:8001/v1/run:

{
  "message": "Hello Basic Agent, tell me a fun fact!",
  "stream": false
}

Core Components

  • FastAPIApp: Wraps Agno agents/teams for FastAPI.
  • serve_fastapi_app: Serves the FastAPI app using Uvicorn.

FastAPIApp uses helper functions for routing.

FastAPIApp Class

Main entry point for Agno FastAPI apps.

Initialization Parameters

ParameterTypeDefaultDescription
agentOptional[Agent]NoneAgno Agent instance.
teamOptional[Team]NoneAgno Team instance.
settingsOptional[APIAppSettings]NoneAPI configuration. Defaults if None.
api_appOptional[FastAPI]NoneExisting FastAPI app. New one created if None.
routerOptional[APIRouter]NoneExisting APIRouter. New one created if None.
app_idOptional[str]NoneApp identifier (autogenerated if not set).
nameOptional[str]NoneName for the App.
descriptionOptional[str]NoneDescription for the App.

Provide agent or team, not both.

Key Method

MethodParametersReturn TypeDescription
get_appuse_async: bool = True
prefix: str = "/v1"
FastAPIReturns configured FastAPI app (async by default). Sets prefix, error handlers, CORS, docs.

Endpoints

Endpoints are available at the specified prefix (default /v1).

1. POST /run

  • Description: Interacts with the agent/team (uses agent.run()/arun() or team.run()/arun()).
  • Request Form Parameters:
    ParameterTypeDefaultDescription
    messagestr...Input message (Required).
    streamboolTrue (sync), False (async default)Stream response.
    monitorboolFalseEnable monitoring.
    session_idOptional[str]NoneSession ID for conversation continuity.
    user_idOptional[str]NoneUser ID.
    filesOptional[List[UploadFile]]NoneFiles to upload.
  • Responses:
    • stream=True: StreamingResponse (text/event-stream) with JSON RunResponse/TeamRunResponse events.
    • stream=False: JSON RunResponse/TeamRunResponse dictionary.

Serving the Application (serve_fastapi_app)

Serves the FastAPI app using Uvicorn.

Parameters

ParameterTypeDefaultDescription
appUnion[str, FastAPI]N/AFastAPI app instance or import string (Required).
hoststr"localhost"Host to bind.
portint7777Port to bind.
reloadboolFalseEnable auto-reload for development.