Skip to main content
The FastAPI adapter enables deploying agents as REST endpoints using the FastAPI framework.

Import


AgentRouter

The AgentRouter class provides a FastAPI router that serves agents or agent registries with REST endpoints.

Constructor

Parameters

Agent | AgentRegistry
required
Either a single Agent instance or an AgentRegistry for multi-agent routing.
Any
Additional keyword arguments passed to FastAPI’s APIRouter.

Raises

  • ImportError: If FastAPI is not installed.

Endpoints

Single Agent Routes

  • POST /chat - Chat with the agent
  • POST /invoke - RPC method calls
  • GET /state - Get agent state
  • POST / - Generic request handler

Registry Routes (with agent_id parameter)

  • POST /{agent_id}/chat - Chat with specific agent
  • POST /{agent_id}/invoke - RPC calls to specific agent
  • GET /{agent_id}/state - Get state of specific agent
  • POST /{agent_id} - Generic request to specific agent

Examples

Single Agent FastAPI App

Multi-Agent Registry FastAPI App

With Custom Prefix and Tags

Streaming Support


Complete Application Example


Error Handling

The FastAPI adapter returns JSON with error details:
For custom error handling, wrap the agent or use FastAPI middleware.