The FastAPI extension provides two integration classes for building Dapr applications with FastAPI: DaprApp for pub/sub subscriptions and DaprActor for actor hosting.
dapr/ext/fastapi/
├── __init__.py # Exports: DaprApp, DaprActor
├── app.py # DaprApp — pub/sub subscription handler
├── actor.py # DaprActor — actor runtime HTTP adapter
└── py.typed
tests/ext/fastapi/
├── test_app.py # DaprApp pub/sub tests
└── test_dapractor.py # DaprActor response wrapping + route tests
Installed via the fastapi extra: pip install "dapr[fastapi]". See RELEASE.md for migration from the legacy dapr-ext-fastapi distribution.
from dapr.ext.fastapi import DaprApp, DaprActorWraps a FastAPI instance to add Dapr pub/sub event handling.
app = FastAPI()
dapr_app = DaprApp(app, router_tags=['PubSub']) # router_tags optional, default ['PubSub']
@dapr_app.subscribe(pubsub='pubsub', topic='orders', route='/handle-order',
metadata={}, dead_letter_topic=None)
def handle_order(event_data):
return {'status': 'ok'}- Auto-registers
GET /dapr/subscribeendpoint returning subscription metadata - Each
@subscriberegisters a POST route on the FastAPI app - If
routeis omitted, defaults to/events/{pubsub}/{topic} - Subscription metadata format:
{"pubsubname", "topic", "route", "metadata", "deadLetterTopic"}
Integrates Dapr's actor runtime with FastAPI by registering HTTP endpoints.
app = FastAPI()
dapr_actor = DaprActor(app, router_tags=['Actor']) # router_tags optional, default ['Actor']
await dapr_actor.register_actor(MyActorClass)Auto-registers six endpoints:
GET /healthz— health checkGET /dapr/config— actor configuration discoveryDELETE /actors/{type}/{id}— deactivationPUT /actors/{type}/{id}/method/{method}— method invocationPUT /actors/{type}/{id}/method/timer/{timer}— timer callbackPUT /actors/{type}/{id}/method/remind/{reminder}— reminder callback
Method invocation extracts Dapr-Reentrancy-Id header for reentrant actor calls. All actor operations delegate to ActorRuntime from the core SDK.
Response wrapping (_wrap_response): Converts handler results to HTTP responses:
- String → JSON
{"message": "..."}with optionalerrorCodefor errors - Bytes → raw
Responsewith specified media type - Dict/object → JSON serialized
Error handling: Catches DaprInternalError and generic Exception, returns 500 with error details.
dapr(core, same wheel as this extension)fastapi >= 0.60.1, < 1.0.0uvicorn >= 0.11.6, < 1.0.0
uv run python -m unittest discover -v ./tests/ext/fastapitest_app.py— uses FastAPITestClientfor HTTP-level testing: subscription registration, custom routes, metadata, dead letter topics, router tagstest_dapractor.py— tests_wrap_responseutility (string, bytes, error, object), router tag propagation across all 6 actor routes
- Async actors:
register_actoris an async method (must be awaited). FastAPI actor routes are async handlers that directlyawaitthe correspondingActorRuntimeoperations on the existing event loop (noasyncio.run()or per-request event loop creation). - Router tags: Both classes support
router_tagsparameter to customize OpenAPI/Swagger documentation grouping. - No gRPC: This extension is HTTP-only. It works with Dapr's HTTP callback protocol, not gRPC.