apache/flink · error · RuntimeException

Unable to serialize record

Error message

Unable to serialize record

What it means

Thrown by TypeInformationSerializationSchema.serialize when the underlying TypeSerializer.serialize throws an IOException. This occurs when the element cannot be serialized — typically because it is of a type incompatible with the configured TypeInformation, or contains fields the serializer cannot handle. The IOException is wrapped in a RuntimeException because the SerializationSchema.serialize contract returns byte[] without checked exceptions.

Source

Thrown at flink-core/src/main/java/org/apache/flink/api/common/serialization/TypeInformationSerializationSchema.java:119

     *
     * @param nextElement The element to test for the end-of-stream signal.
     * @return Returns false.
     */
    @Override
    public boolean isEndOfStream(T nextElement) {
        return false;
    }

    @Override
    public byte[] serialize(T element) {
        if (dos == null) {
            dos = new DataOutputSerializer(16);
        }

        try {
            serializer.serialize(element, dos);
        } catch (IOException e) {
            throw new RuntimeException("Unable to serialize record", e);
        }

        byte[] ret = dos.getCopyOfBuffer();
        dos.clear();
        return ret;
    }

    @Override
    public TypeInformation<T> getProducedType() {
        return typeInfo;
    }
}

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Ensure the element type matches the TypeInformation passed to the schema constructor.
  2. If using POJOs, verify all fields are serializable and match the POJO serializer's expectations.
  3. Construct the schema with an explicit, correct TypeInformation to avoid type-erasure-induced serializer mismatches.

Example fix

// before: type mismatch -> serialize fails
TypeInformationSerializationSchema<String> schema =
    new TypeInformationSerializationSchema<>(Types.STRING, ec);
byte[] out = schema.serialize(123); // Integer != String -> IOException

// after: pass correct type
byte[] out = schema.serialize("hello"); // String matches Types.STRING
Defensive patterns

Strategy: validation

Validate before calling

// Validate element type matches the schema's produced type before serializing
TypeInformation<?> produced = schema.getProducedType();
if (!produced.getTypeClass().isInstance(element)) {
    throw new IllegalStateException(
        "Element type " + (element == null ? "null" : element.getClass())
        + " does not match " + produced);
}

Type guard

public static <T> boolean typeMatches(TypeInformation<T> info, T element) {
    return element != null && info.getTypeClass().isInstance(element);
}

Try / catch

try {
    byte[] out = schema.serialize(element);
} catch (RuntimeException e) {
    if (e.getCause() instanceof IOException) {
        // log and handle serialization failure
    } else {
        throw e;
    }
}

Prevention

When it happens

Trigger: Passing an element whose runtime type does not match the TypeInformation<T> the schema was constructed with, or an element containing null fields in non-nullable positions. The serializer attempts to write and fails.

Common situations: Generic type erasure causing the wrong TypeSerializer to be selected; POJO fields changed without updating the serializer; passing null where a non-null type is expected; nested objects with incompatible serializers.

Related errors


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