From 080d1a82646664bad07e8f0e61024f35b976b5a4 Mon Sep 17 00:00:00 2001 From: skulitom Date: Sat, 12 Sep 2026 17:08:30 +0100 Subject: [PATCH] fix(anthropic): preserve content-filtered finish reasons --- .../adapters/transformation.py | 2 ++ ...al_pass_through_adapters_transformation.py | 22 +++++++++++++++ .../test_streaming_iterator_stop_reason.py | 27 ++++++++++++++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py index 8ff9f2e0679..c7a4af1896b 100644 --- a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py +++ b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py @@ -1367,6 +1367,8 @@ class LiteLLMAnthropicMessagesAdapter: return "max_tokens" elif openai_finish_reason == "tool_calls": return "tool_use" + elif openai_finish_reason == "content_filter": + return "refusal" return "end_turn" @staticmethod diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py index 03b9840b1c3..cc0269b6bba 100644 --- a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py +++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py @@ -57,6 +57,28 @@ def test_translate_openai_response_to_anthropic_empty_choices() -> None: assert result["usage"]["input_tokens"] == 10 +@pytest.mark.parametrize(("finish_reason", "stop_reason"), [("content_filter", "refusal"), ("stop", "end_turn")]) +def test_translate_chat_content_filter_without_refusal_text(finish_reason: str, stop_reason: str) -> None: + response: Final = ModelResponse( + id="chatcmpl-filtered", + model="openai-model", + choices=[ + Choices( + index=0, + finish_reason=finish_reason, + message=Message(content=None, role="assistant"), + ) + ], + usage=Usage(prompt_tokens=12, completion_tokens=0, total_tokens=12), + ) + + result: Final = LiteLLMAnthropicMessagesAdapter().translate_openai_response_to_anthropic(response) + + assert result["stop_reason"] == stop_reason + assert result["content"] == [] + assert result["usage"]["input_tokens"] == 12 + + def test_translate_chat_refusal_to_anthropic_response(): response = ModelResponse( id="chatcmpl-refusal", diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_streaming_iterator_stop_reason.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_streaming_iterator_stop_reason.py index 4b95b36fec3..083ba5cf0de 100644 --- a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_streaming_iterator_stop_reason.py +++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_streaming_iterator_stop_reason.py @@ -10,6 +10,8 @@ bridge emitted ``stop_reason: "end_turn"`` and Anthropic tool-runners (Claude Code, ``messages.stream``) silently dropped the tool call. """ +from typing import Final + import pytest from litellm.llms.anthropic.experimental_pass_through.adapters.streaming_iterator import ( @@ -18,7 +20,7 @@ from litellm.llms.anthropic.experimental_pass_through.adapters.streaming_iterato from litellm.llms.ollama.chat.transformation import ( OllamaChatCompletionResponseIterator, ) -from litellm.types.utils import ModelResponseStream +from litellm.types.utils import Delta, ModelResponseStream, StreamingChoices, Usage _OLLAMA_TOOL_CHUNK = { "model": "qwen3:8b", @@ -77,3 +79,26 @@ def test_ollama_mid_stream_tool_call_yields_tool_use_stop_reason_sync(): async def test_ollama_mid_stream_tool_call_yields_tool_use_stop_reason_async(): wrapper = AnthropicStreamWrapper(completion_stream=_AsyncStream(_ollama_streamed_chunks()), model="qwen3:8b") _assert_tool_use_stop_reason([event async for event in wrapper]) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("async_mode", [False, True], ids=["sync", "async"]) +@pytest.mark.parametrize(("finish_reason", "stop_reason"), [("content_filter", "refusal"), ("stop", "end_turn")]) +async def test_content_filter_without_refusal_text(finish_reason: str, stop_reason: str, async_mode: bool) -> None: + chunks: Final = [ + ModelResponseStream(choices=[StreamingChoices(delta=Delta(role="assistant", content=""), finish_reason=None)]), + ModelResponseStream( + choices=[StreamingChoices(delta=Delta(), finish_reason=finish_reason)], + usage=Usage(prompt_tokens=12, completion_tokens=0, total_tokens=12), + ), + ] + wrapper: Final = AnthropicStreamWrapper( + completion_stream=_AsyncStream(chunks) if async_mode else iter(chunks), model="openai-model" + ) + events: Final = [event async for event in wrapper] if async_mode else list(wrapper) + message_deltas: Final = tuple(event for event in events if event["type"] == "message_delta") + + assert len(message_deltas) == 1 + assert message_deltas[0]["delta"]["stop_reason"] == stop_reason + assert message_deltas[0]["usage"]["input_tokens"] == 12 + assert events[-1]["type"] == "message_stop"