# Avro deserialization fails with 'Unexpected type for field' when reading old records with new schema

- **ID:** `data/avro-schema-field-type-mismatch`
- **Domain:** data
- **Category:** schema_error
- **Error Code:** `AvroTypeException`
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

An Avro schema evolution change modified a field's type (e.g., from 'int' to 'long' or from 'string' to 'bytes') without proper backward compatibility, causing deserialization of old records to fail.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Apache Avro 1.11+ | active | — | — |
| Confluent Schema Registry 7.5+ | active | — | — |
| Kafka Avro Deserializer 7.6+ | active | — | — |

## Workarounds

1. **Create a new schema version that uses a union type to accept both old and new types: change `"type": "long"` to `"type": ["int", "long"]`. Register this as a new version and update the reader to use it.** (85% success)
   ```
   Create a new schema version that uses a union type to accept both old and new types: change `"type": "long"` to `"type": ["int", "long"]`. Register this as a new version and update the reader to use it.
   ```
2. **Use a custom Avro datum reader that overrides the type resolution logic. For Java: extend `SpecificDatumReader` and override `resolveType` to handle the old type.** (75% success)
   ```
   Use a custom Avro datum reader that overrides the type resolution logic. For Java: extend `SpecificDatumReader` and override `resolveType` to handle the old type.
   ```
3. **Reprocess old records through a converter that transforms them to the new schema, using a reader with the old schema and a writer with the new schema. Example with Apache Avro tools: `java -jar avro-tools-1.11.1.jar fromjson --schema-file old_schema.avsc old_data.json | java -jar avro-tools-1.11.1.jar tojson --schema-file new_schema.avsc > new_data.json`** (80% success)
   ```
   Reprocess old records through a converter that transforms them to the new schema, using a reader with the old schema and a writer with the new schema. Example with Apache Avro tools: `java -jar avro-tools-1.11.1.jar fromjson --schema-file old_schema.avsc old_data.json | java -jar avro-tools-1.11.1.jar tojson --schema-file new_schema.avsc > new_data.json`
   ```

## Dead Ends

- **** — This is a destructive operation that causes data loss and is often not feasible in production. (95% fail)
- **** — This does not fix the existing deserialization failure; old records still have the old type and cannot be read with the new schema. (85% fail)
- **** — This allows incompatible changes but does not help deserialize existing data; the error occurs at read time, not registration time. (70% fail)
