# FastStream — Async Python Framework for Event-Driven Applications > An asynchronous Python framework for building event-driven microservices with message broker integration, dependency injection, validation, and automatic AsyncAPI documentation. ## Install Save as a script file and run: # FastStream — Async Python Framework for Event-Driven Applications ## Quick Use ```bash pip install "faststream[kafka]" ``` ```python from faststream import FastStream from faststream.kafka import KafkaBroker broker = KafkaBroker("localhost:9092") app = FastStream(broker) @broker.subscriber("orders") async def handle_order(data: dict): print(f"Processing order: {data}") ``` ## Introduction FastStream is a Python framework for building event-driven applications that consume and produce messages through brokers like Kafka, RabbitMQ, NATS, and Redis. It brings FastAPI-like developer experience to message processing with type validation and dependency injection. ## What FastStream Does - Connects to multiple message brokers with a unified subscriber/publisher API - Validates incoming messages using Pydantic models automatically - Provides dependency injection for shared resources like database connections - Generates AsyncAPI documentation from your code annotations - Includes an in-memory test broker for unit testing without infrastructure ## Architecture Overview FastStream wraps broker client libraries behind a common interface. Subscribers are async functions decorated with routing metadata. The framework handles connection lifecycle, message deserialization, validation through Pydantic, and error handling. A middleware stack allows cross-cutting concerns like logging and retry logic. ## Setup & Configuration - Install with broker extras: pip install faststream[kafka|rabbit|nats|redis] - Define a broker instance pointing to your message infrastructure - Decorate async functions as subscribers with topic/queue configuration - Configure consumer groups, acknowledgment modes, and retry policies - Deploy as a long-running service via Docker, systemd, or Kubernetes ## Key Features - FastAPI-inspired syntax with decorators, type hints, and auto-validation - Built-in testing utilities with in-memory broker simulation - Automatic AsyncAPI specification generation for documentation - Multiple broker support in a single application for bridge patterns - OpenTelemetry integration for distributed tracing across services ## Comparison with Similar Tools - **Celery** — task queue focused on job scheduling; FastStream handles real-time event streams - **Faust** — Kafka-only stream processing; FastStream supports multiple brokers - **nameko** — RPC-focused microservice framework; FastStream is event-driven with modern async - **Dramatiq** — simple task queue; FastStream provides richer validation and documentation - **Apache Kafka Streams** — JVM-native; FastStream brings similar patterns to Python ## FAQ **Q: Can I use FastStream with existing FastAPI applications?** A: Yes. FastStream provides a FastAPI integration that lets you run both HTTP and message handlers in one app. **Q: How does error handling work for failed messages?** A: FastStream supports configurable retry policies, dead-letter queues, and custom error handlers per subscriber. **Q: Does it support exactly-once processing?** A: It supports at-least-once delivery with idempotency patterns; exactly-once depends on broker capabilities. **Q: Can I process messages in batches?** A: Yes. Batch consumption is supported for Kafka and NATS with configurable batch sizes and timeouts. ## Sources - https://github.com/ag2ai/faststream - https://faststream.airt.ai --- Source: https://tokrepo.com/en/workflows/asset-595dcc58 Author: Script Depot