danny-avila/LibreChat · error · AgentRunEnvelopeError

${path} must contain only finite numbers

Error message

${path} must contain only finite numbers

What it means

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.

Source

Thrown at packages/api/src/agents/envelope.ts:113

function cloneJsonValue(
  value: unknown,
  path: string,
  ancestors: WeakSet<object>,
  depth: number,
): unknown {
  if (depth > AGENT_RUN_ENVELOPE_MAX_NESTING_DEPTH) {
    throw new AgentRunEnvelopeError(
      `${path} exceeds the maximum nesting depth of ${AGENT_RUN_ENVELOPE_MAX_NESTING_DEPTH}`,
    );
  }

  if (value === null || typeof value === 'string' || typeof value === 'boolean') {
    return value;
  }

  if (typeof value === 'number') {
    if (!Number.isFinite(value)) {
      throw new AgentRunEnvelopeError(`${path} must contain only finite numbers`);
    }
    return value;
  }

  if (typeof value !== 'object') {
    throw new AgentRunEnvelopeError(`${path} contains a non-JSON ${typeof value} value`);
  }

  if (ancestors.has(value)) {
    throw new AgentRunEnvelopeError(`${path} contains a circular reference`);
  }

  ancestors.add(value);

  try {
    const symbolKeys = Object.getOwnPropertySymbols(value);
    if (symbolKeys.length > 0) {
      throw new AgentRunEnvelopeError(`${path} contains symbol keys`);

View on GitHub (pinned to 5ff282f900)

Solutions

  1. Sanitize numeric fields before placing them in the payload: replace NaN/Infinity with null, 0, or a clamped value.
  2. Validate with Number.isFinite at the tool-output boundary.
  3. Trace which field caused it — the error path (e.g. 'payload.messages[3].token_count') is included in the message.

Example fix

// before
const usage = { prompt_tokens: tokens, ratio: tokens / total }; // total may be 0 -> Infinity

// after
const ratio = Number.isFinite(tokens / total) ? tokens / total : null;
const usage = { prompt_tokens: tokens, ratio };
Defensive patterns

Strategy: validation

Validate before calling

function sanitizeNumbers(v: unknown): unknown {
  if (typeof v === 'number') return Number.isFinite(v) ? v : null;
  if (Array.isArray(v)) return v.map(sanitizeNumbers);
  if (v && typeof v === 'object') return Object.fromEntries(Object.entries(v).map(([k, x]) => [k, sanitizeNumbers(x)]));
  return v;
}
const safePayload = sanitizeNumbers(payload);

Type guard

const isFiniteNumber = (v: unknown): v is number =>
  typeof v === 'number' && Number.isFinite(v);

Try / catch

import { AgentRunEnvelopeError } from '~/agents/envelope';
try {
  envelope = createAgentRunEnvelope(input);
} catch (error) {
  if (error instanceof AgentRunEnvelopeError && /finite numbers/.test(error.message)) {
    input.payload = sanitizeNumbers(input.payload) as typeof input.payload;
    envelope = createAgentRunEnvelope(input);
  } else {
    throw error;
  }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of danny-avila/LibreChat@5ff282f900 (2026-08-12). Data as JSON: /api/errors/1805c25f704a08af. Report an issue: GitHub.