How ag2ai/faststream works
FastStream is an asynchronous Python framework for building event-driven services on Kafka, RabbitMQ, NATS, Redis, and MQTT. In the principal workflow, an application registers broker handlers, starts its broker connections, receives and parses messages, and invokes application handlers; publishers send messages back through broker-specific clients. The repository also provides lifecycle management, serialization, dependency injection, AsyncAPI documentation, in-memory testing, observability, and ASGI/FastAPI integration. The graph uses documented capabilities and sampled call evidence; several feature links are dashed because their implementation was not sampled.
Main entry points: application.py Application · usecase.py Subscriber runtime · router.py Broker router · middlewares.py Middleware
Components 21
Application lifecycle
- Application
faststream/_internal/application.py - ASGI application
faststream/asgi/app.py
Messaging runtime
- Broker router
faststream/_internal/broker/router.py - Subscriber runtime
faststream/_internal/endpoint/subscriber/usecase.py - Message parsing
faststream/_internal/parser.py - Message model
faststream/message/message.py - Publisher runtime
faststream/_internal/endpoint/publisher/usecase.py - Middleware
faststream/_internal/middlewares.py - Context repository
faststream/_internal/context/repository.py
Broker integrations
- Kafka adapter
faststream/kafka/broker/registrator.py - RabbitMQ adapter
faststream/rabbit/broker/registrator.py - NATS adapter
faststream/nats/broker/registrator.py - Redis adapter
faststream/redis/broker/registrator.py - Confluent adapter
faststream/confluent/broker/registrator.py - MQTT adapter
faststream/mqtt/broker/registrator.py
Framework capabilities
- AsyncAPI generation
faststream/specification/asyncapi/factory.py - In-memory testing
faststream/_internal/testing/broker.py - Metrics and tracing
faststream/prometheus/middleware.py - FastAPI integration
faststream/_internal/fastapi/router.py
Other components
- Service developer
- Message broker
Connections 21
- Service developer → Application: configures
- Application → Broker router: registers handlers
- Application → Message broker: starts connections
faststream/_internal/application.py - Broker router → Subscriber runtime: registers subscriber
faststream/_internal/broker/router.py - Broker router → Publisher runtime: registers publisher
faststream/_internal/broker/router.py - Subscriber runtime → Message parsing: parses messages
- Subscriber runtime → Message model: handles messages
- Publisher runtime → Message broker: publishes messages
- Middleware → Subscriber runtime: wraps processing
- Application → Context repository: shares context
- Kafka adapter → Message broker: connects to
- RabbitMQ adapter → Message broker: connects to
- NATS adapter → Message broker: connects to
- Redis adapter → Message broker: connects to
- Confluent adapter → Message broker: connects to
- MQTT adapter → Message broker: connects to
- Application → AsyncAPI generation: generates contract
- Service developer → In-memory testing: tests handlers
- Middleware → Metrics and tracing: supports instrumentation
- ASGI application → Application: extends lifecycle
faststream/asgi/app.py - FastAPI integration → Application: integrates framework
faststream/_internal/fastapi/router.py