pydantic/monty · error · TypeError

ClassInstance attrs entries must be [name, value] pairs

Error message

ClassInstance attrs entries must be [name, value] pairs

What it means

A validation guard in the wasm-worker value converter's `pushClassInstance`, which turns a host-supplied `ClassInstance` marker into flat value nodes. It fires when an entry of the marker's `attrs` array is not a `[name, value]` pair (wrong length or not an array). The marker shape must mirror what the napi path produces — ordered `[name, value]` pairs — so malformed host input is rejected here with a TypeError before it reaches the interpreter.

Source

Thrown at crates/monty-js/ts/worker/value.ts:189

 * Validates and converts a host `ClassInstance` marker (same shape the napi
 * path produces: `attrs` as ordered `[name, value]` pairs, uuids as
 * strings). Validation messages mirror napi's so both transports fail
 * malformed markers alike.
 */
function pushClassInstance(object: Record<string, unknown>, nodes: ValueNode[]): ValueNode {
  if (typeof object.type !== 'object' || object.type === null) {
    throw new TypeError(
      `Object property 'type' type mismatch. Expect value to be Object, but received ${jsType(object.type)}`,
    )
  }
  if (!Array.isArray(object.attrs)) {
    throw new TypeError(
      `Object property 'attrs' type mismatch. Expect value to be Array, but received ${jsType(object.attrs)}`,
    )
  }
  const pairs: [unknown, unknown][] = []
  for (const pair of object.attrs as unknown[]) {
    if (!Array.isArray(pair)) throw new TypeError('ClassInstance attrs entries must be [name, value] pairs')
    if (typeof pair[0] !== 'string') throw new TypeError('ClassInstance attr name must be a string')
    if (!(1 in pair)) throw new TypeError('ClassInstance attr value missing')
    pairs.push([pair[0], pair[1]])
  }
  const classTypeNode = pushClassType(object.type as Record<string, unknown>, nodes)
  const classTypeIndex = nodes.length
  nodes.push({ tag: 'class-type', val: classTypeNode })
  return {
    tag: 'class-instance',
    val: {
      classType: classTypeIndex,
      instanceId: uuidString(object.instanceId, 'ClassInstance instanceId'),
      attrs: pushPairs(pairs, nodes),
    },
  }
}

/** Builds a class-type node from the plain `classType` marker object,

View on GitHub (pinned to adc986b362)

Solutions

  1. Ensure every attrs element is exactly a two-element array [stringName, value]
  2. Use Object.entries(obj) directly, which already yields [key, value] arrays
  3. Validate with attrs.every(Array.isArray) before passing

Example fix

// before
attrs: [{ name: 'x', value: 1 }]
// after
attrs: [['x', 1]]
Defensive patterns

Strategy: validation

Validate before calling

const validPairs = (attrs: unknown[]): boolean => attrs.every(a => Array.isArray(a) && a.length === 2)

Type guard

const isPair = (p: unknown): p is [string, unknown] => Array.isArray(p) && p.length === 2

Try / catch

try {
  return pushClassInstance(object, nodes)
} catch (e) {
  if (e instanceof TypeError && e.message === 'ClassInstance attrs entries must be [name, value] pairs') {
    // find offending entry for diagnostics, then rethrow with index
  }
  throw e
}

Prevention

When it happens

Trigger: attrs containing entries like {name: 'x', value: 1} objects instead of ['x', 1] arrays; attrs containing stray non-pair elements after manual splicing or filtering.

Common situations: Mapping attribute dictionaries entry-by-entry (Object.entries output must be used directly, but hand-rolled transforms may produce objects); copying attrs from another serialization format with different pair encoding.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of pydantic/monty@adc986b362 (2026-09-13). Data as JSON: /api/errors/95f8b600ebaa720b. Report an issue: GitHub.