From 94a1c78554f0f3661b785de0fcdb16ed725ea6fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=AF=E5=9F=BA=E9=AD=81?= <1412414664@qq.com> Date: Tue, 23 Jun 2026 13:37:05 +0800 Subject: [PATCH] test: cover responses container routing in ci shard --- .../containers/test_container_api.py | 68 +++++++++++++++++++ .../test_container_proxy_ownership.py | 53 +++++++++++++++ .../responses/test_responses_utils.py | 13 ++++ 3 files changed, 134 insertions(+) diff --git a/tests/test_litellm/containers/test_container_api.py b/tests/test_litellm/containers/test_container_api.py index 4032c072594..85c08a035b7 100644 --- a/tests/test_litellm/containers/test_container_api.py +++ b/tests/test_litellm/containers/test_container_api.py @@ -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.""" diff --git a/tests/test_litellm/containers/test_container_proxy_ownership.py b/tests/test_litellm/containers/test_container_proxy_ownership.py index 17168660f1b..0e8b646428e 100644 --- a/tests/test_litellm/containers/test_container_proxy_ownership.py +++ b/tests/test_litellm/containers/test_container_proxy_ownership.py @@ -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() diff --git a/tests/test_litellm/responses/test_responses_utils.py b/tests/test_litellm/responses/test_responses_utils.py index 36225998ac4..a40e3da8a07 100644 --- a/tests/test_litellm/responses/test_responses_utils.py +++ b/tests/test_litellm/responses/test_responses_utils.py @@ -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"}