pydantic/monty · error · TypeError

ClassType attr value missing

Error message

ClassType attr value missing

What it means

Each ClassType attrs entry must have both a name (index 0) and a value (index 1). This TypeError is thrown when the value element is missing entirely — the check is `1 in pair`, so `["name"]` or a one-element pair fails, while an explicitly present `undefined` value at index 1 is accepted. It prevents silently encoding attributes with no value.

Source

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

}

/** Builds a class-type node from the plain `classType` marker object,
 *  appending its eager attr nodes to the arena. */
function pushClassType(
  object: Record<string, unknown>,
  nodes: ValueNode[],
): Extract<ValueNode, { tag: 'class-type' }>['val'] {
  // Require an array like the native binding does, so both transports
  // enforce the same marker contract (a missing `attrs` is a forged or
  // malformed marker, not an empty attribute list).
  if (!Array.isArray(object.attrs)) {
    throw new TypeError('ClassType attrs must be an array of [name, value] pairs')
  }
  const attrPairs: [unknown, unknown][] = []
  for (const pair of object.attrs as unknown[]) {
    if (!Array.isArray(pair)) throw new TypeError('ClassType attrs entries must be [name, value] pairs')
    if (typeof pair[0] !== 'string') throw new TypeError('ClassType attr name must be a string')
    if (!(1 in pair)) throw new TypeError('ClassType attr value missing')
    attrPairs.push([pair[0], pair[1]])
  }
  return {
    name: String(object.name),
    id: uuidString(object.id, 'ClassType id'),
    hostDefined: object.hostDefined === true,
    isDataclass: object.isDataclass === true,
    attrs: pushPairs(attrPairs, nodes),
  }
}

/** A canonical uuid string is required for identities crossing the wire. */
function uuidString(value: unknown, what: string): string {
  if (
    typeof value !== 'string' ||
    !/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(value)
  ) {
    throw new TypeError(`${what} must be a canonical uuid string`)

View on GitHub (pinned to adc986b362)

Solutions

  1. Provide the value explicitly: `['x', someValue]`; if the value should be undefined, still write the element.
  2. Build pairs with `Object.entries(obj)` rather than `Object.keys(obj)`.
  3. If serializing markers through JSON, replace undefined values with null before stringify, since JSON drops them.
  4. Validate with `attrs.every(p => 1 in p)` before the call.

Example fix

// before
attrs: [['x']]
// after
attrs: [['x', undefined]]  // or provide a real value: ['x', 0]
Defensive patterns

Strategy: validation

Validate before calling

const missing = marker.attrs.filter(p => !Array.isArray(p) || !(1 in p));
if (missing.length) throw new TypeError('every attr pair needs index 1 (value)');

Type guard

const isCompletePair = (p: unknown): p is [string, unknown] =>
  Array.isArray(p) && 1 in p;

Try / catch

try {
  await session.feedRun(code, { inputs: { cls: marker } });
} catch (e) {
  if (e instanceof TypeError && e.message === 'ClassType attr value missing') {
    marker.attrs = marker.attrs.map(([n]) => [n, null]);
  } else throw e;
}

Prevention

When it happens

Trigger: Passing one-element pairs like `attrs: [['x']]`; building pairs with `Object.keys` instead of `Object.entries`; a sparse array `['x', ]` created with holes at index 1.

Common situations: Programmatic attrs construction that only collects names; refactors that dropped the value half of the pair; JSON round-trips that strip trailing undefined entries (JSON.stringify drops undefined array elements, turning `['x', undefined]` into `['x']`).

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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