{"record":{"id":"137e385eab71408e","repo":"apache/flink","slug":"unable-to-serialize-record","errorCode":null,"errorMessage":"Unable to serialize record","messagePattern":"Unable to serialize record","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/common/serialization/TypeInformationSerializationSchema.java","lineNumber":119,"sourceCode":"     *\n     * @param nextElement The element to test for the end-of-stream signal.\n     * @return Returns false.\n     */\n    @Override\n    public boolean isEndOfStream(T nextElement) {\n        return false;\n    }\n\n    @Override\n    public byte[] serialize(T element) {\n        if (dos == null) {\n            dos = new DataOutputSerializer(16);\n        }\n\n        try {\n            serializer.serialize(element, dos);\n        } catch (IOException e) {\n            throw new RuntimeException(\"Unable to serialize record\", e);\n        }\n\n        byte[] ret = dos.getCopyOfBuffer();\n        dos.clear();\n        return ret;\n    }\n\n    @Override\n    public TypeInformation<T> getProducedType() {\n        return typeInfo;\n    }\n}\n","sourceCodeStart":101,"sourceCodeEnd":132,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/common/serialization/TypeInformationSerializationSchema.java#L101-L132","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the element type matches the TypeInformation passed to the schema constructor.","If using POJOs, verify all fields are serializable and match the POJO serializer's expectations.","Construct the schema with an explicit, correct TypeInformation to avoid type-erasure-induced serializer mismatches."],"exampleFix":"// before: type mismatch -> serialize fails\nTypeInformationSerializationSchema<String> schema =\n    new TypeInformationSerializationSchema<>(Types.STRING, ec);\nbyte[] out = schema.serialize(123); // Integer != String -> IOException\n\n// after: pass correct type\nbyte[] out = schema.serialize(\"hello\"); // String matches Types.STRING","handlingStrategy":"validation","validationCode":"// Validate element type matches the schema's produced type before serializing\nTypeInformation<?> produced = schema.getProducedType();\nif (!produced.getTypeClass().isInstance(element)) {\n    throw new IllegalStateException(\n        \"Element type \" + (element == null ? \"null\" : element.getClass())\n        + \" does not match \" + produced);\n}","typeGuard":"public static <T> boolean typeMatches(TypeInformation<T> info, T element) {\n    return element != null && info.getTypeClass().isInstance(element);\n}","tryCatchPattern":"try {\n    byte[] out = schema.serialize(element);\n} catch (RuntimeException e) {\n    if (e.getCause() instanceof IOException) {\n        // log and handle serialization failure\n    } else {\n        throw e;\n    }\n}","preventionTips":["Construct the schema with an explicit, correct TypeInformation matching the element type.","Unit-test serialization round-trips for each type you serialize.","Avoid passing null for non-nullable fields."],"tags":["serialization","io","type-mismatch","schema"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}