JuliusBrussee/caveman · error

value is not JSON-serializable

Error message

value is not JSON-serializable

What it means

Thrown by canonicalJSON when JSON.stringify returns undefined, meaning the value contains something JSON cannot represent (a bare function, undefined, Symbol, or a BigInt at top level). Assembly headers and hashes need exact canonical bytes, so serialization failure is fatal rather than coerced.

Solutions

  1. Pass only plain JSON data (strings, numbers, booleans, null, arrays, objects) as slot/prefix content
  2. Convert BigInt values to strings before serializing
  3. Strip undefined-valued properties or function references from the payload
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at packages/sdk/typescript/src/index.ts:1952 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/59d5bd171cc58c6e. Report an issue: GitHub.

Appendix: source

Thrown at packages/sdk/typescript/src/index.ts:1952

  const prefixHash = sha256Hex(prefixBytes);
  const assemblyHeader = `v1;slots=${ordered.length};prefix=${prefixHash.slice(0, 12)};vbb=1`;
  assemblyRequestHeaders.set(request, assemblyHeader);
  return {
    request,
    headers: { "x-cave-assembly": assemblyHeader },
    prefixHash,
    breakpoints,
    stableTokens: Math.ceil(new TextEncoder().encode(prefixBytes).length / 4),
    tokenBasis: "estimated_bytes_div_4",
    basis: "inferred",
    volatileBelowBreakpoint: true
  };
}

function canonicalJSON(value: unknown): string {
  const raw = JSON.stringify(value);
  if (raw === undefined) throw new Error("value is not JSON-serializable");
  return stableStringify(JSON.parse(raw));
}

function cloneCanonical(value: unknown): unknown {
  return JSON.parse(canonicalJSON(value)) as unknown;
}

function assemblyText(value: unknown): string {
  return typeof value === "string" ? value : canonicalJSON(value);
}

async function cavePlan(cave: Cave): Promise<CavePlan> {
  // `/sdk/v1/cave-plan` is a gateway-owned project-key route. `controlURL` is
  // reserved for control-api `/api/v1/*` surfaces and must never redirect this
  // call to a route that service does not expose.
  const response = await caveFetch(cave, `${cave.options.baseURL}/sdk/v1/cave-plan`, {
    method: "GET",
    headers: otlpHeaders(cave)

View on GitHub (pinned to 3ee70a1026)