How to specify a byte-array serializer/deserializer in Kafka-Python
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In kafka-python, bytes are already the native wire format. That means if your keys and values are already bytes or bytearray, you often do not need any special serializer or deserializer at all.
The Default Behavior
Kafka transmits raw bytes over the network. kafka-python reflects that design:
- the producer can send raw bytes directly
- the consumer receives raw bytes by default
So the simplest byte-array setup is actually no custom serializer.
On the consumer side:
The key and value arrive as bytes unless you configured a deserializer.
When To Pass An Explicit Serializer Anyway
Sometimes teams want the configuration to document intent explicitly. In that case, you can use an identity serializer.
This is useful when upstream code might hand you bytearray objects and you want to normalize everything to immutable bytes before sending.
Explicit Byte Deserialization
The consumer side usually needs no deserializer if you want raw bytes. But again, you can make that explicit:
That does not transform the payload. It just makes the contract obvious to readers.
When You Actually Need Serialization
If your application works with strings, JSON, or domain objects, then you do need a real serializer layer.
For JSON:
This is not a byte-array serializer anymore. It is application-level encoding into bytes.
Producer And Consumer Must Agree
The important rule is not "always specify a serializer." The real rule is that producer and consumer must agree on the byte contract.
Examples:
- raw bytes on the producer means raw bytes on the consumer
- UTF-8 strings on the producer require decoding on the consumer
- JSON encoding on the producer requires JSON decoding on the consumer
If the two sides disagree, the bug usually appears as a decoding error or silently wrong interpretation of the payload.
bytes Versus bytearray
Kafka libraries conceptually send bytes. Python code may use either bytes or bytearray, but normalizing to bytes is often cleaner because it is immutable and works naturally with serializers and hashing.
That is why lambda v: bytes(v) is a useful explicit adapter when your data source emits byte arrays in varying forms.
Common Pitfalls
The most common mistake is thinking you need a special serializer just to send bytes. In kafka-python, raw bytes are already the expected payload type.
Another mistake is adding a string serializer and then forgetting to decode on the consumer, or vice versa.
Developers also sometimes use str(value).encode("utf-8") on arbitrary objects. That is usually a poor serialization format because it is not structured or stable.
Finally, be explicit about whether the application contract is raw bytes, UTF-8 text, JSON, or something else. Kafka only stores bytes; the meaning above that layer is your responsibility.
Summary
- For raw byte payloads in
kafka-python, you usually do not need a custom serializer or deserializer. - The producer can send
bytesdirectly, and the consumer receivesbytesby default. - Use identity lambdas only if you want the contract to be explicit.
- Add real serializers only when you are encoding strings, JSON, or objects into bytes.
- Producer and consumer must agree on the payload format above Kafka's raw byte layer.
Related reading
- how to specify consumer group in Kafka Spark Streaming using direct stream
- How to start Kafka listener manually?
- How to start Zookeeper and then Kafka?
- How to stop consuming message from kafka and stop calling REST API call of another service in case of failure
- How to split a delimited string to a ListString
- How to split a string into an array in Bash?
- How to specify a part of code to run in a particular thread in a multithreaded environment in python?
- How to specify credentials when connecting to boto3 S3?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.