Skip to content

API Reference

Modules:

Name Description
crewai
errors
llama_index
models
pydantic_ai
smolagents

Classes:

Name Description
FastAPIAgents

FastAPI router for managing multiple agents.

FastAPIAgents

FastAPI router for managing multiple agents.

This router is designed to be used with FastAPI to manage multiple agents, each with its own endpoint.

Parameters:

Name Type Description Default
path_prefix str

The path prefix for the agents' endpoints. Defaults to "/agents".

None
security_dependency Callable

A global security dependency for all agents. Defaults to None.

None
mode APIMode

The mode for registering routes. Defaults to "simple". Also available is "openai" which registers routes as OpenAI-compatible.

'simple'
*args list[Any]

Additional arguments to pass to the APIRouter parent class.

()
**kwargs dict[str, Any]

Additional keyword arguments to pass to the APIRouter parent class.

{}

Raises:

Type Description
ValueError

If a per-agent security dependency is defined when a global security dependency is already set.

Example:

from fastapi import FastAPI, Depends, HTTPException
from fastapi_agents import FastAPIAgents
from fastapi_agents.pydantic_ai import PydanticAIAgent
from pydantic_ai import Agent

# Initialize FastAPI app
app = FastAPI()

# Initialize FastAPIAgents
agents = FastAPIAgents(path_prefix="/agents")

# Register PydanticAI agent
agent = Agent("openai:gpt-4o-mini")
agents.register("pydanticai", PydanticAIAgent(agent), tags=["AI Agents"], description="Pydantic AI Agent")

# Include the router
app.include_router(agents)

Returns:

Name Type Description
FastAPIAgents FastAPIAgents

A FastAPI router for managing multiple agents.

Methods:

Name Description
as_app

Creates and returns a FastAPI app with the FastAPIAgents router included, and injects the register method for easy registration.

register

Register an agent with the FastAPI router.

as_app(path_prefix=None, security_dependency=None, mode='simple', *args, **kwargs) classmethod

Creates and returns a FastAPI app with the FastAPIAgents router included, and injects the register method for easy registration.

Parameters:

Name Type Description Default
path_prefix str

The path prefix for the agents' endpoints. Defaults to "/agents".

None
security_dependency Callable

A global security dependency for all agents. Defaults to None.

None
mode APIMode

The mode for registering routes. Defaults to "simple". Also available is "openai" which registers routes as OpenAI-compatible.

'simple'
*args list[Any]

Additional arguments to pass to the APIRouter parent class.

()
**kwargs dict[str, Any]

Additional keyword arguments to pass to the APIRouter parent class.

{}

Returns:

Name Type Description
FastAPI FastAPI

A FastAPI app instance with the router included and registration capability.

register(name, agent, framework='auto', router=None, tags=None, description=None, security_dependency=None)

Register an agent with the FastAPI router.

Parameters:

Name Type Description Default
name str

The name of the agent.

required
agent BaseAgent | Any

The agent instance to register.

required
framework Framework | Literal['auto', 'custom']

The framework to use for the agent. Defaults to "auto".

'auto'
router APIRouter

The router to use for the agent endpoint. Defaults to None.

None
tags List[str]

The tags to assign to the agent endpoint. Defaults to None.

None
description str

The description of the agent endpoint. Defaults to None.

None
security_dependency Callable

A per-agent security dependency. Defaults to None.

None

Raises:

Type Description
ValueError

If a per-agent security dependency is defined when a global security dependency is already set.

AgentNotFoundError

If the agent is not found in the registry.