{"record":{"id":"8ae45f48fbee69ea","repo":"apache/pulsar","slug":"autoconsumeschema-is-not-intended-to-be-used-for-e","errorCode":null,"errorMessage":"AutoConsumeSchema is not intended to be used for encoding","messagePattern":"AutoConsumeSchema is not intended 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/AutoConsumeSchema.java","lineNumber":102,"sourceCode":"                \"Schema version \" + schemaVersion + \" is not initialized before used\");\n    }\n\n    @Override\n    public void validate(byte[] message) {\n        ensureSchemaInitialized(SchemaVersion.Latest);\n\n        schemaMap.get(SchemaVersion.Latest).validate(message);\n    }\n\n    public void validate(byte[] message, byte[] schemaVersion) {\n        SchemaVersion sv = getSchemaVersion(schemaVersion);\n        ensureSchemaInitialized(sv);\n        schemaMap.get(sv).validate(message);\n    }\n\n    @Override\n    public byte[] encode(GenericRecord message) {\n        throw new UnsupportedOperationException(\"AutoConsumeSchema is not intended to be used for encoding\");\n    }\n\n    @Override\n    public boolean supportSchemaVersioning() {\n        return true;\n    }\n\n    public Schema<?> atSchemaVersion(byte[] schemaVersion) {\n        SchemaVersion sv = getSchemaVersion(schemaVersion);\n        fetchSchemaIfNeeded(sv);\n        ensureSchemaInitialized(sv);\n        Schema<?> topicVersionedSchema = schemaMap.get(sv);\n        if (topicVersionedSchema.supportSchemaVersioning() && topicVersionedSchema instanceof AbstractSchema) {\n            return ((AbstractSchema<?>) topicVersionedSchema).atSchemaVersion(schemaVersion);\n        } else {\n            return topicVersionedSchema;\n        }\n    }","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/AutoConsumeSchema.java#L84-L120","documentation":"AutoConsumeSchema dynamically resolves the writer schema of incoming messages from the broker, so it only knows how to decode into GenericRecords. Its encode() method is hard-wired to throw UnsupportedOperationException because producing with AutoConsumeSchema is meaningless — there is no fixed schema to encode against.","triggerScenarios":"Calling encode() on a schema instance of type AutoConsumeSchema, typically obtained from consumer.getSchema() on a consumer created with Schema.AUTO_CONSUME().","commonSituations":"Generic pipelines that read with AUTO_CONSUME and later try to republish messages using the consumer's schema; helper utilities that call Schema.encode() on any Schema without checking direction.","solutions":["Create an explicit producer schema (typed or Schema.generic(schemaInfo)) for publishing","Use AUTO_PRODUCE() on the producer side if you want to forward messages while preserving their original schemas","Check schema.getSchemaInfo().getType() / instanceof AutoConsumeSchema before calling encode()"],"exampleFix":"// before\nProducer<GenericRecord> p = client.newProducer().schema(consumer.getSchema()) // AutoConsumeSchema\n    .create();\np.newMessage().value(record).send(); // encode throws\n// after\nProducer<GenericRecord> p = client.newProducer()\n    .schema(Schema.generic(consumer.getSchemaInfo())).create();","handlingStrategy":"validation","validationCode":"if (schema instanceof AutoConsumeSchema) {\n    throw new IllegalStateException(\"AutoConsumeSchema cannot encode; use Schema.AUTO_PRODUCE or a typed schema\");\n}","typeGuard":"boolean isAutoConsume(Schema<?> s) {\n    return s instanceof AutoConsumeSchema;\n}","tryCatchPattern":"try {\n    producer.send(record);\n} catch (UnsupportedOperationException e) {\n    // rebuild producer with Schema.generic(schemaInfo) or Schema.AUTO_PRODUCE()\n}","preventionTips":["Use Schema.AUTO_PRODUCE() on forwarding producers instead of AUTO_CONSUME schemas","Do not pass consumer.getSchema() into producer builders","Guard generic encode paths with instanceof AutoConsumeSchema checks"],"tags":["pulsar","schema","auto-consume","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-14T00:17:10.932Z"}