Skip to content

fix(fastapi): handle Kafka tombstones in subscribers - #3381

Closed
Lancetnik wants to merge 6 commits into
mainfrom
stack/kafka-tombstones-consume
Closed

Lancetnik wants to merge 6 commits into
mainfrom
stack/kafka-tombstones-consume

Conversation

@Lancetnik

Copy link
Copy Markdown
Member

Summary

Carry #3028 into the lower layer of the Kafka tombstone stack. Kafka and Confluent distinguish a native null from zero bytes. The default FastAPI binding passes None to the standard optional-model handler. The upper layer carries the explicit publish marker and per-record batch metadata from #3379.

Evidence

This branch points at the reviewed #3028 head, fad698650d. On FastAPI 0.162.0, the standard optional-model case passes. Embedded body binding and previously working bytes-body signatures still need the compatibility fixes identified in review.

Merge Danger

The binding change can reject tombstones before a subscriber runs. Before merge, cover embedded and dependency bodies and confirm that previously valid bytes and required-model handlers still work on both Kafka backends.

AI assistance

The original implementation used Claude Code. Codex assembled the stack and verified the branch against #3028.

aradng and others added 6 commits September 26, 2026 16:17
`parsed_consumer()` always wraps the decoded body as `{first_arg: body}`,
so `solve_dependencies()` never sees an absent body - only a present dict
like `{"msg": None}`. It can't take the no-body shortcut it already uses
for bodyless HTTP requests, so a required field on the target model fails
validation before the handler runs. On a compacted topic that's every
tombstone, forever.

A tombstone is a property of the record ("this record has no value"), not
a value in the body slot, so it's carried as a flag on `StreamMessage`:
the kafka/confluent parsers set `tombstone=value is None`, `route.py`
passes `body=None` to `solve_dependencies` when it's set. `body` stays
`b""`, so `decode_message`, the telemetry providers, the testing brokers
and the producers are untouched and `msg: bytes` handlers are unaffected.
Other brokers never set the flag, so a plain `b"null"` payload on
Rabbit/NATS keeps its existing behavior.

Closes #1967

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JoFxdJYipYjPtMdmJaxCFN
Review asked for the core flag to stop carrying a Kafka noun. What
`route.py` decides is whether the message has a body at all - the same
question FastAPI already answers for a bodyless HTTP request - so
`StreamMessage` takes `no_body` and the other brokers get a flag that
could mean something to them.

The domain word lives one layer down: `KafkaMessage.tombstone` is a
property over `no_body` in both `faststream/kafka/message.py` and
`faststream/confluent/message.py`, so someone consuming a compacted
topic still writes `msg.tombstone` and the docs note stays accurate.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…atch does

Confluent already had a connected test driving `value=None` straight through
the raw producer, so the "a tombstone from any client, not only
`publish(None)`" claim was only proven for one of the two brokers. aiokafka
now has the mirror.

The docs note also promised more than the code delivers: `parse_batch` never
sets the flag, so `msg.tombstone` is always `False` in a `batch=True`
subscriber. Said so, rather than leaving a compacted-topic consumer to find
out. Per-record tombstones in a batch stay out of scope here.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Keep the tombstone implementation and typed regression tests from #2933. Await both producer calls and verify received records independently of partition delivery order.
Restore the single-record FastAPI tombstone fix from #3028 on top of #3378. Retain the typed, order-independent regression tests and the distinction between a null record and an empty payload.
@Lancetnik
Lancetnik added this pull request to stack #3383 November 22, 2026 19:52
@github-actions github-actions Bot added documentation Improvements or additions to documentation Confluent Issues related to `faststream.confluent` module AioKafka Issues related to `faststream.kafka` module labels Nov 22, 2026
@Lancetnik
Lancetnik marked this pull request as ready for review November 22, 2026 19:57
@Lancetnik Lancetnik closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AioKafka Issues related to `faststream.kafka` module Confluent Issues related to `faststream.confluent` module documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants