fix(xai): strip pattern/format from Responses-format tool schemas (closes #27197, #27219)#27575
Merged
Conversation
…ls for xAI compatibility
xAI's /responses endpoint rejects pattern and format JSON Schema keywords
in tool schemas with HTTP 400 'Invalid arguments passed to the model'.
The existing strip_pattern_and_format() only walked OpenAI-format tools
({'function': {'parameters': ...}}), missing Responses-format shapes
({'name': ..., 'parameters': ...}) used by codex_responses API mode.
This shows up most often with MCP-derived tools that carry validation
keywords (e.g. domain pattern regex in firecrawl, format: date-time)
through to the wire.
Extends the walk to handle both shapes. Auto-strip wiring is applied
separately in chat_completion_helpers (post-refactor location).
Closes #27197
Port of the run_agent.py changes from #27219 to current main: the _build_api_kwargs body was extracted into agent/chat_completion_helpers. build_api_kwargs, so wire the xAI tool-schema sanitization there (provider in {'xai', 'xai-oauth'} or base_url=api.x.ai). Logs a warning instead of silently swallowing exceptions, matching the contributor's review-followup fix. Co-authored-by: zccyman <zccyman@163.com>
Contributor
🔎 Lint report:
|
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.
Salvage of #27219 by @zccyman onto current main, with the wire-up ported to the post-refactor location (
_build_api_kwargswas extracted intoagent/chat_completion_helpers.build_api_kwargsafter the PR was opened).Summary
xAI's /responses endpoint rejects
patternandformatJSON Schema keywords in tool schemas with HTTP 400 'Invalid arguments passed to the model'. The existingstrip_pattern_and_format()only walked OpenAI-format tools ({'function': {'parameters': ...}}), missing Responses-format shapes ({'name': ..., 'parameters': ...}) used by codex_responses API mode. MCP-derived tools are the common trigger — they carry JSON Schema validation keywords (firecrawl's domain regex, Wolfram'sformat: date-time, etc.) through to the wire.This is the bug a user just hit in Discord:
hiwith Wolfram MCP enabled on Grok-4.3 → "Invalid arguments passed to the model". Confirmed on current main —tools/schema_sanitizer.py:357-362only descends intotool['function']['parameters'], nevertool['parameters']directly.Changes
tools/schema_sanitizer.py(+14/-1) — extendstrip_pattern_and_format()to handle both OpenAI-format and Responses-format tools.tests/tools/test_schema_sanitizer.py(+131) — 4 new tests covering Responses-format pattern stripping, format keyword stripping, idempotency (property literally namedpatternsurvives), and mixed-format lists.agent/chat_completion_helpers.py(+15) — auto-strip inbuild_api_kwargswhenprovider in {xai, xai-oauth}orbase_url=api.x.ai. Logs a warning on sanitizer failure instead of swallowing the exception (the contributor's own review-followup fix).Validation
E2E verified with a real-world MCP-style Responses-format tool —
patternandformatschema keywords stripped, the property literally namedformatpreserved (sibling-of-type guard works correctly), wire-up is upstream of_ct.build_kwargs.Closes #27197
Closes #27219
Co-authored-by: zccyman zccyman@163.com