FastStream Code Architecture
Public vs internal split
faststream/_internal/ holds shared machinery: broker/ (abstract BrokerUsecase, registrator, router), endpoint/, di/ (fast-depends integration), context/, configs/, logger/, testing/, cli/, fastapi/, utils/.
- Broker packages (
faststream/kafka/, rabbit/, nats/, redis/, confluent/, mqtt/) are thin public layers over _internal.
- Cross-broker public packages:
faststream/middlewares/, params/, response/, specification/, message/, asgi/, opentelemetry/, prometheus/.
Rule: implement shared behavior in _internal/, expose it through broker packages. User-facing code (docs, examples, error messages) must never import from faststream._internal.
Broker package anatomy
Every broker package mirrors the same layout. Canonical reference: faststream/kafka/.
faststream/<broker>/
├── __init__.py # public exports with explicit __all__
├── annotations.py # broker-specific Annotated type aliases
├── broker/ # broker.py (BrokerUsecase subclass), router.py, registrator.py, logging.py
├── configs/ # @dataclass(kw_only=True) configs inheriting BrokerConfig
├── message.py # StreamMessage subclass
├── parser.py # message parser
├── publisher/ # publisher endpoint + producer.py
├── subscriber/ # subscriber endpoint (usecase.py; nats/redis split into usecases/)
├── response.py # PublishCommand subclasses
├── security.py # auth/security helpers
├── testing.py # in-memory TestBroker
└── exceptions.py # broker-specific exceptions
Brokers also carry optional integration subpackages where supported — kafka has fastapi/, helpers/, opentelemetry/, prometheus/, and schemas/ — follow kafka's structure when adding these to another broker.
Feature mirroring
All brokers expose the same surface: publish(), request(), ping(), start(), stop(), routers, publishers, message/response types. When adding a feature:
- Find the closest analogue in another broker (kafka is usually the most complete) and follow its shape and naming.
- Keep the public API identical across brokers unless the feature is inherently broker-specific.
- Broker-specific features stay in the broker package — don't leak them into
_internal/.
Typing
- mypy runs with
strict = true (see [tool.mypy] in pyproject.toml): every function fully annotated, no implicit Optional, decorators typed. Checked paths: faststream/ and tests/mypy/.
- Generics are used for broker abstractions:
BrokerUsecase[MsgType, ConnectionType, BrokerConfigType] (see faststream/_internal/broker/broker.py), BaseMiddleware[PublishCommandType, AnyMsg].
- Import
Callable, Awaitable, Sequence, Mapping from collections.abc; newer typing features (Self, ParamSpec, TypedDict, ...) from typing_extensions.
- Connection kwargs use
TypedDict (e.g. KafkaInitKwargs in faststream/kafka/broker/broker.py).
- Pydantic v1/v2 and Python-version differences go through
faststream/_internal/_compat.py — never inline version checks elsewhere.
Configs
Config classes are @dataclass(kw_only=True) inheriting BrokerConfig (base in faststream/_internal/configs/). Example: faststream/kafka/configs/broker.py.
Public API
- Every
__init__.py declares __all__ explicitly.
- Optional dependencies are guarded with try/except raising an
ImportError that tells the user which extra to install — see faststream/kafka/__init__.py.
Style
- ruff uses
select = ["ALL"] with curated ignores in ruff.toml — don't assume a rule is disabled; run just linter to check.
- Line length 90, double quotes, Google-style docstrings.
just mypy must pass before a PR.
Comments
A comment carries the why — the constraint, the surprise, the reason this line is not the obvious one. What the code does is already on the screen.
- Two lines, maximum. A caveat needing more than that is usually a docstring, an ADR, or a sign the code should be clearer.
- Directly above the line it explains, so the reader meets the explanation at the moment the code surprises them. A caveat about one assertion belongs over that assertion, not in the docstring header several screens up.
- Name the issue when the code exists because of a reported bug:
# ... which no broker can express (see issue #3056). The next reader gets the whole investigation for free.
Related skills
- testing-patterns — every source change needs tests following the base-testcase model.
- dev-workflow — full command reference (lint, mypy, docker brokers).
- documentation-writing — user-facing features need docs with tested snippets.
1---2name: code-architecture3description: Use when writing or modifying FastStream library source code under faststream/ — package layout, broker package anatomy, typing rules, configs, and public API conventions.4---56# FastStream Code Architecture78## Public vs internal split910- `faststream/_internal/` holds shared machinery: `broker/` (abstract `BrokerUsecase`, registrator, router), `endpoint/`, `di/` (fast-depends integration), `context/`, `configs/`, `logger/`, `testing/`, `cli/`, `fastapi/`, `utils/`.11- Broker packages (`faststream/kafka/`, `rabbit/`, `nats/`, `redis/`, `confluent/`, `mqtt/`) are thin public layers over `_internal`.12- Cross-broker public packages: `faststream/middlewares/`, `params/`, `response/`, `specification/`, `message/`, `asgi/`, `opentelemetry/`, `prometheus/`.1314**Rule:** implement shared behavior in `_internal/`, expose it through broker packages. User-facing code (docs, examples, error messages) must never import from `faststream._internal`.1516## Broker package anatomy1718Every broker package mirrors the same layout. Canonical reference: `faststream/kafka/`.1920```21faststream/<broker>/22├── __init__.py # public exports with explicit __all__23├── annotations.py # broker-specific Annotated type aliases24├── broker/ # broker.py (BrokerUsecase subclass), router.py, registrator.py, logging.py25├── configs/ # @dataclass(kw_only=True) configs inheriting BrokerConfig26├── message.py # StreamMessage subclass27├── parser.py # message parser28├── publisher/ # publisher endpoint + producer.py29├── subscriber/ # subscriber endpoint (usecase.py; nats/redis split into usecases/)30├── response.py # PublishCommand subclasses31├── security.py # auth/security helpers32├── testing.py # in-memory TestBroker33└── exceptions.py # broker-specific exceptions34```3536Brokers also carry optional integration subpackages where supported — kafka has `fastapi/`, `helpers/`, `opentelemetry/`, `prometheus/`, and `schemas/` — follow kafka's structure when adding these to another broker.3738## Feature mirroring3940All brokers expose the same surface: `publish()`, `request()`, `ping()`, `start()`, `stop()`, routers, publishers, message/response types. When adding a feature:41421. Find the closest analogue in another broker (kafka is usually the most complete) and follow its shape and naming.432. Keep the public API identical across brokers unless the feature is inherently broker-specific.443. Broker-specific features stay in the broker package — don't leak them into `_internal/`.4546## Typing4748- mypy runs with `strict = true` (see `[tool.mypy]` in `pyproject.toml`): every function fully annotated, no implicit `Optional`, decorators typed. Checked paths: `faststream/` and `tests/mypy/`.49- Generics are used for broker abstractions: `BrokerUsecase[MsgType, ConnectionType, BrokerConfigType]` (see `faststream/_internal/broker/broker.py`), `BaseMiddleware[PublishCommandType, AnyMsg]`.50- Import `Callable`, `Awaitable`, `Sequence`, `Mapping` from `collections.abc`; newer typing features (`Self`, `ParamSpec`, `TypedDict`, ...) from `typing_extensions`.51- Connection kwargs use `TypedDict` (e.g. `KafkaInitKwargs` in `faststream/kafka/broker/broker.py`).52- Pydantic v1/v2 and Python-version differences go through `faststream/_internal/_compat.py` — never inline version checks elsewhere.5354## Configs5556Config classes are `@dataclass(kw_only=True)` inheriting `BrokerConfig` (base in `faststream/_internal/configs/`). Example: `faststream/kafka/configs/broker.py`.5758## Public API5960- Every `__init__.py` declares `__all__` explicitly.61- Optional dependencies are guarded with try/except raising an `ImportError` that tells the user which extra to install — see `faststream/kafka/__init__.py`.6263## Style6465- ruff uses `select = ["ALL"]` with curated ignores in `ruff.toml` — don't assume a rule is disabled; run `just linter` to check.66- Line length 90, double quotes, Google-style docstrings.67- `just mypy` must pass before a PR.6869### Comments7071A comment carries the **why** — the constraint, the surprise, the reason this line is not the obvious one. What the code does is already on the screen.7273- **Two lines, maximum.** A caveat needing more than that is usually a docstring, an ADR, or a sign the code should be clearer.74- **Directly above the line it explains**, so the reader meets the explanation at the moment the code surprises them. A caveat about one assertion belongs over that assertion, not in the docstring header several screens up.75- **Name the issue** when the code exists because of a reported bug: `# ... which no broker can express (see issue #3056).` The next reader gets the whole investigation for free.7677## Related skills7879- **testing-patterns** — every source change needs tests following the base-testcase model.80- **dev-workflow** — full command reference (lint, mypy, docker brokers).81- **documentation-writing** — user-facing features need docs with tested snippets.