mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(anthropic): preserve content-filtered finish reasons
This commit is contained in:
parent
f2e0a5db1e
commit
080d1a8264
3 changed files with 50 additions and 1 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue