fix(xai): treat an explicit empty web_search filters object as unrestricted

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
yassin 2026-09-15 21:24:32 +00:00
parent d415c2856f
commit afb6f8be65
2 changed files with 23 additions and 1 deletions

View file

@ -3,6 +3,7 @@ from types import MappingProxyType
from typing import TYPE_CHECKING, Any, Final
import httpx
from pydantic import TypeAdapter
import litellm
from litellm._logging import verbose_logger
@ -32,6 +33,8 @@ if TYPE_CHECKING:
else:
LiteLLMLoggingObj = Any
_STR_MAPPING_ADAPTER: Final = TypeAdapter(Mapping[str, object])
def _usage_restated_from_xai_ticks(usage: ResponseAPIUsage | None) -> ResponseAPIUsage | None:
reported_cost: Final = xai_reported_cost_in_usd(getattr(usage, "cost_in_usd_ticks", None))
@ -91,7 +94,10 @@ class XAIResponsesAPIConfig(OpenAIResponsesAPIConfig):
"XAI does not support 'search_context_size' parameter. Removing it from web_search tool."
)
domains: Final = tool.get("filters") or tool
nested_filters: Final = tool.get("filters")
domains: Final = (
_STR_MAPPING_ADAPTER.validate_python(nested_filters) if isinstance(nested_filters, Mapping) else tool
)
filters: Final = {key: domains[key] for key in ("allowed_domains", "excluded_domains") if key in domains}
if filters:

View file

@ -164,6 +164,22 @@ class TestXAIResponsesAPITransformation:
assert result["tools"][0]["filters"] == {"allowed_domains": ["nested.com"]}
def test_web_search_empty_nested_filters_win_over_flat(self):
"""An explicit empty 'filters' object means unrestricted search, even when stale flat fields are present"""
config = XAIResponsesAPIConfig()
params = ResponsesAPIOptionalRequestParams(
tools=[{"type": "web_search", "allowed_domains": ["flat.com"], "filters": {}}]
)
result = config.map_openai_params(
response_api_optional_params=params,
model="grok-4-1-fast",
drop_params=False,
)
assert result["tools"][0] == {"type": "web_search"}
def test_web_search_search_context_size_removed(self):
"""Test that search_context_size is removed from web_search tools"""
config = XAIResponsesAPIConfig()