AI Agent interface library for FastAPI with multi-platform support
Project description
FastAPI AgentRouter
Simplified AI Agent integration for FastAPI with Slack support.
Features
- 🚀 Simple Integration - Just 2 lines to add agent to your FastAPI app
- 🤖 Vertex AI ADK Support - Native support for Google's Agent Development Kit
- 💬 Slack Integration - Built-in Slack Bolt integration with lazy listeners
- 🎯 Protocol-Based - Works with any agent implementing
stream_querymethod - ⚡ Async & Streaming - Full async support with streaming responses
- 🧩 Dependency Injection - Leverage FastAPI's DI system
- 📁 Modular Architecture - Clean separation of concerns
Installation
# Basic installation
pip install fastapi-agentrouter
# With Slack support
pip install "fastapi-agentrouter[slack]"
# With Vertex AI ADK support
pip install "fastapi-agentrouter[vertexai]"
# All extras
pip install "fastapi-agentrouter[all]"
Quick Start
from fastapi import FastAPI
from fastapi_agentrouter import router, get_agent_placeholder
# Your agent implementation
class MyAgent:
def stream_query(self, *, message: str, **kwargs):
# Process the message and yield responses
yield f"Response to: {message}"
app = FastAPI()
# Two-line integration!
app.dependency_overrides[get_agent_placeholder] = lambda: MyAgent()
app.include_router(router)
That's it! Your agent is now available at:
/agent/slack/events- Handle all Slack events and interactions
Advanced Usage
With Vertex AI Agent Development Kit (ADK)
from fastapi import FastAPI
from fastapi_agentrouter import router, get_agent_placeholder
from vertexai.preview import reasoning_engines
from vertexai import Agent
# Define your agent with tools
def get_weather(city: str) -> dict:
"""Get weather for a city."""
return {"city": city, "weather": "sunny", "temperature": 25}
agent = Agent(
name="weather_agent",
model="gemini-2.5-flash-lite",
description="Weather information agent",
tools=[get_weather],
)
def get_adk_app():
return reasoning_engines.AdkApp(
agent=agent,
enable_tracing=True,
)
app = FastAPI()
app.dependency_overrides[get_agent_placeholder] = get_adk_app
app.include_router(router)
Custom Agent Implementation
from fastapi import FastAPI
from fastapi_agentrouter import router, get_agent_placeholder
class CustomAgent:
def stream_query(self, *, message: str, user_id=None, session_id=None, **kwargs):
# Your custom logic here
yield f"Response to: {message}"
def get_custom_agent():
return CustomAgent()
app = FastAPI()
app.dependency_overrides[get_agent_placeholder] = get_custom_agent
app.include_router(router)
Disabling Slack Integration
import os
from fastapi import FastAPI
from fastapi_agentrouter import router, get_agent_placeholder
# Disable Slack integration via environment variable
os.environ["DISABLE_SLACK"] = "true" # Slack endpoints will return 404
app = FastAPI()
app.dependency_overrides[get_agent_placeholder] = lambda: YourAgent()
app.include_router(router)
Configuration
Environment Variables
Configure Slack integration via environment variables using pydantic-settings:
# Required for Slack integration
export SLACK_BOT_TOKEN="xoxb-your-bot-token"
export SLACK_SIGNING_SECRET="your-signing-secret"
# Optional: Disable Slack integration
export DISABLE_SLACK="true"
Platform Setup
Slack Setup
- Create a Slack App at https://api.slack.com/apps
- Get your Bot Token and Signing Secret from Basic Information
- Set environment variables:
export SLACK_BOT_TOKEN="xoxb-your-bot-token" export SLACK_SIGNING_SECRET="your-signing-secret"
- Configure Event Subscriptions URL:
https://your-domain.com/agent/slack/events - Subscribe to bot events:
app_mention- When your bot is mentionedmessage.im- Direct messages to your bot (optional)
- For interactive components and slash commands, use the same URL:
https://your-domain.com/agent/slack/events
Agent Protocol
Your agent must implement the stream_query method:
class AgentProtocol:
def stream_query(
self,
*,
message: str,
user_id: Optional[str] = None,
session_id: Optional[str] = None,
**kwargs
) -> Iterator[Any]:
"""Stream responses for a message."""
...
The method should yield response events. For Vertex AI ADK, events have a content attribute.
API Reference
Core Components
fastapi_agentrouter.router
Pre-configured APIRouter with Slack integration:
/agent/slack/events- Main Slack event handler
fastapi_agentrouter.get_agent_placeholder
Dependency placeholder that should be overridden with your agent:
app.dependency_overrides[fastapi_agentrouter.get_agent_placeholder] = your_get_agent_function
Environment Variables
The library uses pydantic-settings for configuration management:
SLACK_BOT_TOKEN- Slack Bot User OAuth Token (required)SLACK_SIGNING_SECRET- Slack Signing Secret (required)DISABLE_SLACK=true- Disable Slack endpoints (return 404)
See the Configuration Guide for detailed documentation on all available settings.
Request body:
{
"message": "Your message here",
"user_id": "optional-user-id",
"session_id": "optional-session-id"
}
Response:
{
"response": "Agent response",
"session_id": "session-id-if-provided"
}
Examples
See the examples directory for complete examples:
- basic_usage.py - Basic integration patterns
- More examples coming soon!
Development
Setup Development Environment
# Clone the repository
git clone https://github.com/chanyou0311/fastapi-agentrouter.git
cd fastapi-agentrouter
# Install with uv (recommended)
uv sync --all-extras --dev
# Or with pip
pip install -e ".[all,dev,docs]"
# Install pre-commit hooks
pre-commit install
Run Tests
# Run all tests
pytest
# Run with coverage
pytest --cov=src --cov-report=html
# Run specific tests
pytest tests/test_router.py
Build Documentation
# Serve docs locally
mkdocs serve
# Build docs
mkdocs build
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Links
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_agentrouter-0.4.0.tar.gz.
File metadata
- Download URL: fastapi_agentrouter-0.4.0.tar.gz
- Upload date:
- Size: 23.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a612309aeb2bdceb7107d7ee408d529065a7ae60f673f6623ca3815c707d3081
|
|
| MD5 |
3e5b2cfe881ecf5317760b23c2fc59ec
|
|
| BLAKE2b-256 |
f14369f0d43a0ecc94961cd02ea6770af408b99825c7f76a5b1edc22dbd5cf52
|
Provenance
The following attestation bundles were made for fastapi_agentrouter-0.4.0.tar.gz:
Publisher:
release-please.yml on chanyou0311/fastapi-agentrouter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_agentrouter-0.4.0.tar.gz -
Subject digest:
a612309aeb2bdceb7107d7ee408d529065a7ae60f673f6623ca3815c707d3081 - Sigstore transparency entry: 380985617
- Sigstore integration time:
-
Permalink:
chanyou0311/fastapi-agentrouter@c5712bde0c41cbc3b611db69f6a2b9a9413e1652 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/chanyou0311
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-please.yml@c5712bde0c41cbc3b611db69f6a2b9a9413e1652 -
Trigger Event:
push
-
Statement type:
File details
Details for the file fastapi_agentrouter-0.4.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_agentrouter-0.4.0-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
800bd16c011e9b2685bc77bf46f47c7f3086d7ab8c2a6996e627974b6e6025e2
|
|
| MD5 |
6879bf3291a4d6af04620441ac2e6718
|
|
| BLAKE2b-256 |
99a609d8af7ae2777fd76bd40c95d29f04796b73c9fcfddaa755ae4d0cd4a2b8
|
Provenance
The following attestation bundles were made for fastapi_agentrouter-0.4.0-py3-none-any.whl:
Publisher:
release-please.yml on chanyou0311/fastapi-agentrouter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_agentrouter-0.4.0-py3-none-any.whl -
Subject digest:
800bd16c011e9b2685bc77bf46f47c7f3086d7ab8c2a6996e627974b6e6025e2 - Sigstore transparency entry: 380985647
- Sigstore integration time:
-
Permalink:
chanyou0311/fastapi-agentrouter@c5712bde0c41cbc3b611db69f6a2b9a9413e1652 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/chanyou0311
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-please.yml@c5712bde0c41cbc3b611db69f6a2b9a9413e1652 -
Trigger Event:
push
-
Statement type: