Fix infinite recursion in example generation for circular type references #10452
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