test: cover responses container routing in ci shard

This commit is contained in:
冯基魁 2026-06-23 13:37:05 +08:00
parent c9a1726525
commit 94a1c78554
3 changed files with 134 additions and 0 deletions

View file

@ -309,6 +309,74 @@ class TestContainerAPI:
assert decoded.get("response_id") == "z"
assert decoded.get("model_id") == "router-gpt"
@pytest.mark.asyncio
async def test_router_aresponses_routes_managed_tool_container_id_to_owner_deployment(
self,
):
router = Router(model_list=[])
router._ageneric_api_call_with_fallbacks = AsyncMock(
return_value={"id": "resp_1"}
)
encoded_container_id = ResponsesAPIRequestUtils._build_container_id(
custom_llm_provider="azure",
model_id="azure-deployment-id",
container_id="cntr_native_123",
)
tools = [
"not-a-dict",
{"type": "file_search", "container": "cntr_ignored"},
{"type": "code_interpreter", "container": {"type": "auto"}},
{"type": "code_interpreter", "container": encoded_container_id},
]
await router.aresponses(
model="logical-model",
input="hello",
tools=tools,
custom_llm_provider="openai",
)
call_kwargs = router._ageneric_api_call_with_fallbacks.call_args.kwargs
assert call_kwargs["model"] == "azure-deployment-id"
assert call_kwargs["custom_llm_provider"] == "azure"
assert call_kwargs["tools"][3]["container"] == "cntr_native_123"
assert tools[3]["container"] == encoded_container_id
def test_router_responses_routes_managed_tool_container_id_to_owner_deployment(
self,
):
router = Router(model_list=[])
router._generic_api_call_with_fallbacks = MagicMock(
return_value={"id": "resp_1"}
)
encoded_container_id = ResponsesAPIRequestUtils._build_container_id(
custom_llm_provider="azure",
model_id="azure-deployment-id",
container_id="cntr_native_123",
)
tools = [
"not-a-dict",
{"type": "file_search", "container": "cntr_ignored"},
{"type": "code_interpreter", "container": {"type": "auto"}},
{"type": "code_interpreter", "container": encoded_container_id},
]
response = router.responses(
model="logical-model",
input="hello",
tools=tools,
custom_llm_provider="openai",
)
call_kwargs = router._generic_api_call_with_fallbacks.call_args.kwargs
assert response == {"id": "resp_1"}
assert call_kwargs["model"] == "azure-deployment-id"
assert call_kwargs["custom_llm_provider"] == "azure"
assert call_kwargs["tools"][3]["container"] == "cntr_native_123"
assert tools[3]["container"] == encoded_container_id
@pytest.mark.asyncio
async def test_aretrieve_container_basic(self):
"""Test basic async container retrieval functionality."""

View file

@ -38,6 +38,59 @@ def _container(container_id: str) -> ContainerObject:
)
def test_collect_response_code_interpreter_container_ids_filters_tools():
from litellm.proxy.common_request_processing import (
_collect_response_code_interpreter_container_ids,
)
assert _collect_response_code_interpreter_container_ids({"tools": None}) == set()
assert _collect_response_code_interpreter_container_ids(
{
"tools": [
"not-a-dict",
{"type": "file_search", "container": "cntr_ignored"},
{"type": "code_interpreter", "container": {"type": "auto"}},
{"type": "code_interpreter", "container": ""},
{"type": "code_interpreter", "container": "cntr_1"},
]
}
) == {"cntr_1"}
@pytest.mark.asyncio
async def test_response_code_interpreter_container_authorization_defaults_to_openai(
monkeypatch,
):
from litellm.proxy.common_request_processing import (
_authorize_response_code_interpreter_containers,
)
mock_assert = AsyncMock()
monkeypatch.setattr(
ownership,
"assert_user_can_access_container",
mock_assert,
)
await _authorize_response_code_interpreter_containers(
data={
"tools": [
{
"type": "code_interpreter",
"container": "cntr_native_123",
}
]
},
user_api_key_dict=UserAPIKeyAuth(user_id="user-1"),
)
mock_assert.assert_awaited_once_with(
container_id="cntr_native_123",
user_api_key_dict=UserAPIKeyAuth(user_id="user-1"),
custom_llm_provider="openai",
)
@pytest.mark.asyncio
async def test_should_record_container_owner_with_original_provider_id(monkeypatch):
table = AsyncMock()

View file

@ -143,6 +143,11 @@ class TestResponsesAPIRequestUtils:
assert updated_tools[0]["container"] == "cntr_native_123"
assert tools[0]["container"] == encoded_container_id
assert (
ResponsesAPIRequestUtils.decode_container_ids_in_tools_for_request(None)
is None
)
plain_tools = [{"type": "file_search", "vector_store_ids": ["vs_123"]}]
plain_result = (
ResponsesAPIRequestUtils.decode_container_ids_in_tools_for_request(
@ -151,6 +156,14 @@ class TestResponsesAPIRequestUtils:
)
assert plain_result is plain_tools
plain_tuple = ({"type": "file_search", "vector_store_ids": ["vs_123"]},)
plain_tuple_result = (
ResponsesAPIRequestUtils.decode_container_ids_in_tools_for_request(
plain_tuple
)
)
assert plain_tuple_result == list(plain_tuple)
def test_update_responses_api_response_id_with_model_id_handles_dict(self):
"""Ensure _update_responses_api_response_id_with_model_id works with dict input"""
responses_api_response = {"id": "resp_abc123"}