{"record":{"id":"1805c25f704a08af","repo":"danny-avila/LibreChat","slug":"path-must-contain-only-finite-numbers","errorCode":null,"errorMessage":"${path} must contain only finite numbers","messagePattern":"(.+?) must contain only finite numbers","errorType":"validation","errorClass":"AgentRunEnvelopeError","httpStatus":null,"severity":"error","filePath":"packages/api/src/agents/envelope.ts","lineNumber":113,"sourceCode":"function cloneJsonValue(\n  value: unknown,\n  path: string,\n  ancestors: WeakSet<object>,\n  depth: number,\n): unknown {\n  if (depth > AGENT_RUN_ENVELOPE_MAX_NESTING_DEPTH) {\n    throw new AgentRunEnvelopeError(\n      `${path} exceeds the maximum nesting depth of ${AGENT_RUN_ENVELOPE_MAX_NESTING_DEPTH}`,\n    );\n  }\n\n  if (value === null || typeof value === 'string' || typeof value === 'boolean') {\n    return value;\n  }\n\n  if (typeof value === 'number') {\n    if (!Number.isFinite(value)) {\n      throw new AgentRunEnvelopeError(`${path} must contain only finite numbers`);\n    }\n    return value;\n  }\n\n  if (typeof value !== 'object') {\n    throw new AgentRunEnvelopeError(`${path} contains a non-JSON ${typeof value} value`);\n  }\n\n  if (ancestors.has(value)) {\n    throw new AgentRunEnvelopeError(`${path} contains a circular reference`);\n  }\n\n  ancestors.add(value);\n\n  try {\n    const symbolKeys = Object.getOwnPropertySymbols(value);\n    if (symbolKeys.length > 0) {\n      throw new AgentRunEnvelopeError(`${path} contains symbol keys`);","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/danny-avila/LibreChat/blob/5ff282f9006c436e561de1afd39a481bea1ef0d8/packages/api/src/agents/envelope.ts#L95-L131","documentation":"cloneJsonValue rejects any number that is not finite — NaN, Infinity, or -Infinity — with AgentRunEnvelopeError. These values are not valid JSON and would break downstream JSON serialization across the agent execution seam, so they are caught at envelope construction.","triggerScenarios":"A payload containing a numeric field set to NaN (e.g. from a failed parseFloat or a 0/0 division), Infinity (e.g. from division by zero), or -Infinity; statistics or token-count fields computed from malformed inputs.","commonSituations":"A tool result with NaN in a numeric statistic; token usage math producing Infinity when dividing by a zero count; deserializing user input with parseFloat that returned NaN; floating-point overflow producing Infinity.","solutions":["Sanitize numeric fields before placing them in the payload: replace NaN/Infinity with null, 0, or a clamped value.","Validate with Number.isFinite at the tool-output boundary.","Trace which field caused it — the error path (e.g. 'payload.messages[3].token_count') is included in the message."],"exampleFix":"// before\nconst usage = { prompt_tokens: tokens, ratio: tokens / total }; // total may be 0 -> Infinity\n\n// after\nconst ratio = Number.isFinite(tokens / total) ? tokens / total : null;\nconst usage = { prompt_tokens: tokens, ratio };","handlingStrategy":"validation","validationCode":"function sanitizeNumbers(v: unknown): unknown {\n  if (typeof v === 'number') return Number.isFinite(v) ? v : null;\n  if (Array.isArray(v)) return v.map(sanitizeNumbers);\n  if (v && typeof v === 'object') return Object.fromEntries(Object.entries(v).map(([k, x]) => [k, sanitizeNumbers(x)]));\n  return v;\n}\nconst safePayload = sanitizeNumbers(payload);","typeGuard":"const isFiniteNumber = (v: unknown): v is number =>\n  typeof v === 'number' && Number.isFinite(v);","tryCatchPattern":"import { AgentRunEnvelopeError } from '~/agents/envelope';\ntry {\n  envelope = createAgentRunEnvelope(input);\n} catch (error) {\n  if (error instanceof AgentRunEnvelopeError && /finite numbers/.test(error.message)) {\n    input.payload = sanitizeNumbers(input.payload) as typeof input.payload;\n    envelope = createAgentRunEnvelope(input);\n  } else {\n    throw error;\n  }\n}","preventionTips":["Replace NaN/Infinity with null or a clamped value before serialization.","Validate tool outputs with Number.isFinite before they enter the payload.","The error path names the offending field — use it to locate the source."],"tags":["agents","envelope","validation","json","numeric"],"backgroundTag":null,"analyzedSha":"5ff282f9006c436e561de1afd39a481bea1ef0d8","analyzedAt":"2026-08-12T21:38:08.145Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}