test: add direct coverage for content_filter and refusal in anthropic and responses adapters

This commit is contained in:
tusharjamunkar 2026-09-12 23:07:31 +05:30
parent cd66b34b45
commit e54399eff7
2 changed files with 102 additions and 0 deletions

View file

@ -102,6 +102,46 @@ def test_translate_chat_length_takes_precedence_over_refusal():
assert result.get("stop_details") is None
def test_translate_chat_content_filter_to_anthropic_response():
response = ModelResponse(
id="chatcmpl-content-filter",
model="openai-model",
choices=[
Choices(
index=0,
finish_reason="content_filter",
message=Message(content=None, role="assistant"),
)
],
usage=Usage(prompt_tokens=1, completion_tokens=0, total_tokens=1),
)
result = LiteLLMAnthropicMessagesAdapter().translate_openai_response_to_anthropic(response)
assert result["content"] == []
assert result["stop_reason"] == "refusal"
def test_translate_chat_refusal_finish_reason_to_anthropic_response():
response = ModelResponse(
id="chatcmpl-refusal-reason",
model="openai-model",
choices=[
Choices(
index=0,
finish_reason="refusal",
message=Message(content=None, role="assistant"),
)
],
usage=Usage(prompt_tokens=1, completion_tokens=0, total_tokens=1),
)
result = LiteLLMAnthropicMessagesAdapter().translate_openai_response_to_anthropic(response)
assert result["content"] == []
assert result["stop_reason"] == "refusal"
def test_translate_streaming_openai_chunk_to_anthropic_content_block():
choices = [
StreamingChoices(

View file

@ -4246,3 +4246,65 @@ class TestStreamingSnapshotItemIds:
reasoning_items = _bridged_output_items(completed_event.response, "reasoning")
assert len(reasoning_items) == 1
assert reasoning_items[0].id == streamed_event.item_id
def test_transform_chat_completion_response_incomplete_details():
from openai.types.responses.response import IncompleteDetails
resp_length = ModelResponse(
id="resp-length",
choices=[Choices(index=0, finish_reason="length", message=Message(content="cutoff", role="assistant"))],
model="gpt-4o",
)
result_length = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response(
request_input="test prompt",
responses_api_request={},
chat_completion_response=resp_length,
)
assert result_length.status == "incomplete"
assert result_length.incomplete_details is not None
assert result_length.incomplete_details.reason == "max_output_tokens"
resp_filter = ModelResponse(
id="resp-filter",
choices=[Choices(index=0, finish_reason="content_filter", message=Message(content=None, role="assistant"))],
model="gpt-4o",
)
result_filter = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response(
request_input="test prompt",
responses_api_request={},
chat_completion_response=resp_filter,
)
assert result_filter.status == "incomplete"
assert result_filter.incomplete_details is not None
assert result_filter.incomplete_details.reason == "content_filter"
resp_refusal = ModelResponse(
id="resp-refusal",
choices=[Choices(index=0, finish_reason="refusal", message=Message(content=None, role="assistant"))],
model="gpt-4o",
)
result_refusal = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response(
request_input="test prompt",
responses_api_request={},
chat_completion_response=resp_refusal,
)
assert result_refusal.status == "incomplete"
assert result_refusal.incomplete_details is not None
assert result_refusal.incomplete_details.reason == "content_filter"
existing_details = IncompleteDetails(reason="content_filter")
resp_existing = ModelResponse(
id="resp-existing",
choices=[Choices(index=0, finish_reason="length", message=Message(content="cutoff", role="assistant"))],
model="gpt-4o",
)
resp_existing.incomplete_details = existing_details
result_existing = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response(
request_input="test prompt",
responses_api_request={},
chat_completion_response=resp_existing,
)
assert result_existing.status == "incomplete"
assert result_existing.incomplete_details == existing_details