pydantic/monty · error · TypeError

Object property 'type' type mismatch. Expect value to be Obj

Error message

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

What it means

pushClassInstance validates ClassInstance marker objects: the `type` property must be an object (the class-type marker describing the instance's class). This TypeError is thrown when `type` is a primitive, array-of-wrong-shape, or null, mirroring napi's validation so both transports fail identically on malformed markers.

Source

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

    throw new TypeError(`Monty${typeName} timezoneName requires offsetSeconds`)
  }
  return aware
    ? {
        offsetSeconds: Number(object.offsetSeconds),
        ...(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 })

View on GitHub (pinned to adc986b362)

Solutions

  1. Provide the full nested class-type object as `type`: { marker: 'ClassType', name: 'MyClass', attrs: [...], ... } as produced by the runtime
  2. Wrap value in typeof checks before passing: if (value && typeof value.type === 'object' && value.type !== null)
  3. Pass values originally returned by the runtime rather than re-creating them

Example fix

// before
{ marker: 'ClassInstance', type: 'Point', attrs: [] }
// after
{ marker: 'ClassInstance', type: { marker: 'ClassType', name: 'Point', attrs: [] }, attrs: [] }
Defensive patterns

Strategy: type-guard

Validate before calling

function assertClassInstance(o: unknown): void {
  const m = o as Record<string, unknown>
  if (typeof m.type !== 'object' || m.type === null) throw new Error('ClassInstance.type must be an object')
  if (!Array.isArray(m.attrs)) throw new Error('ClassInstance.attrs must be an array')
}

Type guard

const isClassInstance = (v: unknown): v is { type: Record<string, unknown>; attrs: [string, unknown][] } =>
  typeof v === 'object' && v !== null && typeof (v as any).type === 'object' && (v as any).type !== null && Array.isArray((v as any).attrs)

Try / catch

try {
  return pushClassInstance(object, nodes)
} catch (e) {
  if (e instanceof TypeError && e.message.includes("property 'type' type mismatch")) {
    throw new Error('Malformed ClassInstance marker: nested class-type object required')
  }
  throw e
}

Prevention

When it happens

Trigger: Passing a ClassInstance marker whose `type` is a string name instead of the nested class-type object (e.g. { marker: 'ClassInstance', type: 'MyClass', attrs: [...] }); JSON transformations that flatten `type` to its name; hand-built class instance inputs.

Common situations: Manual construction of class instance values for session inputs; a serializer that replaced the nested type object with the class name; partial deserialization of values received from a different transport.

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/436ad20fd9b17ca5. Report an issue: GitHub.