Repository navigation
fix(anthropic/responses_adapters): preserve BaseModel and string content parts in translate_response (#54113) - #54127
Conversation
surlymochan
left a comment
There was a problem hiding this comment.
Verified the three new branches of _output_part_mapping against the three new tests: BaseModel parts go through model_dump() (GenericResponseOutputItem with an OutputText part), raw strings are wrapped as {"type": "output_text", "text": ...}, and dict parts fall through to as_str_mapping. Before this change, map(as_str_mapping, ...) dropped BaseModel parts, so a Responses API output item built from OutputText models lost its text entirely — the new tests pin the text surviving in all three shapes.
One behavior change to confirm as intended: the acceptance condition now also admits parts with part_type is None and "text" in part. That rescues untyped dict text parts, but it also means any future untyped part carrying a text key is treated as text. Given the current part types (output_text / text / untyped-with-text) that looks safe — just flagging it as a deliberate widening.
…ent parts in translate_response (BerriAI#54113)
35772ff to
8b24d80
Compare
Handwritten 1-3 sentence description
Fixes an issue where non-streaming
/v1/messagescalls routed to OpenAI models return empty content blocks (content: []). This occurs becauseGenericResponseOutputItemcontains PydanticBaseModelinstances (OutputText) whichas_str_mappingdropped, preventing response text from being translated into Anthropic content blocks.Details
TLDR
Problem this solves:
/v1/messagesrequests foropenai/provider models return emptycontent: [].litellm_completion_transformationwrap text insideOutputTextBaseModel objects or plain strings thatas_str_mappingdiscarded.How it solves it:
_output_part_mappingto safely unwrapBaseModelinstances (part.model_dump()), raw strings, and mappings.type in ("output_text", "text")or implicit text parts when buildingAnthropicResponseContentBlockText.User Flow
Before: a developer using Anthropic SDK or
/v1/messagestargeting anopenai/model receives an empty response body.https://litellm-domain/v1/messageswith{"model": "openai/gpt-4o", "messages": [{"role": "user", "content": "hello"}]}.{"content": [], "role": "assistant", "stop_reason": "end_turn"}.After: the same request returns the complete assistant response text.
https://litellm-domain/v1/messageswith{"model": "openai/gpt-4o", "messages": [{"role": "user", "content": "hello"}]}.{"content": [{"type": "text", "text": "Hello! How can I help you today?"}], "role": "assistant", "stop_reason": "end_turn"}.Relevant issues
Fixes #54113
Pre-Submission checklist
uv run pytest tests/unit/llms/anthropic/messages/responses_adapters/test_responses_adapters_transformation.py -v.