apache/pulsar · error · UnsupportedOperationException

AutoConsumeSchema is not intended to be used for encoding

Error message

AutoConsumeSchema is not intended to be used for encoding

What it means

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.

Source

Thrown at pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/AutoConsumeSchema.java:102

                "Schema version " + schemaVersion + " is not initialized before used");
    }

    @Override
    public void validate(byte[] message) {
        ensureSchemaInitialized(SchemaVersion.Latest);

        schemaMap.get(SchemaVersion.Latest).validate(message);
    }

    public void validate(byte[] message, byte[] schemaVersion) {
        SchemaVersion sv = getSchemaVersion(schemaVersion);
        ensureSchemaInitialized(sv);
        schemaMap.get(sv).validate(message);
    }

    @Override
    public byte[] encode(GenericRecord message) {
        throw new UnsupportedOperationException("AutoConsumeSchema is not intended to be used for encoding");
    }

    @Override
    public boolean supportSchemaVersioning() {
        return true;
    }

    public Schema<?> atSchemaVersion(byte[] schemaVersion) {
        SchemaVersion sv = getSchemaVersion(schemaVersion);
        fetchSchemaIfNeeded(sv);
        ensureSchemaInitialized(sv);
        Schema<?> topicVersionedSchema = schemaMap.get(sv);
        if (topicVersionedSchema.supportSchemaVersioning() && topicVersionedSchema instanceof AbstractSchema) {
            return ((AbstractSchema<?>) topicVersionedSchema).atSchemaVersion(schemaVersion);
        } else {
            return topicVersionedSchema;
        }
    }

View on GitHub (pinned to 820761864e)

Solutions

  1. Create an explicit producer schema (typed or Schema.generic(schemaInfo)) for publishing
  2. Use AUTO_PRODUCE() on the producer side if you want to forward messages while preserving their original schemas
  3. Check schema.getSchemaInfo().getType() / instanceof AutoConsumeSchema before calling encode()

Example fix

// before
Producer<GenericRecord> p = client.newProducer().schema(consumer.getSchema()) // AutoConsumeSchema
    .create();
p.newMessage().value(record).send(); // encode throws
// after
Producer<GenericRecord> p = client.newProducer()
    .schema(Schema.generic(consumer.getSchemaInfo())).create();
Defensive patterns

Strategy: validation

Validate before calling

if (schema instanceof AutoConsumeSchema) {
    throw new IllegalStateException("AutoConsumeSchema cannot encode; use Schema.AUTO_PRODUCE or a typed schema");
}

Type guard

boolean isAutoConsume(Schema<?> s) {
    return s instanceof AutoConsumeSchema;
}

Try / catch

try {
    producer.send(record);
} catch (UnsupportedOperationException e) {
    // rebuild producer with Schema.generic(schemaInfo) or Schema.AUTO_PRODUCE()
}

Prevention

When it happens

Trigger: Calling encode() on a schema instance of type AutoConsumeSchema, typically obtained from consumer.getSchema() on a consumer created with Schema.AUTO_CONSUME().

Common situations: 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.

Related errors


AI-assisted analysis of apache/pulsar@820761864e (2026-09-06). Data as JSON: /api/errors/8ae45f48fbee69ea. Report an issue: GitHub.