lutzroeder/netron · error · Error
Expected 'key'.
Error message
Expected 'key'.
What it means
This is the binary decoder for caffe2.MapFieldEntry (a map<string,string> entry) rejecting input where the required 'key' field is absent. Protobuf map entries encode key and val as ordinary fields, and the generated verifier requires both to be present in the wire data.
Source
Thrown at source/caffe2-proto.js:1070
static decode(reader, length) {
const message = new caffe2.MapFieldEntry();
const end = length === undefined ? reader.length : reader.position + length;
while (reader.position < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.key = reader.string();
break;
case 2:
message.val = reader.string();
break;
default:
reader.skipType(tag & 7);
break;
}
}
if (!Object.prototype.hasOwnProperty.call(message, 'key')) {
throw new Error("Expected 'key'.");
}
if (!Object.prototype.hasOwnProperty.call(message, 'val')) {
throw new Error("Expected 'val'.");
}
return message;
}
static decodeText(reader) {
const message = new caffe2.MapFieldEntry();
reader.start();
while (!reader.end()) {
const tag = reader.tag();
switch (tag) {
case "key":
message.key = reader.string();
break;
case "val":
message.val = reader.string();View on GitHub (pinned to d8a543f5f8)
Solutions
- Verify the byte slice boundaries so the MapFieldEntry starts at the correct offset and includes the key field.
- Re-serialize the source map with a correct encoder so every entry has both key and val.
- If consuming JSON, use decodeText and ensure each entry object has a 'key' property.
- Add a length/field sanity check on the buffer before calling decode.
Example fix
// before
const entry = caffe2.MapFieldEntry.decode(buf.subarray(0, len)); // throws Expected 'key'
// after
const entry = caffe2.MapFieldEntry.decode(buf.subarray(start, start + entryLen));
if (typeof entry.key !== 'string') throw new TypeError('bad map entry'); Defensive patterns
Strategy: try-catch
Validate before calling
// Binary wire: check first byte is field 1 tag (0x0A) before decode
function looksLikeMapEntry(u8){return u8.length>0 && (u8[0]&7)===2 && (u8[0]>>3)===1;} Type guard
function isMapEntry(o){return !!o && typeof o.key==='string' && typeof o.val==='string';} Try / catch
try { entry = caffe2.MapFieldEntry.decode(bytes); } catch (e) { if (/Expected 'key'/.test(e.message)) { skipOrReencode(bytes); } else throw e; } Prevention
- Decode parent messages rather than slicing raw bytes.
- Use the library's own encode() to produce test fixtures.
- Assert buffer offsets/lengths before sub-decoding.
When it happens
Trigger: caffe2.MapFieldEntry.decode(reader) on bytes that contain only a val field (or neither), e.g. hand-crafted wire bytes, buffers sliced at the wrong offset, or a map entry serialized by a producer that omits keys.
Common situations: Parsing caffe2 operator args or device options stored as maps; misaligned buffer slicing when extracting sub-messages from a larger blob; feeding malformed fixtures into tests.
Related errors
AI-assisted analysis of lutzroeder/netron@d8a543f5f8 (2026-08-27).
Data as JSON: /api/errors/dd75d22b6dbe0dc0.
Report an issue: GitHub.