how to get basicproperties header field in pika python from rabbitmq messages?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Pika consumers, the properties argument is already the AMQP BasicProperties object for the message you received. If you want custom header values, the field you usually want is properties.headers, not a separate "header object."
Where the Headers Live
A RabbitMQ delivery callback in Pika normally looks like this:
The important parameters are:
- '
method: delivery metadata such as routing key and delivery tag' - '
properties: AMQP message properties' - '
body: the payload bytes'
Custom headers are stored inside properties.headers, which is usually a Python dictionary or None if no headers were set.
Reading BasicProperties Safely
The simplest safe pattern is:
That properties.headers or {} part matters. If the publisher did not send headers, properties.headers may be None.
Publishing a Message With Headers
To read headers, you first need to publish them. In Pika, you attach them when building BasicProperties.
On the consumer side, those values show up under properties.headers.
Accessing One Header Field
If you only need one header, read it directly from the dictionary.
Use get instead of direct indexing unless the header is mandatory. Direct indexing raises KeyError when the field is missing.
headers Versus Other Message Properties
Do not confuse headers with the built-in AMQP properties already available as top-level attributes.
Examples of built-in properties include:
- '
properties.correlation_id' - '
properties.reply_to' - '
properties.message_id' - '
properties.timestamp' - '
properties.content_type'
Those are not stored inside properties.headers. If a producer sets correlation_id, read it from properties.correlation_id, not properties.headers["correlation_id"].
A Practical Consumer Example
The example below prints both built-in properties and custom headers so you can see the distinction clearly.
This is the version you usually want in real code, because it acknowledges the message only after processing.
Header Types and Serialization
RabbitMQ headers are AMQP field-table values. In practice, keep them simple:
- strings
- integers
- booleans
- small structured values you know your consumers can parse
If you need rich nested data, putting JSON in the message body is often cleaner than pushing too much structure into headers.
Headers are best for metadata such as:
- trace IDs
- tenant IDs
- version flags
- retry counters
- feature switches
Common Pitfalls
- Looking for headers on
methodinstead ofproperties. - Assuming
properties.headersis always a dictionary; it may beNone. - Storing built-in properties such as
correlation_idinside custom headers and then forgetting where to read them. - Using direct dictionary indexing for optional headers and crashing on missing values.
- Putting large business payloads into headers instead of the body.
Summary
- In Pika consumers, the
propertiescallback argument is theBasicPropertiesobject. - Custom header values are available under
properties.headers. - Use
properties.headers or {}to handle messages with no headers safely. - Read built-in AMQP properties from their dedicated attributes, not from the headers dictionary.
- Keep headers small and use the message body for real payload data.

