apache/pulsar · error · SchemaSerializationException

Size of data received by ShortSchema is not 2

Error message

Size of data received by ShortSchema is not 2

What it means

Schema decode guard in ShortSchema.validate: a 16-bit (INT16) message must occupy exactly 2 bytes, but the received payload has a different length, so decode() aborts before reading the short; indicates a producer writing with an incompatible schema for the topic.

Source

Thrown at pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/ShortSchema.java:49

    private static final ShortSchema INSTANCE;
    private static final SchemaInfo SCHEMA_INFO;

    static {
        SCHEMA_INFO = SchemaInfoImpl.builder()
            .name("INT16")
            .type(SchemaType.INT16)
            .schema(new byte[0]).build();
        INSTANCE = new ShortSchema();
    }

    public static ShortSchema of() {
        return INSTANCE;
    }

    @Override
    public void validate(byte[] message) {
        if (message.length != 2) {
            throw new SchemaSerializationException("Size of data received by ShortSchema is not 2");
        }
    }

    @Override
    public void validate(ByteBuf message) {
        if (message.readableBytes() != 2) {
            throw new SchemaSerializationException("Size of data received by ShortSchema is not 2");
        }
    }

    @Override
    public byte[] encode(Short message) {
        if (null == message) {
            return null;
        } else {
            return new byte[] {
                (byte) (message >>> 8),
                message.byteValue()

View on GitHub (pinned to 820761864e)

Solutions

  1. Ensure the producer uses the INT16 schema so exactly 2 bytes are written
  2. Check for payload corruption or a wrong schema on the producing side

Example fix

// before
Short v = Schema.INT16.decode(bytes);
// after
if (bytes.length != 2) {
    throw new IllegalArgumentException("expected 2 bytes, got " + bytes.length);
}
Short v = Schema.INT16.decode(bytes);
Defensive patterns

Strategy: validation

Validate before calling

if (bytes == null || bytes.length != 2) { throw new IllegalArgumentException("ShortSchema payload must be 2 bytes"); }

Type guard

boolean isShortPayload(byte[] b) { return b != null && b.length == 2; }

Try / catch

try { Short v = Schema.INT16.decode(bytes); } catch (SchemaSerializationException e) { log.error("bad payload size", e); /* dead-letter */ }

Prevention

When it happens

Trigger: Calling ShortSchema.decode()/validate() on payloads that are not 2 bytes — e.g. data written with ByteSchema/IntSchema, corrupted or truncated entries.

Common situations: Producer/consumer schema mismatch on the topic; decoding raw bytes from an external tool; a changed topic schema after migration.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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