Fix infinite recursion in example generation for circular type references#10452
Closed
musicpulpite wants to merge 1 commit intomainfrom
Closed
Fix infinite recursion in example generation for circular type references#10452musicpulpite wants to merge 1 commit intomainfrom
musicpulpite wants to merge 1 commit intomainfrom
Conversation
…nces Add cycle detection to prevent infinite recursion when generating examples for types with circular references in both Python and TypeScript generators. Python changes: - Add RecursionGuard utility class to track visited types and enforce depth limits - Thread recursion_guard parameter through SnippetWriter and all snippet generators - Return None/undefined when cycles are detected (filtered by existing code) TypeScript changes: - Add RecursionGuard utility class with similar functionality - Update GeneratedTypeReferenceExampleImpl to check for cycles before recursing - Thread recursion_guard through all buildExample implementations - Return undefined for cyclic values (filtered by existing code) This fixes infinite recursion bugs in both Python and TypeScript SDK generation when types have circular references. Co-Authored-By: William McAdams <musicpulpite@gmail.com>
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix infinite recursion in example generation for circular type references
Summary
This PR adds cycle detection to prevent infinite recursion when generating examples for types with circular references in both Python and TypeScript SDK generators.
Problem: When types have circular references (e.g., type A references type B, which references type A), the example generators would recurse infinitely, causing stack overflow errors as seen in VapiAI/docs PR #795.
Solution:
RecursionGuardutility classes for both Python and TypeScript that track visited types and enforce a maximum recursion depth (default: 5)None/undefinedwhich gets filtered out by existing codePython changes:
RecursionGuardclass ingenerators/python/src/fern_python/snippet/recursion_guard.pySnippetWriterand all snippet generator implementations to accept and pass throughrecursion_guardparameterFernTypedDicthelper methods to support recursion guardTypeScript changes:
RecursionGuardinterface andRecursionGuardImplclass ingenerators/typescript/model/type-reference-example-generator/src/RecursionGuard.tsGeneratedTypeReferenceExampleImpl.buildExample()to check for cycles before recursing into named typesBaseGeneratedTypeinterface and all concrete implementations to accept optionalrecursionGuardparameterReview & Testing Checklist for Human
pnpm buildor equivalent in the TypeScript generators to ensure no compilation errors were introducedNone/undefinedon cycle detection is correct and doesn't break example generation for valid casesTest Plan
type A { b: B },type B { a: A })Notes
withDepthIncrement()method is used for container types (lists, maps) to track depth without marking types as visited