pydantic/monty · error · TypeError

Object property 'attrs' type mismatch. Expect value to be Ar

Error message

Object property 'attrs' type mismatch. Expect value to be Array, but received ${jsType(object.attrs)}

What it means

pushClassInstance requires a ClassInstance marker's `attrs` property to be an array of [name, value] pairs. This TypeError is thrown when attrs is missing or is any non-array value, mirroring the native binding's contract so malformed markers fail on both transports.

Source

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

        ...(typeof object.timezoneName === 'string' ? { timezoneName: object.timezoneName } : {}),
      }
    : {}
}

/**
 * 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'),

View on GitHub (pinned to adc986b362)

Solutions

  1. Express attrs as an ordered array of [name, value] pairs: [['x', 1], ['y', 2]]
  2. Pre-check Array.isArray(marker.attrs) before passing the value
  3. Keep values in their original runtime-produced shape instead of transforming attribute maps

Example fix

// before
{ marker: 'ClassInstance', type: t, attrs: { x: 1, y: 2 } }
// after
{ marker: 'ClassInstance', type: t, attrs: [['x', 1], ['y', 2]] }
Defensive patterns

Strategy: type-guard

Validate before calling

function assertAttrsArray(o: Record<string, unknown>): void {
  if (!Array.isArray(o.attrs)) throw new Error('attrs must be an array of [name, value] pairs')
}

Type guard

const hasPairArray = (o: Record<string, unknown>): o is { attrs: unknown[] } => Array.isArray(o.attrs)

Try / catch

try {
  return pushClassInstance(object, nodes)
} catch (e) {
  if (e instanceof TypeError && e.message.includes("property 'attrs' type mismatch")) {
    if (object.attrs && typeof object.attrs === 'object') {
      object.attrs = Object.entries(object.attrs as Record<string, unknown>) // repair object-map form
      return pushClassInstance(object, nodes)
    }
  }
  throw e
}

Prevention

When it happens

Trigger: Passing { marker: 'ClassInstance', type: {...} } with no attrs key; attrs given as an object map ({x: 1}) instead of an array of pairs; JSON input where attrs was serialized to a dictionary.

Common situations: Hand-building class instance values and using a JS object for attributes instead of ordered pairs; round-tripping values through a format that converts arrays to keyed objects.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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