> ## Content Index
> Fetch the complete content index at: https://datajutsu.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# An Avro Union Is an Index Into the Schema (and the Reordered Record Swaps Its Values Silently)
- URL: https://datajutsu.com/avro-union-branch-index/
- Published: 2026-10-23T21:01:24.000Z
- Updated: 2026-10-23T21:01:24.000Z
- Author: Data Jutsu

I put a nullable field in an Avro record, a \[null, string\] union, and wrote it both ways. With the first field null and the second set to hi, the bytes come out 0002046869, and the first byte is not a value, it is an index into the branch list, 00 for the null branch, and the next field writes 02, which is branch index 1 zigzagged, meaning the string branch, before the actual payload. Flip the schema to \[string, null\] and the same record writes 02 for the null and 00046869 for the hi. Same data, different bytes, no error in either direction. The position of a branch in the schema list is its only identity, and if a newer version of the schema moves a branch, every byte an older writer produced now points at a different branch, and a reader that only knows the new schema follows it happily. The branch index is the one self-describing thing in an Avro stream. The payload is along for the ride.

The index is a zigzag varint, so it is one byte for the small schemas and grows when the list grows: a union with 101 branches writes index 100 as c8 01, two bytes, and decodes it fine. The decoder checks exactly one thing about it: it compares the index against its own branch count. An index of 101 into a 101-branch schema throws invalid union index, and a null byte into a schema that has no null branch throws the same way, naming the list. So the index can be out of range, but only out of range against the reader's schema. Whether the branch it points at is the one the writer meant is exactly what nobody checks, because the writer's list is not in the bytes of the stream, it is in a schema file that the reader may or may not have been given.

The write side in the JavaScript library is strict about one thing: it will not guess your branch. A union field declared \[int, long\] handed a plain 42 throws invalid, because both branches accept that number and the library refuses to pick one for you, and two anonymous string branches in the same union are rejected when the schema compiles, with a message that names the duplicate branch. You write by branch name, and on the read side the value comes back wrapped in the branch, an object whose only job is to tell you which branch the bytes claimed. That wrapper is the entire record of the writer's decision. Strip it off, or decode the bytes with a hand-rolled reader, and what is left is a number and a payload, indistinguishable in shape from any other field in the stream.

An enum is the same idea with names on the list: a zigzag index into the symbol array, so RED writes 00, GREEN writes 02, BLUE writes 04\. The JavaScript library turns out to be closed on both ends, which is the strictest enum behavior I have measured in either wire format. Writing the name PURPLE throws, naming the symbol and the list it is not in. Decoding index 5 throws invalid Color enum index, and index 3, just one past the last symbol, throws the same error. The protobuf side is looser in both directions: its JavaScript decoder keeps an unknown enum value as a raw number, and its JavaScript encoder silently drops an unknown name while the C++ compiler rejects it with the value named. A fixed is the other end of the spectrum, raw bytes with no length prefix, the size living in the schema and nowhere in the bytes. A size-4 fixed handed 3 or 5 bytes both throw, and inside a union it is just its bytes after the branch index, still unambiguous because the size was declared up front.

The field order finding is the one that would make me nervous. Avro fields have no numbers; they sit at the position the schema declares them, and I proved it in both directions. A schema that declares b before a, handed the object with a=9 and b=7, wrote 0e12, b's 7 first and a's 9 after, because the schema said so and not because of the key order in the object. Decode those same bytes under a schema that declares a first and you get a=7, b=9\. No error, both values present, swapped. In protobuf this class of break is the one a diff tool reports as breaking, and in Avro it is the default state. The only thing standing between you and it is the reader-writer resolver, the feature that matches fields by name across two schemas, and it only runs when the reader is handed the writer's schema alongside its own. A decoder that only has its own schema does the swap and tells you nothing.

Bytes and string are the same on the wire, hi is 046869 under either, and the library does not enforce UTF-8 on a string field: the raw byte ff comes back as a replacement character, no error, while the same byte under a bytes field comes back as a raw buffer with its 0xff intact. Arrays and maps share one framing, a zigzag count, the items, and a zero terminator. A negative count means more blocks are coming, so 04 02 04 02 06 00 is the list 1, 2, 3 spread across two blocks, and a map is literally an array of key-value pairs in that same framing, {a: 1, b: 2} is 04 0261 02 0262 04 00\. Every one of these facts sits on the byte side of a schema, which is the whole point of the file format: it ships the schema in the header so the reader never has to guess, and everything I measured here is what happens when you read a stream where that guarantee is not in force. The tool on the protobuf side of this, at [proto.datajutsu.com](https://proto.datajutsu.com/?ref=datajutsu.com), is where I measure every byte before I write it down, and the float and integer comparison for the same record is in the [avro versus protobuf post](https://datajutsu.com/avro-vs-protobuf-bytes/).