fix(xai): wire schema sanitizer into post-refactor build_api_kwargs

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>
This commit is contained in:
teknium1 2026-05-17 12:42:13 -07:00 committed by Teknium
parent 2551f08130
commit bdc2113b5c

View file

@ -286,6 +286,21 @@ def build_api_kwargs(agent, api_messages: list) -> dict:
)
is_xai_responses = agent.provider in {"xai", "xai-oauth"} or agent._base_url_hostname == "api.x.ai"
_msgs_for_codex = agent._prepare_messages_for_non_vision_model(api_messages)
# xAI's /responses endpoint rejects ``pattern`` and ``format`` keywords
# in tool schemas (HTTP 400 "Invalid arguments passed to the model").
# Most commonly hit when MCP-derived tools carry JSON Schema validation
# keywords through. Strip them before building kwargs. See #27197.
if is_xai_responses:
try:
from tools.schema_sanitizer import strip_pattern_and_format
tools_for_api, _ = strip_pattern_and_format(tools_for_api)
except Exception as exc:
logger.warning(
"%s⚠️ Failed to sanitize tool schemas for xAI: %s",
getattr(agent, "log_prefix", ""), exc,
)
return _ct.build_kwargs(
model=agent.model,
messages=_msgs_for_codex,