Skip to content

fix(tools): support self-referencing component schemas in OpenAPITool - #2012

Merged
Tomas2D merged 1 commit into
i-am-bee:mainfrom
shlokbam:fix/openapi-recursive-component-schemas
Nov 26, 2026
Merged

Tomas2D merged 1 commit into
i-am-bee:mainfrom
shlokbam:fix/openapi-recursive-component-schemas

Conversation

@shlokbam

Copy link
Copy Markdown
Contributor

Which issue(s) does this pull-request address?

Closes: #2011

Description

If a schema in components/schemas refers to itself (a tree, a comment thread), OpenAPITool.input_schema fails with RecursionError. #1970 handles cycles in inline_schema_refs, but only for references into $defs, and OpenAPI specs use #/components/schemas/....

Changes in tools/openapi.py:

  • Before inlining, #/components/schemas/X references are rewritten to #/$defs/X and the component schemas are passed as $defs, so inline_schema_refs keeps the references that close a cycle instead of producing a cyclic dict.
  • The definitions it keeps are moved to the top of the operation's input schema. JSONSchemaModel.create only reads $defs there (left inside body, model_json_schema() fails with KeyError: '#/$defs/Category').

The result matches #1970: the first levels of a recursive type are validated, and deeper levels accept any object.

Schemas without cycles give the same input schema as before. I compared model_json_schema() on main and with this change for 455 tools from 150 generated specs (nested, array, anyOf and parameter references, no cycles): all identical.

Checklist

General

  • I have read the appropriate contributor guide: Python
  • I have signed off on my commit
  • Commit messages and PR title follow conventional commits
  • Appropriate label(s) added to PR: Python

Code quality checks

  • Code quality checks pass: ruff check, ruff format --check (0.9.10). pyrefly: not run

Testing

  • Unit tests pass: tests/tools/test_openapi.py, test_openapi_ssrf.py, tests/backend/test_utils.py, tests/utils/test_models.py: 41 passed (+2 new)
  • E2E tests pass: not run (they need provider credentials)
  • Tests are included: a request body with a self-referencing schema (built, validated, and sent as JSON), and a query parameter with mutually referencing schemas. Both fail on main with RecursionError and pass with this change.

Documentation

  • Documentation is updated: not needed

@shlokbam
shlokbam requested a review from a team as a code owner November 25, 2026 11:49
@github-actions github-actions Bot added the python Python related functionality label Nov 25, 2026
A schema in components/schemas that refers to itself (a tree, a comment thread) made OpenAPITool.input_schema fail with RecursionError. Point those references at $defs so inline_schema_refs keeps the cycle, and move the kept definitions to the top of the operation's schema, where JSONSchemaModel.create reads them.

Signed-off-by: Shlok Bam <[email protected]>
@Tomas2D
Tomas2D force-pushed the fix/openapi-recursive-component-schemas branch from 1cb95dd to 36f5130 Compare November 26, 2026 22:49

@Tomas2D Tomas2D left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I rebased this onto main (the only conflict was both PRs adding a module-level helper in the same place) and reran the OpenAPI tests.

@Tomas2D
Tomas2D merged commit 6ad233f into i-am-bee:main Nov 26, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Python related functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAPITool (Python) fails with RecursionError when a component schema refers to itself

2 participants