apache/flink · error · WrappingRuntimeException

Failed to serialize schema registry.

Error message

Failed to serialize schema registry.

What it means

Despite the message, this is not only about schema registries: AvroSerializationSchema.serialize throws WrappingRuntimeException('Failed to serialize schema registry.') whenever datumWriter.write() or encoder.flush() fails with an IOException while encoding the record to the in-memory stream. Registry-specific I/O is handled elsewhere; here the cause is typically a record/schema mismatch or encoder state corruption.

Source

Thrown at flink-formats/flink-avro/src/main/java/org/apache/flink/formats/avro/AvroSerializationSchema.java:174

            this.schema = new Parser().parse(schemaString);
        }
    }

    @Override
    public byte[] serialize(T object) {
        checkAvroInitialized();

        if (object == null) {
            return null;
        } else {
            try {
                datumWriter.write(object, encoder);
                encoder.flush();
                byte[] bytes = arrayOutputStream.toByteArray();
                arrayOutputStream.reset();
                return bytes;
            } catch (IOException e) {
                throw new WrappingRuntimeException("Failed to serialize schema registry.", e);
            }
        }
    }

    protected void checkAvroInitialized() {
        if (datumWriter != null) {
            return;
        }
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        if (SpecificRecord.class.isAssignableFrom(recordClazz)) {
            Schema schema = SpecificData.get().getSchema(recordClazz);
            this.datumWriter = new SpecificDatumWriter<>(schema);
            this.schema = schema;
        } else {
            this.schema = new Schema.Parser().parse(this.schemaString);
            GenericData genericData = new GenericData(cl);

            this.datumWriter = new GenericDatumWriter<>(schema, genericData);

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Check e.getCause() for Avro's encoder exception and reconcile the record's schema with the schema the AvroSerializationSchema was created with (they must match).
  2. Recreate AvroSerializationSchema (or rebuild it via its factory) whenever the schema evolves; the datum writer is lazily cached per schema.
  3. For SpecificRecord, ensure the class on the classpath of the task matches the class used to build records at runtime.

Example fix

// before
GenericRecord rec = new GenericData.Record(schemaV2);
... serializer.serialize(rec); // serializer built with schemaV1

// after
GenericRecord rec = new GenericData.Record(schemaV1); // or rebuild serializer with schemaV2
... serializer.serialize(rec);
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure record conforms to the writer schema before serializing
if (record instanceof IndexedRecord
        && !((IndexedRecord) record).getSchema().equals(expectedWriterSchema)) {
    throw new IllegalArgumentException("Record schema != serializer schema");
}

Try / catch

try {
    bytes = serializer.serialize(record);
} catch (WrappingRuntimeException e) { // message: Failed to serialize schema registry.
    log.error("Avro encode failed for schema={} cause={}",
        record.getSchema(), e.getCause().getMessage());
    throw e; // data error: do not silently drop
}

Prevention

When it happens

Trigger: AvroSerializationSchema.forSpecific/forGeneric(...).serialize(record) where the record does not conform to the writer schema (missing fields, wrong field type), or the schema changed between serialization calls without re-initialization.

Common situations: Passing a GenericRecord built with schema v2 while the schema object pinned at construction is v1; reusing a serialized schema object across schema upgrades; records produced by different producers.

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/90b75aac7c95b776. Report an issue: GitHub.