Skip to content

fix(anthropic): tolerate a text block start without a text field - #48021

Open
lester076 wants to merge 1 commit into
BerriAI:mainfrom
lester076:litellm_anthropic_missing_block_text
Open

lester076 wants to merge 1 commit into
BerriAI:mainfrom
lester076:litellm_anthropic_missing_block_text

Conversation

@lester076

@lester076 lester076 commented Oct 23, 2026 •

Copy link
Copy Markdown

Streaming through an Anthropic-compatible gateway continues when a text block start omits its empty text field. The SDK accepts the empty prefix and delivers the following text deltas; the handler raised KeyError: 'text' before any answer text.

Change

litellm/llms/anthropic/chat/handler.py: the content_block_start text branch reads content_block.get("text", "") — a missing block-start text is an empty prefix, not an error. Plus one regression test (test_streaming_text_block_start_without_text_field_delivers_deltas) that streams a block start without text followed by a text delta and asserts the answer text is delivered.

Branch state

Rebuilt as a minimal patch on current main: the earlier revision also carried an unrelated dev-tooling MSAL dependency bump and repeated uv.lock sync churn; both are dropped so this PR reviews as the streaming fix alone. That also clears the previous lockfile/fork-guard concern from the thread.

Local note: this package now builds a Rust bridge, which does not compile in my Windows sandbox (maturin/getrandom build failure), so the regression test is validated by CI rather than locally.

@codspeed

codspeed Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 35 untouched benchmarks


Comparing lester076:litellm_anthropic_missing_block_text (a0ab98c) with main (cfe0100)

Open in CodSpeed

@codecov

codecov Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@lester076
lester076 requested a review from a team October 24, 2026 04:41
@CLAassistant

CLAassistant commented Oct 24, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@lester076

Copy link
Copy Markdown
Author

Security fixes pass OSV, but the fork guard requires uv.lock changes on a canonical branch. Could a maintainer adopt these dependency fixes?

@lester076

Copy link
Copy Markdown
Author

{"body":"Synced with main (0cb7651) — merge conflicts resolved and re-verified:\n\n- tests/unit/llms/anthropic/chat/test_anthropic_chat_handler.py: kept both sides (my test_streaming_text_block_start_without_text_field plus upstream's new bound-thinking helpers) — the full suite passes locally: 122 passed.\n- osv-scanner.toml and the earlier sync: union of both sides' ignore entries.\n- filelock ranges in pyproject.toml / uv.lock / packaging/litellm-core/pyproject.toml: took upstream's >=3.16.1,<5 from #49137 (our per-Python-version pins predated it; that merged PR is the canonical resolution).\n\nThe anthropic regression test (text_block_start without a text field) still passes after the sync. Remaining CI failures on the previous push were the dependency-modification restriction and the upstream security scan — both should now clear with the #49137 range in place."}

A compatible gateway may start a text content block without the empty
'text' field. The streaming handler read content_block['text'] directly,
so the stream raised KeyError before any answer text, even though the
following text_delta chunks carry a valid answer (the SDK accepts the
empty prefix). Missing block-start text now defaults to an empty string,
so the deltas are delivered normally.

Rebuilt as a minimal patch on current main: the earlier revision also
carried an unrelated dev-tooling dependency bump; that is dropped so this
PR reviews as the streaming fix alone.
@lester076
lester076 force-pushed the litellm_anthropic_missing_block_text branch from eafa645 to bdce8f6 Compare November 18, 2026 10:25
@veria-ai

veria-ai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

No security issues were found in this pull request.

Reviews

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.

2 participants