mirror of
https://github.com/BerriAI/litellm.git
synced 2026-08-28 05:25:59 +00:00
Merge 9fa8d062b3 into 1df25e26cf
This commit is contained in:
commit
a8d962be2d
2 changed files with 196 additions and 1 deletions
|
|
@ -2292,11 +2292,23 @@ def _is_unsignable_thinking_block(block: object) -> bool:
|
|||
signature is null, empty, or missing (e.g. from an open-source reasoning model)
|
||||
is rejected with a 400 and must be dropped rather than blanked or repaired.
|
||||
`redacted_thinking` blocks carry no signature and are always kept.
|
||||
|
||||
Anthropic also rejects a `thinking` block whose `thinking` text is empty or
|
||||
whitespace-only ("each thinking block must contain thinking"), regardless of
|
||||
signature. This shape reaches us when a caller replays a `thinking_blocks`
|
||||
history item that originated from a non-Anthropic reasoning provider (e.g. an
|
||||
OpenAI Responses-API turn with no summary text) through this Anthropic-shaped
|
||||
request path (`/v1/chat/completions` -> anthropic/vertex_ai's claude models),
|
||||
which is the same failure the Anthropic Responses-bridge adapter guards
|
||||
against (see PR #36033) for its own separate content-block path.
|
||||
"""
|
||||
if not isinstance(block, dict) or block.get("type") != "thinking":
|
||||
return False
|
||||
signature: Final = block.get("signature")
|
||||
return not (isinstance(signature, str) and len(signature) > 0)
|
||||
if not (isinstance(signature, str) and len(signature) > 0):
|
||||
return True
|
||||
thinking_text: Final = block.get("thinking")
|
||||
return not (isinstance(thinking_text, str) and len(thinking_text.strip()) > 0)
|
||||
|
||||
|
||||
def _drop_unsignable_thinking_blocks(
|
||||
|
|
|
|||
|
|
@ -3578,3 +3578,186 @@ async def test_bedrock_converse_pdf_only_user_message_gets_text_block_async():
|
|||
assert len(result) == 1
|
||||
assert any("document" in block for block in result[0]["content"])
|
||||
assert _text_blocks(result[0]) == [BEDROCK_DOCUMENT_PLACEHOLDER_TEXT]
|
||||
|
||||
|
||||
def test_anthropic_messages_pt_drops_empty_but_signed_thinking_block():
|
||||
"""
|
||||
Anthropic rejects a `thinking` block whose `thinking` text is empty, even
|
||||
when it carries a valid-looking signature, with:
|
||||
400 messages.N.content.M.thinking: each thinking block must contain thinking
|
||||
This shape is reachable via cross-provider replay of a `thinking_blocks`
|
||||
history item (see PR #36033), so `_is_unsignable_thinking_block()` must
|
||||
also check the thinking text, not just the signature.
|
||||
"""
|
||||
from litellm.litellm_core_utils.prompt_templates.factory import (
|
||||
anthropic_messages_pt,
|
||||
)
|
||||
|
||||
messages = [
|
||||
{"role": "user", "content": "What's 2+2?"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "4",
|
||||
"thinking_blocks": [
|
||||
{
|
||||
"type": "thinking",
|
||||
"thinking": "",
|
||||
"signature": "sig_abc123_looks_valid",
|
||||
}
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
result = anthropic_messages_pt(
|
||||
messages=messages,
|
||||
model="claude-sonnet-4-5-20250929",
|
||||
llm_provider="anthropic",
|
||||
)
|
||||
|
||||
assistant_msg = result[1]
|
||||
assert isinstance(assistant_msg["content"], list)
|
||||
content_types = [block.get("type") for block in assistant_msg["content"]]
|
||||
assert "thinking" not in content_types, "empty-text thinking block must be dropped even though it has a signature"
|
||||
|
||||
|
||||
def test_anthropic_messages_pt_keeps_non_empty_signed_thinking_block():
|
||||
"""
|
||||
Regression: a real, non-empty, signed thinking block must still pass
|
||||
through unchanged.
|
||||
"""
|
||||
from litellm.litellm_core_utils.prompt_templates.factory import (
|
||||
anthropic_messages_pt,
|
||||
)
|
||||
|
||||
messages = [
|
||||
{"role": "user", "content": "What's 2+2?"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "4",
|
||||
"thinking_blocks": [
|
||||
{
|
||||
"type": "thinking",
|
||||
"thinking": "Let me add these numbers together.",
|
||||
"signature": "sig_abc123_looks_valid",
|
||||
}
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
result = anthropic_messages_pt(
|
||||
messages=messages,
|
||||
model="claude-sonnet-4-5-20250929",
|
||||
llm_provider="anthropic",
|
||||
)
|
||||
|
||||
assistant_msg = result[1]
|
||||
assert isinstance(assistant_msg["content"], list)
|
||||
thinking_block = next((b for b in assistant_msg["content"] if b.get("type") == "thinking"), None)
|
||||
assert thinking_block is not None, "non-empty signed thinking block must be kept"
|
||||
assert thinking_block["thinking"] == "Let me add these numbers together."
|
||||
assert thinking_block["signature"] == "sig_abc123_looks_valid"
|
||||
|
||||
|
||||
def test_anthropic_messages_pt_keeps_redacted_thinking_block():
|
||||
"""
|
||||
Regression: `redacted_thinking` blocks carry no signature and no plaintext
|
||||
`thinking` field by design, and must always be kept regardless of the new
|
||||
emptiness check (which only applies to `type == "thinking"` blocks).
|
||||
"""
|
||||
from litellm.litellm_core_utils.prompt_templates.factory import (
|
||||
anthropic_messages_pt,
|
||||
)
|
||||
|
||||
messages = [
|
||||
{"role": "user", "content": "What's 2+2?"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "4",
|
||||
"thinking_blocks": [
|
||||
{
|
||||
"type": "redacted_thinking",
|
||||
"data": "encrypted_opaque_blob",
|
||||
}
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
result = anthropic_messages_pt(
|
||||
messages=messages,
|
||||
model="claude-sonnet-4-5-20250929",
|
||||
llm_provider="anthropic",
|
||||
)
|
||||
|
||||
assistant_msg = result[1]
|
||||
assert isinstance(assistant_msg["content"], list)
|
||||
content_types = [block.get("type") for block in assistant_msg["content"]]
|
||||
assert "redacted_thinking" in content_types, "redacted_thinking blocks must always be kept"
|
||||
|
||||
|
||||
def test_anthropic_messages_pt_drops_unsigned_thinking_block():
|
||||
"""
|
||||
Regression (pre-existing behaviour): a thinking block with no signature
|
||||
(or an empty/null one) must still be dropped, independent of whether the
|
||||
thinking text is populated.
|
||||
"""
|
||||
from litellm.litellm_core_utils.prompt_templates.factory import (
|
||||
anthropic_messages_pt,
|
||||
)
|
||||
|
||||
messages = [
|
||||
{"role": "user", "content": "What's 2+2?"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "4",
|
||||
"thinking_blocks": [
|
||||
{
|
||||
"type": "thinking",
|
||||
"thinking": "Let me add these numbers together.",
|
||||
"signature": "",
|
||||
}
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
result = anthropic_messages_pt(
|
||||
messages=messages,
|
||||
model="claude-sonnet-4-5-20250929",
|
||||
llm_provider="anthropic",
|
||||
)
|
||||
|
||||
assistant_msg = result[1]
|
||||
assert isinstance(assistant_msg["content"], list)
|
||||
content_types = [block.get("type") for block in assistant_msg["content"]]
|
||||
assert "thinking" not in content_types, "unsigned thinking block must still be dropped"
|
||||
|
||||
|
||||
def test_is_unsignable_thinking_block_treats_whitespace_only_as_empty():
|
||||
"""
|
||||
Edge case: a `thinking` field that is present but whitespace-only (e.g.
|
||||
a single trailing newline forwarded from another provider's empty
|
||||
reasoning summary) is functionally empty and Anthropic's API will still
|
||||
reject it with "each thinking block must contain thinking". We treat it
|
||||
the same as a fully empty string and drop the block.
|
||||
|
||||
Note the sibling check ~30 lines below this function's definition
|
||||
(the "don't pass empty text blocks" comment) uses a bare
|
||||
`len(thinking_block) > 0`, which by itself would treat whitespace-only
|
||||
text as non-empty. That sibling check is always combined with
|
||||
`not _is_unsignable_thinking_block(m)` in an `and`, so this function's
|
||||
stricter whitespace-aware check is still the one that decides whether a
|
||||
whitespace-only block survives — the two checks don't disagree in
|
||||
practice, but this function is intentionally the stricter of the two
|
||||
since it is also called standalone (via `_drop_unsignable_thinking_blocks`)
|
||||
without that extra `len(...) > 0` guard.
|
||||
"""
|
||||
from litellm.litellm_core_utils.prompt_templates.factory import (
|
||||
_is_unsignable_thinking_block,
|
||||
)
|
||||
|
||||
whitespace_only_block = {
|
||||
"type": "thinking",
|
||||
"thinking": " \n\t ",
|
||||
"signature": "sig_abc123_looks_valid",
|
||||
}
|
||||
|
||||
assert _is_unsignable_thinking_block(whitespace_only_block) is True
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue