toStrictJsonSchema: RangeError: Maximum call stack size exceeded on cyclic $ref inside anyOf/allOf/oneOf

Open Beginner friendly
#3,232 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
nodejs, typescript

Research direction

Start in src/lib/transform.ts at isNullable and trace how seenRefs is handled when following $ref and recursing through allOf, anyOf, and oneOf. Check the existing tests for toStrictJsonSchema and add coverage for a cyclic reference inside each branch type. Done means these cases return a boolean without a stack overflow.

Written by the indexing model from the issue text.

Description

Confirm this is a Node library issue and not an underlying OpenAI API issue
  • This is an issue with the Node library
Describe the bug

When toStrictJsonSchema() checks whether an optional object property is nullable via isNullable(), a cyclic $ref nested inside anyOf, allOf, or oneOf causes RangeError: Maximum call stack size exceeded instead of returning true or false.

Root cause:
In src/lib/transform.ts (isNullable), seenRefs is tracked when following $ref, but seenRefs is omitted when recursing into allOf, anyOf, and oneOf branches (isNullable(variant, root)). Passing seenRefs to those recursive calls (isNullable(variant, root, seenRefs)) lets cyclic branches terminate conservatively (false) without overflowing the call stack.

To Reproduce
  1. Call toStrictJsonSchema() (or zodResponseFormat() / standardSchemaResponseFormat()) with an optional property whose schema references a cyclic $ref inside anyOf, allOf, or oneOf.
  2. Observe RangeError: Maximum call stack size exceeded at isNullable in src/lib/transform.ts.
Code snippets
import { toStrictJsonSchema } from "openai/lib/transform";

toStrictJsonSchema({
  type: "object",
  $defs: {
    Node: {
      anyOf: [{ $ref: "#/$defs/Node" }, { type: "null" }],
    },
  },
  properties: {
    next: { $ref: "#/$defs/Node" },
  },
});
OS

macOS

Node version

v22.23.3

Library version

openai v7.69.0

Dominant language
TypeScript
Stars
11.3k
Forks
1.6k
Avg merge
19h 41m
Merged PRs (30d)
110

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from openai/openai-node

All issues in openai/openai-node

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.