Skip to content

fix(schema_sanitizer): strip pattern/format from Responses-format tools for xAI compatibility#27219

Closed
zccyman wants to merge 3 commits into
NousResearch:mainfrom
atyou2happy:fix/schema-sanitizer-responses-format-27197
Closed

fix(schema_sanitizer): strip pattern/format from Responses-format tools for xAI compatibility#27219
zccyman wants to merge 3 commits into
NousResearch:mainfrom
atyou2happy:fix/schema-sanitizer-responses-format-27197

Conversation

@zccyman

@zccyman zccyman commented May 17, 2026

Copy link
Copy Markdown
Contributor

Summary

strip_pattern_and_format() now handles Responses-format tool schemas ({\name: ..., \parameters: ...}) used by codex_responses API mode, plus auto-strips before xAI /responses calls.

Closes #27197

Problem

xAI's /responses endpoint rejects pattern and format keywords in tool schemas with HTTP 400. The existing strip_pattern_and_format() only walked OpenAI-format tools ({\function: {\parameters: ...}}), missing Responses-format shapes.

Solution

  1. tools/schema_sanitizer.py: Extended strip_pattern_and_format() to handle both OpenAI-format ({\function: {\parameters: ...}}) and Responses-format ({\name: ..., \parameters: ...}) tools
  2. run_agent.py: Auto-strip pattern/format from tool schemas before building codex_responses kwargs for xAI providers (xai, xai-oauth)

Files Changed

File +/- Description
tools/schema_sanitizer.py +14/-1 Responses-format tool walk path
run_agent.py +10/-0 xAI auto-strip in _build_api_kwargs
tests/tools/test_schema_sanitizer.py +60/-0 2 new tests

Test Results

tests/tools/test_schema_sanitizer.py ✓ 29 passed in 3.61s

…ls for xAI compatibility

Closes NousResearch#27197

Problem:
strip_pattern_and_format() only walked OpenAI-format tool shapes
({"function": {"parameters": ...}}), missing Responses-format
({"name": ..., "parameters": ...}), leaving pattern/format in
xAI /responses calls causing HTTP 400.

Solution:
1. Extend strip_pattern_and_format() for Responses-format tools
2. Call it before codex_responses kwargs for xAI providers

Tests:
29/29 passed (27 original + 2 new Responses-format tests)
+156 chars in new test file
@cardtest15-coder

This comment was marked as spam.

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent loop, run_agent.py, prompt builder provider/xai xAI (Grok) labels May 17, 2026
@zccyman

zccyman commented May 17, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @cardtest15-coder for the thorough review!

Addressed in f1f1ef3c0:

  1. Silent except Exception: pass → now logs via logging.warning(f"...: {e}") instead of silent pass
  2. Missing format test for Responses format → added test_strip_responses_format_strips_format_keyword() — verifies format: "date-time" is stripped from Responses-format tools
  3. Inline import — kept it because the import is inside a conditional branch (is_xai_responses) and moving it to module level would add an unnecessary dependency; logging makes the failure mode visible now

Regarding the _walk implementation concern: it's value-type-aware (isinstance(node[key], str) before stripping pattern/format), so a property literally named pattern with a dict schema value survives correctly (verified by existing test_strip_preserves_property_named_pattern and our new idempotency test).

@cardtest15-coder

This comment was marked as spam.

@zccyman

zccyman commented May 17, 2026

Copy link
Copy Markdown
Contributor Author

@cardtest15-coder — proactively added mixed-format test in 4d56366 (OpenAI-format + Responses-format tools in one list, both verified sanitized). All 31 schema sanitizer tests pass.

Looks like your follow-up ran into a Fireworks API 412 error. Once the API is back, feel free to re-review! 🚀

teknium1 added a commit that referenced this pull request May 17, 2026
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>
@teknium1

Copy link
Copy Markdown
Contributor

Thanks for this fix — landed in main via #27575 with your authorship preserved on the sanitizer commit. I ported your run_agent.py wire-up to agent/chat_completion_helpers.py since _build_api_kwargs was extracted into that module after you opened the PR (which is why the merge was DIRTY). Co-author credit on the wire-up commit.

gweeteve pushed a commit to gweeteve/hermes-agent that referenced this pull request Jun 2, 2026
Port of the run_agent.py changes from NousResearch#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>
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 P2 Medium — degraded but workaround exists provider/xai xAI (Grok) 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

4 participants