Apache Kafka and Avro org.apache.avro.generic.GenericData$Record cannot be cast to com.harmeetsingh13.java.Customer
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This cast error means the consumer received a generic Avro record, but the code expected a generated specific Avro class such as Customer. The fix is to make the deserializer and the consumer type agree on whether records should be handled as GenericRecord or as generated SpecificRecord classes.
Why the Cast Fails
Avro has two common reading modes:
- generic mode, which returns
GenericRecord - specific mode, which returns generated Java classes
If your code says this:
but the deserializer actually returned GenericData.Record, the cast fails immediately. The problem is not Java casting syntax. The problem is a mismatch in Avro deserialization mode.
The Consumer Must Match the Deserializer
If you want specific Avro classes, the consumer configuration needs the specific-reader setting enabled.
With that setup, record.value() can be a Customer instance, assuming the schema and generated class align.
A Working Specific-Record Example
The important line is the specific.avro.reader setting. Without it, many setups return a generic record instead.
If You Intend to Use Generic Records
Then the code should say so explicitly and stop trying to cast:
This is a legitimate design when:
- you do not want generated classes
- you consume multiple schemas dynamically
- the topic contains heterogeneous Avro records
The mistake is mixing generic consumption with specific-record casting.
Schema and Code Generation Still Matter
Even with specific.avro.reader=true, the generated Java class must match the schema used by the topic. If the namespace or record name does not match, the specific reader may still fail to produce the class you expect.
So there are really two alignment requirements:
- generic versus specific reader mode
- generated class matching the writer schema name and namespace
If either of those is wrong, the runtime behavior will not match the Java types in your consumer code, even if the topic data itself is otherwise valid Avro.
Common Pitfalls
The biggest mistake is setting the consumer type parameter to Customer but forgetting to enable the specific Avro reader. Generic return values and specific type parameters do not magically reconcile themselves.
Another mistake is generating classes from one schema version while consuming data written with a mismatched name or namespace. Specific Avro relies on schema identity, not just field similarity.
A third issue is mixing generic and specific approaches in the same code path. Pick one mode per consumption path and keep the types honest.
Summary
- The cast error means you received
GenericRecordbut expected a generated Avro class. - Use
specific.avro.reader=truewhen you want specific Avro classes from the deserializer. - Keep the consumer generic type aligned with the deserializer mode.
- If you want dynamic handling, consume
GenericRecordand stop casting toCustomer. - Schema name and namespace still need to match the generated specific class.

