Skip to content

fix(xai): strip pattern/format from Responses-format tool schemas (closes #27197, #27219)#27575

Merged
teknium1 merged 2 commits into
mainfrom
hermes/hermes-4631ae05
May 17, 2026
Merged

fix(xai): strip pattern/format from Responses-format tool schemas (closes #27197, #27219)#27575
teknium1 merged 2 commits into
mainfrom
hermes/hermes-4631ae05

Conversation

@teknium1

Copy link
Copy Markdown
Contributor

Salvage of #27219 by @zccyman onto current main, with the wire-up ported to the post-refactor location (_build_api_kwargs was extracted into agent/chat_completion_helpers.build_api_kwargs after the PR was opened).

Summary

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. MCP-derived tools are the common trigger — they carry JSON Schema validation keywords (firecrawl's domain regex, Wolfram's format: date-time, etc.) through to the wire.

This is the bug a user just hit in Discord: hi with Wolfram MCP enabled on Grok-4.3 → "Invalid arguments passed to the model". Confirmed on current main — tools/schema_sanitizer.py:357-362 only descends into tool['function']['parameters'], never tool['parameters'] directly.

Changes

  • tools/schema_sanitizer.py (+14/-1) — extend strip_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 named pattern survives), and mixed-format lists.
  • agent/chat_completion_helpers.py (+15) — auto-strip in build_api_kwargs when provider in {xai, xai-oauth} or base_url=api.x.ai. Logs a warning on sanitizer failure instead of swallowing the exception (the contributor's own review-followup fix).

Validation

scripts/run_tests.sh tests/tools/test_schema_sanitizer.py
# 31 passed in 0.92s

E2E verified with a real-world MCP-style Responses-format tool — pattern and format schema keywords stripped, the property literally named format preserved (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

zccyman and others added 2 commits May 17, 2026 12:42
…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>
@github-actions

Copy link
Copy Markdown
Contributor

🔎 Lint report: hermes/hermes-4631ae05 vs origin/main

ruff

Total: 0 on HEAD, 0 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 0 pre-existing issues carried over.

ty (type checker)

Total: 8708 on HEAD, 8708 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 4590 pre-existing issues carried over.

Diagnostics are surfaced as warnings — this check never fails the build.

@alt-glitch alt-glitch added type/bug Something isn't working P3 Low — cosmetic, nice to have provider/xai xAI (Grok) comp/tools Tool registry, model_tools, toolsets comp/agent Core agent loop, run_agent.py, prompt builder tool/mcp MCP client and OAuth labels May 17, 2026
@teknium1 teknium1 merged commit bdc2113 into main May 17, 2026
20 of 21 checks passed
@teknium1 teknium1 deleted the hermes/hermes-4631ae05 branch May 17, 2026 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent loop, run_agent.py, prompt builder comp/tools Tool registry, model_tools, toolsets P3 Low — cosmetic, nice to have provider/xai xAI (Grok) tool/mcp MCP client and OAuth type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: xAI Grok (xai-oauth) returns HTTP 400 or streaming fallback errors on Responses API

3 participants