litellm/tests/test_litellm/test_xai_responses_auto_routing.py
yuneng-jiang 6a0d03914c
test: drop the cwd-relative sys.path.insert calls from the test suite (#37802)
* test: drop the cwd-relative sys.path.insert calls from the test suite

TQ003 stands at 1,077 across 1,058 files, and 1,015 of them are the same shape:
sys.path.insert(0, os.path.abspath("../..")) and its deeper siblings. The
argument resolves against the working directory rather than the file, so from
the repo root, where every job runs pytest, it inserts the directory two levels
above the checkout. It has never pointed at litellm. The package is installed
into the environment anyway, which is what actually makes the import work, and
what the rule's message has said all along.

Removing them leaves 1,634 imports of sys and os with no remaining reference,
and those go too, except where another test module imports the name back out of
the file. The rest of TQ003 is 62 call sites that resolve against __file__ or a
variable, which are a different question and are left alone.

Collection is identical either way: 45,871 tests and the same 51 pre-existing
collection errors before and after, and ruff reports no new undefined name.

* test: drop the duplicate imports the sys.path sweep exposed to F811

* test(pre-call-utils): restore the os import the new bedrock tests need
2026-08-22 09:25:58 -07:00

248 lines
8.6 KiB
Python

"""
Test automatic routing to xAI Responses API when tools are present
"""
from unittest.mock import MagicMock, patch
import pytest
import litellm
from litellm.main import responses_api_bridge_check
class TestXAIResponsesAutoRouting:
"""Test that xAI requests with tools automatically route to Responses API"""
def test_responses_api_bridge_check_without_tools(self):
"""Test that without tools, xAI uses chat mode"""
model = "grok-3"
custom_llm_provider = "xai"
tools = None
web_search_options = None
model_info, updated_model = responses_api_bridge_check(
model=model,
custom_llm_provider=custom_llm_provider,
web_search_options=web_search_options,
)
# Should not auto-route to responses mode without tools
assert model_info.get("mode") != "responses"
assert updated_model == model
def test_responses_api_bridge_check_with_tools(self):
"""Test that with tools, xAI automatically routes to Responses API"""
model = "grok-3"
custom_llm_provider = "xai"
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the weather",
"parameters": {
"type": "object",
"properties": {"location": {"type": "string"}},
},
},
}
]
web_search_options = None
model_info, updated_model = responses_api_bridge_check(
model=model,
custom_llm_provider=custom_llm_provider,
web_search_options=web_search_options,
)
# Should auto-route to responses mode when tools are present
assert model_info.get("mode") == "chat"
assert updated_model == model
def test_responses_api_bridge_check_with_empty_tools(self):
"""Test that with empty tools list, xAI does not route to Responses API"""
model = "grok-3"
custom_llm_provider = "xai"
tools = []
web_search_options = None
model_info, updated_model = responses_api_bridge_check(
model=model,
custom_llm_provider=custom_llm_provider,
web_search_options=web_search_options,
)
# Should not auto-route with empty tools list
assert model_info.get("mode") != "responses"
assert updated_model == model
def test_responses_api_bridge_check_non_xai_provider_with_tools(self):
"""Test that non-xAI providers don't get auto-routed"""
model = "gpt-4"
custom_llm_provider = "openai"
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the weather",
},
}
]
web_search_options = None
model_info, updated_model = responses_api_bridge_check(
model=model,
custom_llm_provider=custom_llm_provider,
web_search_options=web_search_options,
)
# Should not auto-route non-xAI providers
assert model_info.get("mode") != "responses"
assert updated_model == model
def test_responses_api_bridge_check_with_responses_prefix(self):
"""Test that responses/ prefix still works"""
model = "responses/grok-3"
custom_llm_provider = "xai"
tools = None
web_search_options = None
model_info, updated_model = responses_api_bridge_check(
model=model,
custom_llm_provider=custom_llm_provider,
web_search_options=web_search_options,
)
# Should route to responses mode with prefix, even without tools
assert model_info.get("mode") == "responses"
assert updated_model == "grok-3" # prefix removed
def test_responses_api_bridge_check_with_code_interpreter_tool(self):
"""Test auto-routing with code_interpreter tool"""
model = "grok-3"
custom_llm_provider = "xai"
tools = [{"type": "code_interpreter"}]
web_search_options = None
model_info, updated_model = responses_api_bridge_check(
model=model,
custom_llm_provider=custom_llm_provider,
web_search_options=web_search_options,
)
# Should auto-route with code_interpreter tool
assert model_info.get("mode") == "chat"
assert updated_model == model
def test_responses_api_bridge_check_with_web_search_tool(self):
"""Test auto-routing with web_search tool"""
model = "grok-4"
custom_llm_provider = "xai"
tools = [
{"type": "web_search", "filters": {"allowed_domains": ["wikipedia.org"]}}
]
web_search_options = None
model_info, updated_model = responses_api_bridge_check(
model=model,
custom_llm_provider=custom_llm_provider,
web_search_options=web_search_options,
)
# Should auto-route with web_search tool
assert model_info.get("mode") == "chat"
assert updated_model == model
def test_responses_api_bridge_check_with_x_search_tool(self):
"""Test auto-routing with x_search tool"""
model = "grok-4"
custom_llm_provider = "xai"
tools = [{"type": "x_search", "allowed_x_handles": ["@elonmusk"]}]
web_search_options = None
model_info, updated_model = responses_api_bridge_check(
model=model,
custom_llm_provider=custom_llm_provider,
web_search_options=web_search_options,
)
# Should auto-route with x_search tool
assert model_info.get("mode") == "chat"
assert updated_model == model
def test_responses_api_bridge_check_with_web_search_options(self):
"""Test auto-routing with web_search_options"""
model = "grok-4-1-fast"
custom_llm_provider = "xai"
tools = None
web_search_options = {} # Empty dict should trigger routing
model_info, updated_model = responses_api_bridge_check(
model=model,
custom_llm_provider=custom_llm_provider,
web_search_options=web_search_options,
)
# Should auto-route with web_search_options
assert model_info.get("mode") == "responses"
assert updated_model == model
def test_responses_api_bridge_check_with_web_search_options_and_tools(self):
"""Test auto-routing with both web_search_options and tools"""
model = "grok-4"
custom_llm_provider = "xai"
tools = [{"type": "code_interpreter"}]
web_search_options = {"enabled": True}
model_info, updated_model = responses_api_bridge_check(
model=model,
custom_llm_provider=custom_llm_provider,
web_search_options=web_search_options,
)
# Should auto-route with both present
assert model_info.get("mode") == "responses"
assert updated_model == model
@patch("litellm.completion_extras.responses_api_bridge.completion")
def test_completion_with_tools_routes_to_responses_api(
self, mock_responses_completion
):
"""Test that completion() with tools routes to Responses API"""
# Mock the responses_api_bridge.completion to avoid actual API calls
mock_responses_completion.return_value = MagicMock()
model = "xai/grok-3"
messages = [{"role": "user", "content": "What's the weather?"}]
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather info",
"parameters": {
"type": "object",
"properties": {"location": {"type": "string"}},
},
},
}
]
try:
litellm.completion(
model=model,
messages=messages,
tools=tools,
mock_response="This is a test", # Use mock mode to avoid API calls
)
except Exception:
# It's ok if this fails, we just want to verify the routing logic
pass
# The mock should have been called, indicating responses API was used
# Note: This test may need adjustment based on actual mock_response behavior
# The key is that the responses_api_bridge_check logic routes correctly
if __name__ == "__main__":
pytest.main([__file__, "-v"])