# Avro deserialization fails when union field has null as first element instead of last

- **ID:** `data/avro-union-null-ordering`
- **Domain:** data
- **Category:** serialization_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Some Avro libraries expect null to be the first element in a union type (e.g., ['null', 'string']), while others expect it last, causing schema compatibility issues.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Apache Avro 1.11.0 | active | — | — |
| Confluent Schema Registry 7.4.0 | active | — | — |
| Kafka 3.5.0 | active | — | — |

## Workarounds

1. **Ensure all Avro schemas use the same union ordering convention: always put null first: {"type": ["null", "string"]}** (95% success)
   ```
   Ensure all Avro schemas use the same union ordering convention: always put null first: {"type": ["null", "string"]}
   ```
2. **Use a custom deserializer that reorders union types: GenericDatumReader<GenericRecord> reader = new GenericDatumReader<>(writerSchema, readerSchema) { @Override protected Object read(Object old, Decoder in) throws IOException { return super.read(old, in); } };** (70% success)
   ```
   Use a custom deserializer that reorders union types: GenericDatumReader<GenericRecord> reader = new GenericDatumReader<>(writerSchema, readerSchema) { @Override protected Object read(Object old, Decoder in) throws IOException { return super.read(old, in); } };
   ```

## Dead Ends

- **Setting compatibility to NONE in schema registry** — Changing schema registry compatibility type does not fix the union ordering issue. (80% fail)
- **Modifying the data to include null values in a different order** — The null position is determined by the schema, not the data payload. (75% fail)
