ai icon indicating copy to clipboard operation
ai copied to clipboard

isJSONSerializable throws RangeError on circular references instead of returning false

Open nikolas-sapa opened this issue 1 month ago • 3 comments

Description

isJSONSerializable in @ai-sdk/provider-utils is a predicate type-guard designed to check whether a value can cross a serialization boundary.

When evaluated against circular structures (such as cyclical objects or arrays), isJSONSerializable enters infinite recursion across Object.values() or array items, throwing an unhandled RangeError: Maximum call stack size exceeded rather than returning false.

Reproduction

import { isJSONSerializable } from '@ai-sdk/provider-utils';

const circular: Record<string, unknown> = { name: 'test' };
circular.self = circular;

isJSONSerializable(circular);
// Throws: RangeError: Maximum call stack size exceeded
// Expected: returns false

Proposed Fix

Track active traversal ancestors using a Set<unknown> to detect cycles along the current branch while safely permitting valid DAGs (shared non-cyclic object references):

export function isJSONSerializable(value: unknown): value is JSONValue {
  return checkJSONSerializable(value, new Set());
}

function checkJSONSerializable(
  value: unknown,
  ancestors: Set<unknown>,
): boolean {
  if (value === null || value === undefined) return true;

  const type = typeof value;
  if (type === 'string' || type === 'number' || type === 'boolean') return true;
  if (type === 'function' || type === 'symbol' || type === 'bigint')
    return false;

  if (typeof value === 'object') {
    if (ancestors.has(value)) {
      return false;
    }
  }

  if (Array.isArray(value)) {
    ancestors.add(value);
    const result = value.every(item => checkJSONSerializable(item, ancestors));
    ancestors.delete(value);
    return result;
  }

  if (Object.getPrototypeOf(value) === Object.prototype) {
    ancestors.add(value);
    const result = Object.values(value as Record<string, unknown>).every(item =>
      checkJSONSerializable(item, ancestors),
    );
    ancestors.delete(value);
    return result;
  }

  return false;
}

nikolas-sapa avatar Oct 25 '26 12:10 nikolas-sapa

Identify issue type

Identified as Bug
Confidence: High

Rationale

The issue reports an unexpected exception from an existing serialization predicate when given circular data, with a clear reproduction and expected behavior.

Evidence

  • isJSONSerializable recurses indefinitely on circular objects or arrays.
  • The provided reproduction results in RangeError: Maximum call stack size exceeded instead of the expected predicate result false.
  • The proposed cycle-detection change addresses a concrete failure in existing behavior.

ai-sdk-factory[bot] avatar Oct 25 '26 12:10 ai-sdk-factory[bot]

Bug discovery clarification

@nikolas-sapa, how did you discover this bug? Did you encounter it while using the AI SDK in production, find it while auditing or reviewing AI SDK source code, or discover it another way? Please describe the original discovery context.

The report does not establish whether the bug was originally discovered through production use or an audit of the SDK source. The reproduction and implementation-level proposed fix show investigation, but not the original discovery source; the author has not answered a clarification request.

ai-sdk-factory[bot] avatar Oct 25 '26 12:10 ai-sdk-factory[bot]

If you find bugs through an agent audit, please share the prompts you used instead. Please see our updated guidelines at https://github.com/vercel/ai/issues/17243. If you have any questions, comment on #24118.

gr2m avatar Oct 27 '26 18:10 gr2m