Skip to content

fix(fastapi): bind null Kafka records inside nullable model batches - #3390

Open
Lancetnik wants to merge 4 commits into
stack/tombstone-route-batchfrom
stack/tombstone-fastapi-batch
Open

Lancetnik wants to merge 4 commits into
stack/tombstone-route-batchfrom
stack/tombstone-fastapi-batch

Conversation

@Lancetnik

Copy link
Copy Markdown
Member

Summary

Bind tombstone positions to None in a default-decoded list[Model | None]. Keep empty bytes in list[bytes] and preserve a custom decoder’s results.

Layer 7 of 7. The diff contains 127 added and removed lines, including tests and docs. Derived from #3028 and #3379.

Evidence

The shared FastAPI tests cover mixed batches through both Test brokers and verify nullable models, bytes, and custom decoding.

AI assistance

Codex prepared and tested this layer from the earlier tombstone work.

@Lancetnik
Lancetnik added this pull request to stack #3391 November 22, 2026 20:22
@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 23, 2026 05:10
@Lancetnik
Lancetnik force-pushed the stack/tombstone-fastapi-batch branch from 82ab5c8 to 6989a9a Compare November 23, 2026 19:10
Comment on lines +369 to +385
def _accepts_nullable_model_items(field: "ModelField") -> bool:
annotation = (
field.field_info.annotation if FASTAPI_V2 else getattr(field, "outer_type_", None)
)
annotation = _unwrap_annotated(annotation)
if get_origin(annotation) in {Union, UnionType}:
types = tuple(arg for arg in get_args(annotation) if arg is not type(None))
if len(types) != 1:
return False
annotation = _unwrap_annotated(types[0])
if get_origin(annotation) not in {list, Sequence}:
return False
items = get_args(annotation)
if not items:
return False
item = _unwrap_annotated(items[0])
return type(None) in get_args(item) and _is_model_annotation(item)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Batch with tombstone breaks list[bytes] with Pydantic v1. The same script gets [b""] at #3389 and crashes at #3390 when reading field_info.annotation.

Comment on lines +165 to +179
def get_tombstone_body(
message: "StreamMessage[Any]", decoded: Any
) -> TombstoneBody | None:
if not message.uses_default_decoder:
return None
if not message.has_body and _is_empty_body(decoded):
return TombstoneBody()

missing = message.batch_body_missing
if isinstance(decoded, list) and len(decoded) == len(missing) and any(missing):
return TombstoneBody([
None if absent and _is_empty_body(item) else item
for item, absent in zip(decoded, missing, strict=True)
])
return None

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rearranging the decoded batch in middleware violates the list[Model | None] binding. If consume_scope expands the list from await msg.decode(), the tombstones mask remains in its original order. On both TestBroker got a validation error on b"" instead of [None, Model(...)]

This branch has not been deployed

No deployments
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