Skip to content

KafkaMessage

faststream.kafka.message.KafkaMessage #

KafkaMessage(*args, consumer, **kwargs)

Bases: StreamMessage[Union['ConsumerRecord', Tuple['ConsumerRecord', ...]]]

Represents a Kafka message in the FastStream framework.

This class extends StreamMessage and is specialized for handling Kafka ConsumerRecord objects.

Source code in faststream/kafka/message.py
def __init__(
    self,
    *args: Any,
    consumer: ConsumerProtocol,
    **kwargs: Any,
) -> None:
    super().__init__(*args, **kwargs)

    self.consumer = consumer

raw_message instance-attribute #

raw_message

body instance-attribute #

body

headers class-attribute instance-attribute #

headers = field(default_factory=dict)

batch_headers class-attribute instance-attribute #

batch_headers = field(default_factory=list)

path class-attribute instance-attribute #

path = field(default_factory=dict)

content_type class-attribute instance-attribute #

content_type = None

reply_to class-attribute instance-attribute #

reply_to = ''

message_id class-attribute instance-attribute #

message_id = field(default_factory=gen_cor_id)

correlation_id class-attribute instance-attribute #

correlation_id = field(default_factory=gen_cor_id)

processed class-attribute instance-attribute #

processed = field(default=False, init=False)

committed class-attribute instance-attribute #

committed = field(default=None, init=False)

decoded_body property writable #

decoded_body

consumer instance-attribute #

consumer = consumer

ack async #

ack()
Source code in faststream/broker/message.py
async def ack(self) -> None:
    self.committed = AckStatus.acked

reject async #

reject()
Source code in faststream/broker/message.py
async def reject(self) -> None:
    self.committed = AckStatus.rejected

decode async #

decode()

Serialize the message by lazy decoder.

Source code in faststream/broker/message.py
async def decode(self) -> Optional["DecodedMessage"]:
    """Serialize the message by lazy decoder."""
    # TODO: make it lazy after `decoded_body` removed
    return self._decoded_body

nack async #

nack()

Reject the Kafka message.

Source code in faststream/kafka/message.py
async def nack(self) -> None:
    """Reject the Kafka message."""
    if not self.committed:
        raw_message = (
            self.raw_message[0]
            if isinstance(self.raw_message, tuple)
            else self.raw_message
        )
        topic_partition = AIOKafkaTopicPartition(
            raw_message.topic,
            raw_message.partition,
        )
        self.consumer.seek(
            partition=topic_partition,
            offset=raw_message.offset,
        )
        await super().nack()