{"record":{"id":"0be113305c8c28b4","repo":"apache/flink","slug":"writable-type-expected-but-was","errorCode":null,"errorMessage":"Writable type '{}' expected but was '{}'.","messagePattern":"Writable type '(.+?)' expected but was '(.+?)'\\.","errorType":"validation","errorClass":"InvalidTypesException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/java/typeutils/TypeExtractor.java","lineNumber":2503,"sourceCode":"            // try to load the writable type info\n\n            Class<?> writableTypeInfoClass =\n                    Class.forName(\n                            HADOOP_WRITABLE_TYPEINFO_CLASS,\n                            false,\n                            typeInfo.getClass().getClassLoader());\n\n            if (writableTypeInfoClass.isAssignableFrom(typeInfo.getClass())) {\n                // this is actually a writable type info\n                // check if the type is a writable\n                if (!(type instanceof Class && isHadoopWritable((Class<?>) type))) {\n                    throw new InvalidTypesException(HADOOP_WRITABLE_CLASS + \" type expected.\");\n                }\n\n                // check writable type contents\n                Class<?> clazz = (Class<?>) type;\n                if (typeInfo.getTypeClass() != clazz) {\n                    throw new InvalidTypesException(\n                            \"Writable type '\"\n                                    + typeInfo.getTypeClass().getCanonicalName()\n                                    + \"' expected but was '\"\n                                    + clazz.getCanonicalName()\n                                    + \"'.\");\n                }\n            }\n        } catch (ClassNotFoundException e) {\n            // class not present at all, so cannot be that type info\n            // ignore\n        }\n    }\n}\n","sourceCodeStart":2485,"sourceCodeEnd":2517,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/java/typeutils/TypeExtractor.java#L2485-L2517","documentation":"Thrown by TypeExtractor.validateIfWritable when a WritableTypeInfo is expected, the type is a valid Hadoop Writable, but the expected Writable type class differs from the actual class found by reflection. This indicates a mismatch between two specific Writable types.","triggerScenarios":"Called during validateIfWritable after confirming the type is a Writable, but typeInfo.getTypeClass() != clazz (the actual class). Occurs when the declared WritableTypeInfo references one Writable class but the reflected type argument resolves to a different Writable class.","commonSituations":"Declaring a WritableTypeInfo for IntWritable but the function returns LongWritable. Generic UDFs where different Writable types appear in declared vs actual positions. Using a custom Writable subclass where the parent Writable was expected. Refactoring Writable type usages without updating type hints.","solutions":["Align the declared WritableTypeInfo with the actual Writable class used at runtime.","Provide the correct explicit TypeInformation via .returns(WritableTypeInfo.of(ActualWritableClass.class)).","Check for accidental Writable type substitution (e.g., Text vs BytesWritable).","Update type hints after refactoring Writable types."],"exampleFix":"// before\n.returns(WritableTypeInfo.of(IntWritable.class)) // actual is LongWritable\n\n// after\n.returns(WritableTypeInfo.of(LongWritable.class))","handlingStrategy":"validation","validationCode":"// Check that the expected Writable class matches the actual\nClass<?> expectedWritable = typeInfo.getTypeClass();\nClass<?> actualWritable = (Class<?>) type;\nif (expectedWritable != actualWritable) {\n    // align WritableTypeInfo with actual class\n    ti = WritableTypeInfo.of(actualWritable);\n}","typeGuard":"static boolean writableClassesMatch(TypeInformation<?> expected, Class<?> actual) {\n    return expected.getTypeClass() == actual;\n}","tryCatchPattern":"try {\n    validateIfWritable(typeInfo, type);\n} catch (InvalidTypesException e) {\n    ti = WritableTypeInfo.of((Class<?>) type);\n}","preventionTips":["Align declared WritableTypeInfo with the actual Writable class.","Avoid substituting one Writable type for another.","Update type hints after refactoring Writable types."],"tags":["hadoop","writable","type-mismatch","typeinfo"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}