Skip to content

fix: normalize const constraints in OpenAI tool schemas - #10725

Draft
arccat-114 wants to merge 2 commits into
AstrBotDevs:masterfrom
arccat-114:fix/10720-tool-schema-const
Draft

arccat-114 wants to merge 2 commits into
AstrBotDevs:masterfrom
arccat-114:fix/10720-tool-schema-const

Conversation

@arccat-114

@arccat-114 arccat-114 commented Oct 24, 2026 •

Copy link
Copy Markdown

Fixes #10720.

Modifications

I convert JSON Schema const constraints to single-value enum constraints when building OpenAI-compatible tool declarations. Nested schema nodes are handled without changing property names, defaults, examples, or the original tool parameters. Existing enum constraints keep their intersection with const.

No new dependencies or WebUI changes.

Verification

  • The initial 8 regression cases fail on the original implementation (3 existing tests pass).
  • After the fix and additional coverage, python -m pytest tests/unit/test_tool_google_schema.py -q: 23 passed on Windows, Python 3.13.14.
  • ruff check . and ruff format --check .: passed (Ruff 0.15.22).
  • Explicit Ruff check of the modified test file and git diff --check: passed.

I tested the emitted schemas locally, including nested constraints, literal objects, conflicting enums, boolean/number equality, and unchanged Anthropic input. I have not run the full application suite or sent a live request to a Gemini gateway.

  • No breaking change intended.
  • Regression tests and verification results included.
  • No documentation update needed: no navigation, labels, or configuration changes.
  • No new dependencies.

Summary by Sourcery

Normalize OpenAI tool schemas so nested const constraints are safely represented as compatible enums without mutating source parameters.

Bug Fixes:

  • Normalize JSON Schema const constraints to single-value enum constraints in OpenAI-compatible tool schemas, including nested schema nodes while preserving equivalent constraint behavior.

Enhancements:

  • Preserve original tool parameters and avoid transforming literal const keys in defaults, examples, and enum values.
  • Keep provider-specific schemas unchanged and retain intersections between existing enum and const constraints.

Tests:

  • Add regression coverage for nested schema constraints, conflicting and type-sensitive enum intersections, schema keyword traversal, immutability, and provider-specific behavior.

Signed-off-by: YiMing Zhang <zzz777110@qq.com>
@arccat-114
arccat-114 marked this pull request as draft October 24, 2026 10:01

@sourcery-ai sourcery-ai Bot 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.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="tests/unit/test_tool_google_schema.py" line_range="256" />
<code_context>
+
+    expected = [{"enum": ["a"]}] if isinstance(nested, list) else {"enum": ["a"]}
+    assert result["properties"]["value"][keyword] == expected
+    assert tool.parameters == parameters
+
+
</code_context>
<issue_to_address>
**Source mutation goes undetected**

When conversion mutates `tool.parameters` for a schema-keyword or existing-enum case, when conversion mutates the source for one of these schema cases, `test_openai_schema_converts_const_in_schema_keywords` compares `tool.parameters` with the same `parameters` object, and `test_openai_schema_preserves_const_and_enum_intersection` validates against `tool.parameters` after conversion. Both tests can pass despite the source being changed.

Save a deep copy of the parameters before conversion and assert that the tool's parameters still equal that snapshot.

Also at `tests/unit/test_tool_google_schema.py:224-225`.
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: tests/unit/test_tool_google_schema.py:256


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread tests/unit/test_tool_google_schema.py Outdated
Signed-off-by: YiMing Zhang <zzz777110@qq.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] OpenAI-compatible Gemini 工具 schema const 导致 400

1 participant