{"record":{"id":"c901584e431b79c2","repo":"apache/pulsar","slug":"this-schema-is-not-meant-to-be-used-for-encoding","errorCode":null,"errorMessage":"This schema is not meant to be used for encoding","messagePattern":"This schema is not meant to be used for encoding","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/AbstractStructSchema.java","lineNumber":165,"sourceCode":"\n        @Override\n        public boolean requireFetchingSchemaInfo() {\n            return true;\n        }\n\n        @Override\n        public T decode(byte[] bytes) {\n            return decode(bytes, schemaVersion);\n        }\n\n        @Override\n        public T decode(ByteBuf byteBuf) {\n            return decode(byteBuf, schemaVersion);\n        }\n\n        @Override\n        public byte[] encode(T message) {\n            throw new UnsupportedOperationException(\"This schema is not meant to be used for encoding\");\n        }\n\n        @Override\n        @SuppressWarnings(\"unchecked\")\n        public Optional<Object> getNativeSchema() {\n            if (reader instanceof AbstractMultiVersionReader) {\n                AbstractMultiVersionReader abstractMultiVersionReader = (AbstractMultiVersionReader) reader;\n                try {\n                    SchemaReader schemaReader = abstractMultiVersionReader.getSchemaReader(schemaVersion);\n                    return schemaReader.getNativeSchema();\n                } catch (ExecutionException err) {\n                    throw new RuntimeException(err.getCause());\n                }\n            } else {\n                return Optional.empty();\n            }\n        }\n","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/AbstractStructSchema.java#L147-L183","documentation":"AbstractStructSchema's encode() is deliberately unimplemented: struct schemas (Avro/Protobuf/JSON based, multi-version readers) are designed for decoding only on the consumer side. Calling encode() throws UnsupportedOperationException to signal that this schema instance cannot serialize messages back to bytes.","triggerScenarios":"Calling schema.encode(message) on an AbstractStructSchema instance (e.g. a schema obtained via AutoConsumeSchema, schema registry lookup, or reader-side multi-version schema) instead of using the original typed schema created for producing.","commonSituations":"Developers fetch a schema via Schema.AVRO(...) lookup from a broker or via AutoConsumeSchema on the consumer side and then try to reuse it on a producer to publish records; or generic code paths that assume every Schema supports encode().","solutions":["Use the original typed schema (e.g. Schema.AVRO(MyPojo.class)) on the producer instead of the consumer-side struct schema","If you need both directions, create a separate schema instance for encoding rather than reusing the decoding schema","If the value is a GenericRecord, use a schema built from its SchemaInfo (e.g. Schema.generic(schemaInfo)) that supports encoding"],"exampleFix":"// before\nSchema<MyPojo> schema = consumer.getSchema();\nbyte[] bytes = schema.encode(myPojo); // throws\n// after\nSchema<MyPojo> schema = Schema.AVRO(MyPojo.class);\nbyte[] bytes = schema.encode(myPojo);","handlingStrategy":"validation","validationCode":"if (schema instanceof AbstractStructSchema || !schemaSupportsEncode(schema)) {\n    throw new IllegalStateException(\"Schema cannot encode; use a producer-side typed schema\");\n}\nboolean schemaSupportsEncode(Schema<?> s) {\n    return !(s instanceof AutoConsumeSchema) && !(s.getClass().getSimpleName().contains(\"MultiVersion\"));\n}","typeGuard":"boolean isEncodeCapable(Schema<?> s) {\n    return s != null && !(s instanceof AutoConsumeSchema) && !(s instanceof AbstractStructSchema);\n}","tryCatchPattern":"try {\n    byte[] bytes = schema.encode(message);\n} catch (UnsupportedOperationException e) {\n    // fall back to a producer-side schema\n    bytes = Schema.AVRO(Message.class).encode(message);\n}","preventionTips":["Never reuse consumer-side schemas (AutoConsumeSchema, lookup-based struct schemas) for producing","Create dedicated producer schemas with Schema.AVRO/JSON/Protobuf(Class)","Check instanceof before calling encode in generic schema-agnostic code"],"tags":["pulsar","schema","unsupported-operation","encode"],"backgroundTag":"schema-not-encodable","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}