{"record":{"id":"0264d869d9b72021","repo":"apache/flink","slug":"failed-to-create-avro-encoder","errorCode":null,"errorMessage":"Failed to create Avro encoder.","messagePattern":"Failed to create Avro encoder\\.","errorType":"exception","errorClass":"WrappingRuntimeException","httpStatus":null,"severity":"error","filePath":"flink-formats/flink-avro/src/main/java/org/apache/flink/formats/avro/AvroSerializationSchema.java","lineNumber":200,"sourceCode":"        }\n        ClassLoader cl = Thread.currentThread().getContextClassLoader();\n        if (SpecificRecord.class.isAssignableFrom(recordClazz)) {\n            Schema schema = SpecificData.get().getSchema(recordClazz);\n            this.datumWriter = new SpecificDatumWriter<>(schema);\n            this.schema = schema;\n        } else {\n            this.schema = new Schema.Parser().parse(this.schemaString);\n            GenericData genericData = new GenericData(cl);\n\n            this.datumWriter = new GenericDatumWriter<>(schema, genericData);\n        }\n\n        this.arrayOutputStream = new ByteArrayOutputStream();\n        if (encoding == AvroEncoding.JSON) {\n            try {\n                this.encoder = EncoderFactory.get().jsonEncoder(this.schema, arrayOutputStream);\n            } catch (IOException e) {\n                throw new WrappingRuntimeException(\"Failed to create Avro encoder.\", e);\n            }\n        } else {\n            this.encoder = EncoderFactory.get().directBinaryEncoder(arrayOutputStream, null);\n        }\n    }\n\n    @Override\n    public boolean equals(Object o) {\n        if (this == o) {\n            return true;\n        }\n        if (o == null || getClass() != o.getClass()) {\n            return false;\n        }\n        AvroSerializationSchema<?> that = (AvroSerializationSchema<?>) o;\n        return recordClazz.equals(that.recordClazz) && Objects.equals(schema, that.schema);\n    }\n","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-formats/flink-avro/src/main/java/org/apache/flink/formats/avro/AvroSerializationSchema.java#L182-L218","documentation":"Thrown from AvroSerializationSchema.checkAvroInitialized when EncoderFactory.get().jsonEncoder(schema, out) raises IOException while constructing a JSON encoder for AvroEncoding.JSON. The schema was already parsed successfully (or the parse error surfaces separately); encoder creation fails for malformed schema structures that Avro's JsonEncoder rejects.","triggerScenarios":"Building AvroSerializationSchema with encoding=AvroEncoding.JSON and a schema string whose record/field structure is invalid for JSON encoding (e.g. unnamed nested types Avro cannot name in JSON output).","commonSituations":"Switching from binary to JSON encoding for debugging/inspection; hand-written .avsc with subtle structural errors that a lenient parser accepts but JsonEncoder rejects.","solutions":["Validate the schema independently: new Schema.Parser().setValidate(true).parse(schemaString) and EncoderFactory.get().jsonEncoder(schema, new ByteArrayOutputStream()) in a unit test before deploying.","Fix the .avsc (name the nested records properly, correct malformed unions).","If JSON output is optional, fall back to AvroEncoding.BINARY which uses directBinaryEncoder and does not hit this path."],"exampleFix":"// before\nnew AvroSerializationSchema<>(clazz, schemaString, AvroEncoding.JSON) // schema broken for JSON\n\n// after\n// pre-validate in a test:\nSchema s = new Schema.Parser().parse(schemaString);\nEncoderFactory.get().jsonEncoder(s, new ByteArrayOutputStream()); // throws early, fix schema here","handlingStrategy":"validation","validationCode":"Schema s = new Schema.Parser().parse(schemaString);\ntry {\n    EncoderFactory.get().jsonEncoder(s, new ByteArrayOutputStream());\n} catch (IOException e) {\n    throw new IllegalArgumentException(\"Schema unusable for JSON encoding: \" + e.getMessage(), e);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate schema + JSON encoder pairing in a unit test before job submission.","Lint .avsc files with avro-tools in CI."],"tags":["avro","flink","json-encoding","schema-validation"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}