Skip to content

fix(anthropic/responses_adapters): preserve BaseModel and string content parts in translate_response (#54113) - #54127

Open
MilindChatrabhuji wants to merge 1 commit into
BerriAI:mainfrom
MilindChatrabhuji:fix/anthropic-responses-empty-content-54113
Open

MilindChatrabhuji wants to merge 1 commit into
BerriAI:mainfrom
MilindChatrabhuji:fix/anthropic-responses-empty-content-54113

Conversation

@MilindChatrabhuji

Copy link
Copy Markdown

Handwritten 1-3 sentence description

Fixes an issue where non-streaming /v1/messages calls routed to OpenAI models return empty content blocks (content: []). This occurs because GenericResponseOutputItem contains Pydantic BaseModel instances (OutputText) which as_str_mapping dropped, preventing response text from being translated into Anthropic content blocks.

Details

TLDR

Problem this solves:

  • Non-streaming Anthropic /v1/messages requests for openai/ provider models return empty content: [].
  • Output items from litellm_completion_transformation wrap text inside OutputText BaseModel objects or plain strings that as_str_mapping discarded.

How it solves it:

  • Added _output_part_mapping to safely unwrap BaseModel instances (part.model_dump()), raw strings, and mappings.
  • Accepted type in ("output_text", "text") or implicit text parts when building AnthropicResponseContentBlockText.

User Flow

Before: a developer using Anthropic SDK or /v1/messages targeting an openai/ model receives an empty response body.

  1. They send POST https://litellm-domain/v1/messages with {"model": "openai/gpt-4o", "messages": [{"role": "user", "content": "hello"}]}.
  2. The proxy responds HTTP 200 with {"content": [], "role": "assistant", "stop_reason": "end_turn"}.
  3. The client application sees an empty text response.

After: the same request returns the complete assistant response text.

  1. They send POST https://litellm-domain/v1/messages with {"model": "openai/gpt-4o", "messages": [{"role": "user", "content": "hello"}]}.
  2. The proxy responds HTTP 200 with {"content": [{"type": "text", "text": "Hello! How can I help you today?"}], "role": "assistant", "stop_reason": "end_turn"}.
  3. The client application receives and renders the full model response.

Relevant issues

Fixes #54113

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/llms/anthropic/messages/responses_adapters/test_responses_adapters_transformation.py -v.
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

@CLAassistant

CLAassistant commented Nov 24, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@veria-ai

veria-ai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

No security issues were found in this pull request.

Reviews

@surlymochan surlymochan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@MilindChatrabhuji
MilindChatrabhuji force-pushed the fix/anthropic-responses-empty-content-54113 branch from 35772ff to 8b24d80 Compare November 27, 2026 11:55

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: /v1/messages non-streaming returns empty content for openai/ provider models (streaming and /v1/chat/completions work)

3 participants