diff --git a/litellm/llms/anthropic/chat/guardrail_translation/handler.py b/litellm/llms/anthropic/chat/guardrail_translation/handler.py index 27052e33de8..54140a4b902 100644 --- a/litellm/llms/anthropic/chat/guardrail_translation/handler.py +++ b/litellm/llms/anthropic/chat/guardrail_translation/handler.py @@ -453,16 +453,16 @@ def _write_back_tool_use( def _streamed_query_strings( - tool_call_blocks: Sequence[tuple[int, str]], tool_calls: Sequence[_ToolCallShape] + tool_call_blocks: Sequence[tuple[int, Mapping[str, object]]], tool_calls: Sequence[_ToolCallShape] ) -> tuple[_WebSearchString, ...]: if len(tool_call_blocks) != len(tool_calls): return () return tuple( chain.from_iterable( _web_search_block_strings( - index, {"type": block_type, "input": _rewritten_tool_use_input(tool_call.arguments)}, with_query=True + index, {**block, "input": _rewritten_tool_use_input(tool_call.arguments)}, with_query=True ) - for (index, block_type), tool_call in zip(tool_call_blocks, tool_calls) + for (index, block), tool_call in zip(tool_call_blocks, tool_calls) ) ) @@ -486,7 +486,7 @@ def _with_query(tool_call: _ToolCallShape, query: str) -> _ToolCallShape: def _with_query_rewrites( tool_calls: tuple[_ToolCallShape, ...], - tool_call_blocks: Sequence[tuple[int, str]], + tool_call_blocks: Sequence[tuple[int, Mapping[str, object]]], query_rewrites: Mapping[int, str], ) -> tuple[_ToolCallShape, ...]: if not query_rewrites or len(tool_call_blocks) != len(tool_calls): @@ -1866,10 +1866,12 @@ class AnthropicMessagesHandler(BaseTranslation): cls._rewrite_ended_stream_events(responses_so_far, rewrite_result_block) @classmethod - def _streamed_tool_call_blocks(cls, responses_so_far: Sequence[object]) -> tuple[tuple[int, str], ...]: + def _streamed_tool_call_blocks( + cls, responses_so_far: Sequence[object] + ) -> tuple[tuple[int, Mapping[str, object]], ...]: events: Final = chain.from_iterable(cls._iter_sse_events(item) for item in responses_so_far) return tuple( - (index, block_type) + (index, block) for event in events if event.get("type") == "content_block_start" and (block := as_str_mapping(event.get("content_block"))) is not None diff --git a/tests/unit/llms/anthropic/chat/guardrail_translation/test_anthropic_guardrail_handler.py b/tests/unit/llms/anthropic/chat/guardrail_translation/test_anthropic_guardrail_handler.py index 2436efa8a2f..152846bde9a 100644 --- a/tests/unit/llms/anthropic/chat/guardrail_translation/test_anthropic_guardrail_handler.py +++ b/tests/unit/llms/anthropic/chat/guardrail_translation/test_anthropic_guardrail_handler.py @@ -621,7 +621,7 @@ class TestAnthropicMessagesHandlerStreamingOutputProcessing: } @classmethod - def _web_search_stream_chunks(cls) -> list[bytes]: + def _web_search_stream_chunks(cls, server_tool_block_type: str = "server_tool_use") -> list[bytes]: events = [ ( "message_start", @@ -643,7 +643,12 @@ class TestAnthropicMessagesHandlerStreamingOutputProcessing: { "type": "content_block_start", "index": 0, - "content_block": {"type": "server_tool_use", "id": "srvtoolu_1", "name": "web_search", "input": {}}, + "content_block": { + "type": server_tool_block_type, + "id": "srvtoolu_1", + "name": "web_search", + "input": {}, + }, }, ), ( @@ -685,14 +690,17 @@ class TestAnthropicMessagesHandlerStreamingOutputProcessing: ] return [f"event: {name}\ndata: {json.dumps(payload)}\n\n".encode() for name, payload in events] + @pytest.mark.parametrize("server_tool_block_type", ["server_tool_use", "tool_use"]) @pytest.mark.parametrize( "guardrail_factory", [_python_masking_guardrail, _python_texts_masking_guardrail], ids=["texts_and_tool_calls", "texts_only"], ) @pytest.mark.asyncio - async def test_deliver_ended_stream_rewrites_mask_the_web_search_query_results_and_text(self, guardrail_factory): - chunks = self._web_search_stream_chunks() + async def test_deliver_ended_stream_rewrites_mask_the_web_search_query_results_and_text( + self, guardrail_factory, server_tool_block_type + ): + chunks = self._web_search_stream_chunks(server_tool_block_type) await AnthropicMessagesHandler().process_output_streaming_response( responses_so_far=chunks,