From b6ee13803d1c21bc0b07dac6e6b9ad2ede7ad7aa Mon Sep 17 00:00:00 2001 From: HarryZhou <2373256746@qq.com> Date: Sun, 9 Aug 2026 22:59:25 +0800 Subject: [PATCH 01/10] fix(responses-bridge): preserve reasoning input items as reasoning_content --- .../transformation.py | 164 +++++++++++++++++- .../test_reasoning_input_item_preservation.py | 147 ++++++++++++++++ 2 files changed, 310 insertions(+), 1 deletion(-) create mode 100644 tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index 4892e3b348c5..5d3ed0477e3d 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -557,7 +557,108 @@ def _transform_response_input_param_to_chat_completion_message( continue messages.extend(chat_completion_messages) - return messages + return LiteLLMCompletionResponsesConfig._merge_reasoning_only_assistant_messages(messages) + + @staticmethod + def _merge_reasoning_only_assistant_messages( + messages: list[ + AllMessageValues + | GenericChatCompletionMessage + | ChatCompletionMessageToolCall + | ChatCompletionResponseMessage + ], + ) -> list[ + AllMessageValues | GenericChatCompletionMessage | ChatCompletionMessageToolCall | ChatCompletionResponseMessage + ]: + """ + Responses API emits prior-turn reasoning as its own ``reasoning`` input + item, which becomes a standalone assistant message with + ``content=None`` + ``reasoning_content``. Chat-completions providers + (e.g. DeepSeek V4, Kimi K2.6) expect the chain-of-thought on the + assistant message that carries the answer or tool calls. This pass + merges standalone reasoning-only assistant messages into the + immediately following assistant message. + + If the reasoning item is not followed by an assistant message (e.g. a + stateless chain replays ``reasoning`` + ``user``), the standalone + reasoning message is preserved so the reasoning is still passed back. + """ + + def _role(msg: Any) -> str: + if isinstance(msg, dict): + return str(msg.get("role") or "") + return str(getattr(msg, "role", "") or "") + + def _reasoning_text(msg: Any) -> str | None: + if isinstance(msg, dict): + value = msg.get("reasoning_content") + else: + value = getattr(msg, "reasoning_content", None) + return value if isinstance(value, str) and value else None + + def _content(msg: Any) -> Any: + if isinstance(msg, dict): + return msg.get("content") + return getattr(msg, "content", None) + + def _tool_calls(msg: Any) -> Any: + if isinstance(msg, dict): + return msg.get("tool_calls") + return getattr(msg, "tool_calls", None) + + merged: list[ + AllMessageValues + | GenericChatCompletionMessage + | ChatCompletionMessageToolCall + | ChatCompletionResponseMessage + ] = [] + pending_reasoning: list[str] = [] + + for msg in messages: + if ( + _role(msg) == "assistant" + and _content(msg) is None + and not _tool_calls(msg) + and _reasoning_text(msg) is not None + ): + pending_reasoning.append(_reasoning_text(msg) or "") + continue + + if pending_reasoning and _role(msg) == "assistant": + combined = "\n".join(pending_reasoning) + existing = _reasoning_text(msg) + if existing: + combined = existing + "\n" + combined + if isinstance(msg, dict): + msg["reasoning_content"] = combined + else: + setattr(msg, "reasoning_content", combined) + pending_reasoning = [] + elif pending_reasoning: + # Not followed by an assistant message — keep the reasoning + # standalone instead of dropping it. + for text in pending_reasoning: + merged.append( + ChatCompletionResponseMessage( + role="assistant", + content=None, + reasoning_content=text, + ) + ) + pending_reasoning = [] + + merged.append(msg) + + for text in pending_reasoning: + merged.append( + ChatCompletionResponseMessage( + role="assistant", + content=None, + reasoning_content=text, + ) + ) + + return merged @staticmethod def _merged_trailing_assistant_message( @@ -1026,6 +1127,25 @@ def _transform_responses_api_input_item_to_chat_completion_message( return LiteLLMCompletionResponsesConfig._transform_responses_api_function_call_to_chat_completion_message( function_call=input_item ) + elif input_item.get("type") == "reasoning": + # A ResponseReasoningItemParam carries the prior-turn chain-of-thought. + # Chat-completions providers (DeepSeek V4, Kimi K2.6, ...) expect this + # to be replayed as `reasoning_content` on an assistant message, not as + # visible `content` (prompt pollution) and not dropped (DeepSeek V4 + # rejects multi-turn requests with a missing `reasoning_content`). + reasoning_text = LiteLLMCompletionResponsesConfig._extract_reasoning_text_from_input_item(input_item) + if not reasoning_text: + # No plaintext reasoning is available (e.g. encrypted_content only). + # Chat-completions providers cannot consume opaque encrypted blobs, + # so skip the item instead of polluting the prompt. + return [] + return [ + ChatCompletionResponseMessage( + role="assistant", + content=None, + reasoning_content=reasoning_text, + ) + ] else: content: Final[object] = input_item.get("content") # Handle None content: Responses API allows None content, but GenericChatCompletionMessage requires content @@ -1041,6 +1161,48 @@ def _transform_responses_api_input_item_to_chat_completion_message( ) ] + @staticmethod + def _extract_reasoning_text_from_input_item(input_item: Mapping[str, object]) -> str | None: + """ + Extract plaintext reasoning from a ResponseReasoningItemParam. + + Handles: + - content as a string + - content as a list of blocks (output_text / summary_text / text) + - summary as a list of summary_text blocks (fallback) + + Returns None when only opaque forms (e.g. encrypted_content) are present. + """ + content: Final[object] = input_item.get("content") + if isinstance(content, str) and content.strip(): + return content + if isinstance(content, list): + text_parts: list[str] = [] + for block in content: + if not isinstance(block, Mapping): + continue + block_type = block.get("type") + if block_type in ("encrypted_content", "redacted_thinking"): + continue + text = block.get("text") + if isinstance(text, str) and text.strip(): + text_parts.append(text.strip()) + if text_parts: + return "\n".join(text_parts) + + summary: Final[object] = input_item.get("summary") + if isinstance(summary, list): + text_parts = [] + for block in summary: + if not isinstance(block, Mapping): + continue + text = block.get("text") + if isinstance(text, str) and text.strip(): + text_parts.append(text.strip()) + if text_parts: + return "\n".join(text_parts) + return None + @staticmethod def _is_input_item_tool_call_output(input_item: Mapping[str, object]) -> bool: """ diff --git a/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py b/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py new file mode 100644 index 000000000000..5fcd4df3ff89 --- /dev/null +++ b/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py @@ -0,0 +1,147 @@ +""" +Unit tests for preserving prior-turn ``reasoning`` input items when the +Responses API is bridged to chat completions. + +Without this handling, a ``ResponseReasoningItemParam`` falls through to the +generic message branch, polluting the prompt as visible assistant ``content`` +or being silently dropped. Chat-completions providers such as DeepSeek V4 and +Kimi K2.6 require the chain-of-thought to be replayed as ``reasoning_content`` +on an assistant message. +""" + +from litellm.responses.litellm_completion_transformation.transformation import ( + LiteLLMCompletionResponsesConfig, +) + + +def _transform_item(item): + return LiteLLMCompletionResponsesConfig._transform_responses_api_input_item_to_chat_completion_message( + input_item=item + ) + + +def _transform_input(input_items): + return LiteLLMCompletionResponsesConfig._transform_response_input_param_to_chat_completion_message( + input=input_items + ) + + +class TestReasoningInputItemHandler: + """Reasoning input items map to assistant ``reasoning_content``.""" + + def test_reasoning_item_with_output_text_content(self): + """Standard Responses-API reasoning item with output_text blocks.""" + item = { + "type": "reasoning", + "id": "rs_abc", + "summary": [], + "content": [{"type": "output_text", "text": "step 1: think about X"}], + } + messages = _transform_item(item) + assert len(messages) == 1 + assert messages[0]["role"] == "assistant" + assert messages[0]["content"] is None + assert messages[0]["reasoning_content"] == "step 1: think about X" + + def test_reasoning_item_with_string_content(self): + """Variant: reasoning content as a plain string.""" + item = {"type": "reasoning", "id": "rs_1", "content": "step 1: ..."} + messages = _transform_item(item) + assert messages[0]["reasoning_content"] == "step 1: ..." + + def test_reasoning_item_with_summary_only(self): + """SDK form: reasoning carried in summary list, no content.""" + item = { + "type": "reasoning", + "id": "rs_2", + "summary": [{"type": "summary_text", "text": "..."}], + } + messages = _transform_item(item) + assert messages[0]["reasoning_content"] == "..." + + def test_reasoning_item_with_encrypted_content_only_dropped(self): + """Opaque encrypted reasoning cannot be forwarded to chat completions.""" + item = {"type": "reasoning", "id": "rs_3", "encrypted_content": "opaque-blob"} + assert _transform_item(item) == [] + + def test_reasoning_item_empty_dropped(self): + """Reasoning item with neither content nor summary drops cleanly.""" + assert _transform_item({"type": "reasoning", "id": "rs_4"}) == [] + + +class TestReasoningInputItemMerging: + """Standalone reasoning messages merge into the following assistant turn.""" + + def test_reasoning_merged_into_following_assistant_message(self): + """Reasoning + assistant answer become one assistant message.""" + messages = _transform_input( + [ + { + "type": "reasoning", + "id": "rs_1", + "content": [{"type": "output_text", "text": "secret reasoning"}], + }, + {"type": "message", "role": "assistant", "content": "The answer."}, + ] + ) + assert len(messages) == 1 + assert messages[0]["role"] == "assistant" + assert messages[0]["content"] == "The answer." + assert messages[0]["reasoning_content"] == "secret reasoning" + + def test_reasoning_preserved_when_followed_by_user_message(self): + """Stateless chain: reasoning + user prompt keeps the reasoning turn.""" + messages = _transform_input( + [ + { + "type": "reasoning", + "id": "rs_1", + "content": [{"type": "output_text", "text": "secret BLUEBERRY"}], + }, + {"role": "user", "content": "What is the secret word?"}, + ] + ) + assert len(messages) == 2 + assert messages[0]["role"] == "assistant" + assert messages[0]["content"] is None + assert messages[0]["reasoning_content"] == "secret BLUEBERRY" + assert messages[1]["role"] == "user" + + def test_reasoning_merged_into_function_call_assistant(self): + """Reasoning + function_call becomes one assistant tool-call message.""" + messages = _transform_input( + [ + { + "type": "reasoning", + "id": "rs_1", + "content": [{"type": "output_text", "text": "I should look this up"}], + }, + { + "type": "function_call", + "call_id": "call_1", + "name": "lookup", + "arguments": '{"cwe": "79"}', + }, + ] + ) + assert len(messages) == 1 + assert messages[0]["role"] == "assistant" + assert messages[0]["reasoning_content"] == "I should look this up" + assert len(messages[0]["tool_calls"]) == 1 + + +class TestNonReasoningInputItemUnchanged: + """Non-reasoning items still flow through the existing branches.""" + + def test_user_message_unchanged(self): + item = {"role": "user", "content": "hello"} + out = _transform_item(item) + assert len(out) == 1 + assert out[0]["role"] == "user" + + def test_assistant_message_unchanged(self): + item = {"role": "assistant", "content": "hi"} + out = _transform_item(item) + assert len(out) == 1 + assert out[0]["role"] == "assistant" + assert out[0]["content"] == "hi" From 3a77556dc14660e88a7d20f54e9762c39f24b749 Mon Sep 17 00:00:00 2001 From: HarryZhou <2373256746@qq.com> Date: Mon, 10 Aug 2026 00:45:25 +0800 Subject: [PATCH 02/10] fix(responses-bridge): preserve reasoning merge order when assistant already has reasoning_content --- .../transformation.py | 2 +- .../test_reasoning_input_item_preservation.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index 5d3ed0477e3d..0604c3636ffd 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -628,7 +628,7 @@ def _tool_calls(msg: Any) -> Any: combined = "\n".join(pending_reasoning) existing = _reasoning_text(msg) if existing: - combined = existing + "\n" + combined + combined = combined + "\n" + existing if isinstance(msg, dict): msg["reasoning_content"] = combined else: diff --git a/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py b/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py index 5fcd4df3ff89..ecc024b7d04a 100644 --- a/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py +++ b/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py @@ -129,6 +129,18 @@ def test_reasoning_merged_into_function_call_assistant(self): assert messages[0]["reasoning_content"] == "I should look this up" assert len(messages[0]["tool_calls"]) == 1 + def test_reasoning_merged_into_assistant_with_existing_reasoning_content(self): + """Old reasoning precedes existing reasoning on the target assistant turn.""" + messages = LiteLLMCompletionResponsesConfig._merge_reasoning_only_assistant_messages( + [ + {"role": "assistant", "content": None, "reasoning_content": "old reasoning"}, + {"role": "assistant", "content": "The answer.", "reasoning_content": "new reasoning"}, + ] + ) + assert len(messages) == 1 + assert messages[0]["content"] == "The answer." + assert messages[0]["reasoning_content"] == "old reasoning\nnew reasoning" + class TestNonReasoningInputItemUnchanged: """Non-reasoning items still flow through the existing branches.""" From 5911124f1dbba1e9c58f3b53619c3f875752a20f Mon Sep 17 00:00:00 2001 From: HarryZhou <2373256746@qq.com> Date: Mon, 10 Aug 2026 00:58:47 +0800 Subject: [PATCH 03/10] fix(responses-bridge): satisfy ruff strict-rule budget in reasoning merge --- .../transformation.py | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index 0604c3636ffd..2c506d4a4c7f 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -584,24 +584,24 @@ def _merge_reasoning_only_assistant_messages( reasoning message is preserved so the reasoning is still passed back. """ - def _role(msg: Any) -> str: + def _role(msg: object) -> str: if isinstance(msg, dict): return str(msg.get("role") or "") return str(getattr(msg, "role", "") or "") - def _reasoning_text(msg: Any) -> str | None: + def _reasoning_text(msg: object) -> str | None: if isinstance(msg, dict): value = msg.get("reasoning_content") else: value = getattr(msg, "reasoning_content", None) return value if isinstance(value, str) and value else None - def _content(msg: Any) -> Any: + def _content(msg: object) -> object | None: if isinstance(msg, dict): return msg.get("content") return getattr(msg, "content", None) - def _tool_calls(msg: Any) -> Any: + def _tool_calls(msg: object) -> object | None: if isinstance(msg, dict): return msg.get("tool_calls") return getattr(msg, "tool_calls", None) @@ -632,31 +632,35 @@ def _tool_calls(msg: Any) -> Any: if isinstance(msg, dict): msg["reasoning_content"] = combined else: - setattr(msg, "reasoning_content", combined) + setattr(msg, "reasoning_content", combined) # noqa: B010 pending_reasoning = [] elif pending_reasoning: # Not followed by an assistant message — keep the reasoning # standalone instead of dropping it. - for text in pending_reasoning: - merged.append( + merged.extend( + [ ChatCompletionResponseMessage( role="assistant", content=None, reasoning_content=text, ) - ) + for text in pending_reasoning + ] + ) pending_reasoning = [] merged.append(msg) - for text in pending_reasoning: - merged.append( + merged.extend( + [ ChatCompletionResponseMessage( role="assistant", content=None, reasoning_content=text, ) - ) + for text in pending_reasoning + ] + ) return merged From 438c1850fe1223feec1e2e6e5b48f0a6c15a1328 Mon Sep 17 00:00:00 2001 From: HarryZhou <2373256746@qq.com> Date: Mon, 10 Aug 2026 01:09:26 +0800 Subject: [PATCH 04/10] fix(responses-bridge): satisfy type-discipline budget in reasoning merge --- .../transformation.py | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index 2c506d4a4c7f..e3e62ab3c555 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -561,13 +561,13 @@ def _transform_response_input_param_to_chat_completion_message( @staticmethod def _merge_reasoning_only_assistant_messages( - messages: list[ + messages: list[ # mutable-ok: input sequence AllMessageValues | GenericChatCompletionMessage | ChatCompletionMessageToolCall | ChatCompletionResponseMessage ], - ) -> list[ + ) -> list[ # mutable-ok: fresh merged list AllMessageValues | GenericChatCompletionMessage | ChatCompletionMessageToolCall | ChatCompletionResponseMessage ]: """ @@ -591,9 +591,9 @@ def _role(msg: object) -> str: def _reasoning_text(msg: object) -> str | None: if isinstance(msg, dict): - value = msg.get("reasoning_content") + value = msg.get("reasoning_content") # rebind-ok: branch lookup else: - value = getattr(msg, "reasoning_content", None) + value = getattr(msg, "reasoning_content", None) # rebind-ok: branch lookup return value if isinstance(value, str) and value else None def _content(msg: object) -> object | None: @@ -606,13 +606,13 @@ def _tool_calls(msg: object) -> object | None: return msg.get("tool_calls") return getattr(msg, "tool_calls", None) - merged: list[ + merged: list[ # mutable-ok: accumulator # rebind-ok: accumulator AllMessageValues | GenericChatCompletionMessage | ChatCompletionMessageToolCall | ChatCompletionResponseMessage - ] = [] - pending_reasoning: list[str] = [] + ] = [] # mutable-ok: accumulator + pending_reasoning: list[str] = [] # mutable-ok: accumulator # rebind-ok: accumulator for msg in messages: if ( @@ -633,11 +633,11 @@ def _tool_calls(msg: object) -> object | None: msg["reasoning_content"] = combined else: setattr(msg, "reasoning_content", combined) # noqa: B010 - pending_reasoning = [] + pending_reasoning = [] # mutable-ok: reset accumulator elif pending_reasoning: # Not followed by an assistant message — keep the reasoning # standalone instead of dropping it. - merged.extend( + merged.extend( # mutable-ok: append reasoning messages [ ChatCompletionResponseMessage( role="assistant", @@ -647,11 +647,11 @@ def _tool_calls(msg: object) -> object | None: for text in pending_reasoning ] ) - pending_reasoning = [] + pending_reasoning = [] # mutable-ok: reset accumulator merged.append(msg) - merged.extend( + merged.extend( # mutable-ok: append trailing reasoning [ ChatCompletionResponseMessage( role="assistant", @@ -1137,13 +1137,15 @@ def _transform_responses_api_input_item_to_chat_completion_message( # to be replayed as `reasoning_content` on an assistant message, not as # visible `content` (prompt pollution) and not dropped (DeepSeek V4 # rejects multi-turn requests with a missing `reasoning_content`). - reasoning_text = LiteLLMCompletionResponsesConfig._extract_reasoning_text_from_input_item(input_item) + reasoning_text = LiteLLMCompletionResponsesConfig._extract_reasoning_text_from_input_item( # rebind-ok: extraction result + input_item + ) if not reasoning_text: # No plaintext reasoning is available (e.g. encrypted_content only). # Chat-completions providers cannot consume opaque encrypted blobs, # so skip the item instead of polluting the prompt. - return [] - return [ + return [] # mutable-ok: empty drop result + return [ # mutable-ok: single message result ChatCompletionResponseMessage( role="assistant", content=None, @@ -1181,7 +1183,7 @@ def _extract_reasoning_text_from_input_item(input_item: Mapping[str, object]) -> if isinstance(content, str) and content.strip(): return content if isinstance(content, list): - text_parts: list[str] = [] + text_parts: list[str] = [] # mutable-ok: text accumulator # rebind-ok: text accumulator for block in content: if not isinstance(block, Mapping): continue @@ -1196,7 +1198,7 @@ def _extract_reasoning_text_from_input_item(input_item: Mapping[str, object]) -> summary: Final[object] = input_item.get("summary") if isinstance(summary, list): - text_parts = [] + text_parts = [] # mutable-ok: text accumulator # rebind-ok: text accumulator for block in summary: if not isinstance(block, Mapping): continue From de95372dfbd7bbba8c478815340dd49c1b21da11 Mon Sep 17 00:00:00 2001 From: HarryZhou <2373256746@qq.com> Date: Mon, 10 Aug 2026 01:24:24 +0800 Subject: [PATCH 05/10] fix(responses-bridge): type-safe reasoning_content assignment in merge pass --- .../litellm_completion_transformation/transformation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index e3e62ab3c555..d7b6b8c7b8f1 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -630,7 +630,7 @@ def _tool_calls(msg: object) -> object | None: if existing: combined = combined + "\n" + existing if isinstance(msg, dict): - msg["reasoning_content"] = combined + cast(dict[str, Any], msg)["reasoning_content"] = combined # cast-ok: mutable reasoning carrier else: setattr(msg, "reasoning_content", combined) # noqa: B010 pending_reasoning = [] # mutable-ok: reset accumulator From 2d4e6afe1c7d6d3233a18668d53fafe4cffa50b3 Mon Sep 17 00:00:00 2001 From: HarryZhou <2373256746@qq.com> Date: Tue, 18 Aug 2026 21:11:29 +0800 Subject: [PATCH 06/10] fix(guardrails): inspect responses reasoning content and summary text --- litellm/proxy/guardrails/_content_utils.py | 59 +++++++++++++------ .../transformation.py | 6 +- .../proxy/guardrails/test_content_utils.py | 54 +++++++++++++++++ 3 files changed, 100 insertions(+), 19 deletions(-) diff --git a/litellm/proxy/guardrails/_content_utils.py b/litellm/proxy/guardrails/_content_utils.py index 6ed6f0013df5..ae92adcb1ee7 100644 --- a/litellm/proxy/guardrails/_content_utils.py +++ b/litellm/proxy/guardrails/_content_utils.py @@ -8,7 +8,7 @@ every text fragment. """ -from collections.abc import Callable, Iterator +from collections.abc import Callable, Iterator, Mapping from typing import Any, Final # Call types whose body carries free-form chat / prompt text that @@ -33,7 +33,9 @@ def is_text_content_call_type(call_type: str) -> bool: return call_type in TEXT_CONTENT_CALL_TYPES -TEXT_PART_TYPES: Final[frozenset[str]] = frozenset({"text", "input_text", "output_text"}) +TEXT_PART_TYPES: Final[frozenset[str]] = frozenset( + {"text", "input_text", "output_text", "summary_text", "reasoning_text"} +) # Responses-API item types whose ``output`` field carries user/tool text # that guardrails should inspect. ``function_call_output`` is the @@ -42,6 +44,16 @@ def is_text_content_call_type(call_type: str) -> bool: _OUTPUT_ITEM_TYPES: Final[frozenset[str]] = frozenset({"function_call_output", "custom_tool_call_output"}) +def _part_text(part: Mapping[str, object]) -> str | None: + """Return non-empty plaintext from any content part that carries ``text``.""" + if not isinstance(part, dict): + return None + text = part.get("text") + if isinstance(text, str) and text: + return text + return None + + def _iter_text_parts_in_content(content: Any) -> Iterator[str]: """Yield text fragments from a ``message.content`` value (string or multimodal list). Non-text parts (images, audio, …) are skipped.""" @@ -58,10 +70,9 @@ def _iter_text_parts_in_content(content: Any) -> Iterator[str]: continue if not isinstance(part, dict): continue - if part.get("type") in TEXT_PART_TYPES: - text = part.get("text") - if isinstance(text, str) and text: - yield text + text = _part_text(part) + if text is not None: + yield text def _coerce_input_to_messages(input_value: Any) -> list[dict[str, Any]]: @@ -75,8 +86,23 @@ def _coerce_input_to_messages(input_value: Any) -> list[dict[str, Any]]: if isinstance(item, str): messages.append({"role": "user", "content": item}) elif isinstance(item, dict): - if item.get("type") in TEXT_PART_TYPES: + if _part_text(item) is not None: messages.append({"role": item.get("role") or "user", "content": [item]}) + elif item.get("type") == "reasoning": + if "content" in item: + messages.append( + { # mutable-ok: append reasoning content + "role": item.get("role") or "assistant", + "content": item["content"], + } + ) + if isinstance(item.get("summary"), list): + messages.append( + { # mutable-ok: append reasoning summary + "role": item.get("role") or "assistant", + "content": item["summary"], + } + ) elif "content" in item: messages.append({"role": item.get("role") or "user", "content": item["content"]}) elif item.get("type") in _OUTPUT_ITEM_TYPES and "output" in item: @@ -126,12 +152,7 @@ def _rewrite_content(content: Any) -> Any: if isinstance(part, str) and part: visited += 1 new_parts.append(visit(part)) - elif ( - isinstance(part, dict) - and part.get("type") in TEXT_PART_TYPES - and isinstance(part.get("text"), str) - and part["text"] - ): + elif isinstance(part, dict) and _part_text(part) is not None: visited += 1 new_parts.append({**part, "text": visit(part["text"])}) else: @@ -158,10 +179,14 @@ def _rewrite_content(content: Any) -> Any: visited += 1 input_value[idx] = visit(item) elif isinstance(item, dict): - if item.get("type") in TEXT_PART_TYPES: - if isinstance(item.get("text"), str) and item["text"]: - visited += 1 - input_value[idx] = {**item, "text": visit(item["text"])} + if _part_text(item) is not None: + visited += 1 + input_value[idx] = {**item, "text": visit(item["text"])} # mutable-ok: rewrite text part in place + elif item.get("type") == "reasoning": + if "content" in item: + item["content"] = _rewrite_content(item["content"]) + if isinstance(item.get("summary"), list): + item["summary"] = _rewrite_content(item["summary"]) elif "content" in item: item["content"] = _rewrite_content(item["content"]) elif item.get("type") in _OUTPUT_ITEM_TYPES and "output" in item: diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index d7b6b8c7b8f1..c5f40242bfd1 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -638,7 +638,7 @@ def _tool_calls(msg: object) -> object | None: # Not followed by an assistant message — keep the reasoning # standalone instead of dropping it. merged.extend( # mutable-ok: append reasoning messages - [ + [ # mutable-ok: append reasoning messages ChatCompletionResponseMessage( role="assistant", content=None, @@ -652,7 +652,7 @@ def _tool_calls(msg: object) -> object | None: merged.append(msg) merged.extend( # mutable-ok: append trailing reasoning - [ + [ # mutable-ok: append trailing reasoning ChatCompletionResponseMessage( role="assistant", content=None, @@ -1196,6 +1196,8 @@ def _extract_reasoning_text_from_input_item(input_item: Mapping[str, object]) -> if text_parts: return "\n".join(text_parts) + # Guardrail traversal in litellm/proxy/guardrails/_content_utils.py + # inspects and rewrites these summary blocks before they are forwarded. summary: Final[object] = input_item.get("summary") if isinstance(summary, list): text_parts = [] # mutable-ok: text accumulator # rebind-ok: text accumulator diff --git a/tests/test_litellm/proxy/guardrails/test_content_utils.py b/tests/test_litellm/proxy/guardrails/test_content_utils.py index 3dfb98c12ea2..d9e079c6d929 100644 --- a/tests/test_litellm/proxy/guardrails/test_content_utils.py +++ b/tests/test_litellm/proxy/guardrails/test_content_utils.py @@ -149,6 +149,22 @@ def test_iter_message_text_responses_api_tool_call_taxonomy(): assert list(iter_message_text(data)) == ["hello", "sunny"] +def test_iter_message_text_inspects_reasoning_content_and_summary(): + """VERIA: reasoning items forwarded as ``reasoning_content`` must be + inspected, including ``summary`` blocks the bridge reads as a fallback.""" + data = { + "input": [ + { + "type": "reasoning", + "id": "rs_1", + "content": [{"type": "summary_text", "text": "content secret"}], + "summary": [{"type": "summary_text", "text": "summary secret"}], + } + ] + } + assert list(iter_message_text(data)) == ["content secret", "summary secret"] + + # ── walk_user_text ──────────────────────────────────────────────────────────── @@ -308,6 +324,27 @@ def test_walk_user_text_redacts_mixed_list_input(): assert data["input"][2] == {"type": "image_url", "image_url": {"url": "..."}} +def test_walk_user_text_redacts_reasoning_content_and_summary(): + """VERIA: in-place redaction must cover both plaintext shapes the bridge + forwards from a reasoning item.""" + data = { + "input": [ + { + "type": "reasoning", + "id": "rs_1", + "content": [{"type": "summary_text", "text": "AKIAEXAMPLE content"}], + "summary": [{"type": "summary_text", "text": "AKIAEXAMPLE summary"}], + } + ] + } + visited = walk_user_text(data, lambda s: s.replace("AKIAEXAMPLE", "[REDACTED]")) + assert visited == 2 + item = data["input"][0] + assert item["content"][0]["text"] == "[REDACTED] content" + assert item["summary"][0]["text"] == "[REDACTED] summary" + assert item["id"] == "rs_1" + + # ── build_inspection_messages ───────────────────────────────────────────────── @@ -462,6 +499,23 @@ def test_build_inspection_messages_empty_data(): assert build_inspection_messages({"input": ""}) == [] +def test_build_inspection_messages_includes_reasoning_summary(): + """VERIA: remote guardrail APIs must see reasoning summaries even when + the reasoning item has no ``content`` field.""" + data = { + "input": [ + { + "type": "reasoning", + "id": "rs_1", + "summary": [{"type": "summary_text", "text": "secret summary"}], + } + ] + } + assert build_inspection_messages(data) == [ + {"role": "assistant", "content": "secret summary"} + ] + + # ── has_non_string_content ──────────────────────────────────────────────────── From a7afe986e3e4d7f2202df7c4528cf7806060a29d Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Sat, 22 Aug 2026 10:51:40 -0700 Subject: [PATCH 07/10] fix(responses): replay signed thinking blocks through the completion bridge encrypted_content on a reasoning input item is written by LiteLLM's own _encode_thinking_blocks as a JSON array of Anthropic/Bedrock thinking blocks, so decode it back and replay the signed blocks on the assistant message instead of dropping them. Providers without a native ResponsesAPIConfig now keep the verifiable chain-of-thought across turns, and prior-turn reasoning stops reaching the provider as visible assistant text. --- .../transformation.py | 176 ++++++++++++++---- .../test_reasoning_input_item_preservation.py | 128 ++++++++++++- 2 files changed, 263 insertions(+), 41 deletions(-) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index 0f3f5ba9a6a0..c2e803c8a43e 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -42,8 +42,10 @@ AllMessageValues, ChatCompletionImageObject, ChatCompletionImageUrlObject, + ChatCompletionRedactedThinkingBlock, ChatCompletionResponseMessage, ChatCompletionSystemMessage, + ChatCompletionThinkingBlock, ChatCompletionToolCallChunk, ChatCompletionToolCallFunctionChunk, ChatCompletionToolMessage, @@ -559,6 +561,25 @@ def _transform_response_input_param_to_chat_completion_message( messages.extend(chat_completion_messages) return LiteLLMCompletionResponsesConfig._merge_reasoning_only_assistant_messages(messages) + @staticmethod + def _reasoning_only_assistant_message( + reasoning_text: str | None, + thinking_blocks: Sequence[ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock] | None, + ) -> ChatCompletionResponseMessage: + """ + Build the assistant message that carries a prior turn's reasoning and + nothing else, so a reasoning item never reaches the provider as visible + assistant ``content``. + """ + message: Final = ChatCompletionResponseMessage(role="assistant", content=None) + if reasoning_text: + message["reasoning_content"] = reasoning_text + if thinking_blocks: + message["thinking_blocks"] = list( # mutable-ok: thinking_blocks is a list on the message contract + thinking_blocks + ) + return message + @staticmethod def _merge_reasoning_only_assistant_messages( messages: list[ # mutable-ok: input sequence @@ -579,6 +600,11 @@ def _merge_reasoning_only_assistant_messages( merges standalone reasoning-only assistant messages into the immediately following assistant message. + Signed ``thinking_blocks`` decoded from ``encrypted_content`` travel the + same way and are placed ahead of any thinking blocks the target message + already carries, because Anthropic and Bedrock verify signatures against + the original block order. + If the reasoning item is not followed by an assistant message (e.g. a stateless chain replays ``reasoning`` + ``user``), the standalone reasoning message is preserved so the reasoning is still passed back. @@ -596,6 +622,15 @@ def _reasoning_text(msg: object) -> str | None: value = getattr(msg, "reasoning_content", None) # rebind-ok: branch lookup return value if isinstance(value, str) and value else None + def _thinking_blocks( + msg: object, + ) -> tuple[ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock, ...] | None: + if isinstance(msg, dict): + value = msg.get("thinking_blocks") # rebind-ok: branch lookup + else: + value = getattr(msg, "thinking_blocks", None) # rebind-ok: branch lookup + return tuple(value) if isinstance(value, list) and value else None + def _content(msg: object) -> object | None: if isinstance(msg, dict): return msg.get("content") @@ -606,60 +641,73 @@ def _tool_calls(msg: object) -> object | None: return msg.get("tool_calls") return getattr(msg, "tool_calls", None) + def _apply_pending( + msg: object, + pending_items: Sequence[ + tuple[ + str | None, + tuple[ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock, ...] | None, + ] + ], + ) -> None: + pending_texts: Final = tuple(text for text, _ in pending_items if text) + pending_blocks: Final = tuple(block for _, blocks in pending_items for block in blocks or ()) + if pending_texts: + existing_text: Final = _reasoning_text(msg) + combined: Final = "\n".join(pending_texts + ((existing_text,) if existing_text else ())) + if isinstance(msg, dict): + cast(dict[str, Any], msg)["reasoning_content"] = combined # cast-ok: mutable reasoning carrier + else: + setattr(msg, "reasoning_content", combined) # noqa: B010 # attribute name is fixed, not dynamic + if pending_blocks: + replayed: Final = list( # mutable-ok: thinking_blocks is a list on the message contract + pending_blocks + (_thinking_blocks(msg) or ()) + ) + if isinstance(msg, dict): + cast(dict[str, Any], msg)["thinking_blocks"] = replayed # cast-ok: mutable reasoning carrier + else: + setattr(msg, "thinking_blocks", replayed) # noqa: B010 # attribute name is fixed, not dynamic + + _standalone: Final = LiteLLMCompletionResponsesConfig._reasoning_only_assistant_message + merged: list[ # mutable-ok: accumulator # rebind-ok: accumulator AllMessageValues | GenericChatCompletionMessage | ChatCompletionMessageToolCall | ChatCompletionResponseMessage ] = [] # mutable-ok: accumulator - pending_reasoning: list[str] = [] # mutable-ok: accumulator # rebind-ok: accumulator + pending: list[ # mutable-ok: accumulator # rebind-ok: accumulator + tuple[ + str | None, + tuple[ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock, ...] | None, + ] + ] = [] # mutable-ok: accumulator for msg in messages: if ( _role(msg) == "assistant" and _content(msg) is None and not _tool_calls(msg) - and _reasoning_text(msg) is not None + and (_reasoning_text(msg) is not None or _thinking_blocks(msg) is not None) ): - pending_reasoning.append(_reasoning_text(msg) or "") + pending.append((_reasoning_text(msg), _thinking_blocks(msg))) continue - if pending_reasoning and _role(msg) == "assistant": - combined = "\n".join(pending_reasoning) - existing = _reasoning_text(msg) - if existing: - combined = combined + "\n" + existing - if isinstance(msg, dict): - cast(dict[str, Any], msg)["reasoning_content"] = combined # cast-ok: mutable reasoning carrier - else: - setattr(msg, "reasoning_content", combined) # noqa: B010 - pending_reasoning = [] # mutable-ok: reset accumulator - elif pending_reasoning: + if pending and _role(msg) == "assistant": + _apply_pending(msg, pending) + pending = [] # mutable-ok: reset accumulator + elif pending: # Not followed by an assistant message — keep the reasoning # standalone instead of dropping it. merged.extend( # mutable-ok: append reasoning messages - [ # mutable-ok: append reasoning messages - ChatCompletionResponseMessage( - role="assistant", - content=None, - reasoning_content=text, - ) - for text in pending_reasoning - ] + [_standalone(text, blocks) for text, blocks in pending] # mutable-ok: append reasoning messages ) - pending_reasoning = [] # mutable-ok: reset accumulator + pending = [] # mutable-ok: reset accumulator merged.append(msg) merged.extend( # mutable-ok: append trailing reasoning - [ # mutable-ok: append trailing reasoning - ChatCompletionResponseMessage( - role="assistant", - content=None, - reasoning_content=text, - ) - for text in pending_reasoning - ] + [_standalone(text, blocks) for text, blocks in pending] # mutable-ok: append trailing reasoning ) return merged @@ -1140,16 +1188,15 @@ def _transform_responses_api_input_item_to_chat_completion_message( reasoning_text = LiteLLMCompletionResponsesConfig._extract_reasoning_text_from_input_item( # rebind-ok: extraction result input_item ) - if not reasoning_text: - # No plaintext reasoning is available (e.g. encrypted_content only). - # Chat-completions providers cannot consume opaque encrypted blobs, - # so skip the item instead of polluting the prompt. + thinking_blocks = LiteLLMCompletionResponsesConfig._decode_thinking_blocks_from_input_item( # rebind-ok: extraction result + input_item + ) + if not reasoning_text and not thinking_blocks: return [] # mutable-ok: empty drop result return [ # mutable-ok: single message result - ChatCompletionResponseMessage( - role="assistant", - content=None, - reasoning_content=reasoning_text, + LiteLLMCompletionResponsesConfig._reasoning_only_assistant_message( + reasoning_text=reasoning_text, + thinking_blocks=thinking_blocks, ) ] else: @@ -1211,6 +1258,57 @@ def _extract_reasoning_text_from_input_item(input_item: Mapping[str, object]) -> return "\n".join(text_parts) return None + @staticmethod + def _decode_thinking_blocks_from_input_item( + input_item: Mapping[str, object], + ) -> tuple[ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock, ...] | None: + """ + Decode ``encrypted_content`` written by ``_encode_thinking_blocks`` back + into the signed thinking blocks it serialized. + + LiteLLM writes this field itself for providers whose reasoning is signed + (Anthropic, Bedrock converse): it is a JSON array of the provider's own + ``thinking`` / ``redacted_thinking`` blocks, not an opaque OpenAI blob. + Replaying the blocks on the assistant message is what lets the provider + verify the signature and keep the prior chain-of-thought. + + Returns None for anything this deployment did not write, so a genuinely + opaque blob is still skipped rather than forwarded as garbage. + """ + encrypted_content: Final[object] = input_item.get("encrypted_content") + if not isinstance(encrypted_content, str) or not encrypted_content.strip(): + return None + try: + decoded: Final[object] = json.loads(encrypted_content) + except ValueError: + return None + if not isinstance(decoded, list): + return None + + blocks: Final = tuple( + cast( # cast-ok: shape validated by _is_replayable_thinking_block + ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock, + block, + ) + for block in decoded + if isinstance(block, Mapping) and LiteLLMCompletionResponsesConfig._is_replayable_thinking_block(block) + ) + return blocks or None + + @staticmethod + def _is_replayable_thinking_block(block: Mapping[str, object]) -> bool: + """ + A thinking block is only worth replaying when the provider can verify + it: a ``thinking`` block needs its signature, a ``redacted_thinking`` + block needs its opaque data. + """ + block_type: Final[object] = block.get("type") + if block_type == "thinking": + return bool(block.get("signature")) + if block_type == "redacted_thinking": + return bool(block.get("data")) + return False + @staticmethod def _is_input_item_tool_call_output(input_item: Mapping[str, object]) -> bool: """ diff --git a/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py b/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py index ecc024b7d04a..b21be67b1504 100644 --- a/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py +++ b/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py @@ -7,11 +7,18 @@ or being silently dropped. Chat-completions providers such as DeepSeek V4 and Kimi K2.6 require the chain-of-thought to be replayed as ``reasoning_content`` on an assistant message. + +Providers whose reasoning is signed (Anthropic, Bedrock converse) get their +blocks back through ``encrypted_content``, which LiteLLM itself writes as a +JSON array of thinking blocks on the response side. """ +import json + from litellm.responses.litellm_completion_transformation.transformation import ( LiteLLMCompletionResponsesConfig, ) +from litellm.types.utils import Message def _transform_item(item): @@ -59,8 +66,8 @@ def test_reasoning_item_with_summary_only(self): messages = _transform_item(item) assert messages[0]["reasoning_content"] == "..." - def test_reasoning_item_with_encrypted_content_only_dropped(self): - """Opaque encrypted reasoning cannot be forwarded to chat completions.""" + def test_reasoning_item_with_opaque_encrypted_content_dropped(self): + """An encrypted blob LiteLLM did not write cannot be forwarded.""" item = {"type": "reasoning", "id": "rs_3", "encrypted_content": "opaque-blob"} assert _transform_item(item) == [] @@ -142,6 +149,123 @@ def test_reasoning_merged_into_assistant_with_existing_reasoning_content(self): assert messages[0]["reasoning_content"] == "old reasoning\nnew reasoning" +class TestEncryptedReasoningRoundTrip: + """``encrypted_content`` LiteLLM wrote decodes back into thinking blocks.""" + + def test_encoded_thinking_blocks_decode_back(self): + """The decoder is the inverse of the encoder the response side uses.""" + blocks = [ + {"type": "thinking", "thinking": "step one", "signature": "sig-one"}, + {"type": "redacted_thinking", "data": "redacted-payload"}, + ] + message = Message(role="assistant", content="answer", thinking_blocks=blocks) + encoded = LiteLLMCompletionResponsesConfig._encode_thinking_blocks(message) + decoded = LiteLLMCompletionResponsesConfig._decode_thinking_blocks_from_input_item( + {"type": "reasoning", "encrypted_content": encoded} + ) + assert list(decoded) == blocks + + def test_signed_thinking_blocks_replayed_on_assistant_message(self): + """A signed block survives the bridge instead of vanishing.""" + item = { + "type": "reasoning", + "id": "rs_1", + "encrypted_content": json.dumps( + [{"type": "thinking", "thinking": "hidden", "signature": "sig-one"}] + ), + } + messages = _transform_item(item) + assert len(messages) == 1 + assert messages[0]["content"] is None + assert messages[0]["thinking_blocks"] == [ + {"type": "thinking", "thinking": "hidden", "signature": "sig-one"} + ] + + def test_unsigned_blocks_dropped(self): + """Blocks without a signature or redacted payload are not replayed.""" + item = { + "type": "reasoning", + "id": "rs_2", + "encrypted_content": json.dumps([{"type": "thinking", "thinking": "unsigned"}]), + } + assert _transform_item(item) == [] + + def test_json_object_encrypted_content_dropped(self): + """A JSON payload that is not a block array is treated as opaque.""" + item = { + "type": "reasoning", + "id": "rs_3", + "encrypted_content": json.dumps({"ciphertext": "abc"}), + } + assert _transform_item(item) == [] + + def test_thinking_blocks_merged_onto_tool_call_assistant(self): + """Signed reasoning lands on the assistant turn carrying the tool call.""" + messages = _transform_input( + [ + { + "type": "reasoning", + "id": "rs_1", + "summary": [{"type": "summary_text", "text": "look it up"}], + "encrypted_content": json.dumps( + [{"type": "thinking", "thinking": "hidden", "signature": "sig-one"}] + ), + }, + { + "type": "function_call", + "call_id": "call_1", + "name": "lookup", + "arguments": '{"cwe": "79"}', + }, + ] + ) + assert len(messages) == 1 + assert messages[0]["reasoning_content"] == "look it up" + assert messages[0]["thinking_blocks"] == [ + {"type": "thinking", "thinking": "hidden", "signature": "sig-one"} + ] + assert len(messages[0]["tool_calls"]) == 1 + + def test_replayed_blocks_precede_existing_blocks(self): + """Signature verification depends on the original block order.""" + messages = LiteLLMCompletionResponsesConfig._merge_reasoning_only_assistant_messages( + [ + { + "role": "assistant", + "content": None, + "thinking_blocks": [{"type": "thinking", "thinking": "older", "signature": "a"}], + }, + { + "role": "assistant", + "content": "answer", + "thinking_blocks": [{"type": "thinking", "thinking": "newer", "signature": "b"}], + }, + ] + ) + assert len(messages) == 1 + assert [block["thinking"] for block in messages[0]["thinking_blocks"]] == ["older", "newer"] + + def test_encrypted_only_reasoning_preserved_before_user_turn(self): + """A signed item with no plaintext still survives a stateless replay.""" + messages = _transform_input( + [ + { + "type": "reasoning", + "id": "rs_1", + "encrypted_content": json.dumps( + [{"type": "thinking", "thinking": "hidden", "signature": "sig-one"}] + ), + }, + {"role": "user", "content": "and now?"}, + ] + ) + assert len(messages) == 2 + assert messages[0]["role"] == "assistant" + assert "reasoning_content" not in messages[0] + assert messages[0]["thinking_blocks"][0]["signature"] == "sig-one" + assert messages[1]["role"] == "user" + + class TestNonReasoningInputItemUnchanged: """Non-reasoning items still flow through the existing branches.""" From ae25da3d5436f488f31ef6df374c1b65eae976d4 Mon Sep 17 00:00:00 2001 From: Mateo Wang Date: Sat, 22 Aug 2026 11:39:10 -0700 Subject: [PATCH 08/10] fix(responses-bridge): keep reasoning text visible to inspection-only callers Guardrails, token counting and rate limiting share the input transform with the provider path, so moving reasoning onto reasoning_content hid it from them. Provider-bound callers opt in with replay_reasoning. --- .../session_handler.py | 2 + .../transformation.py | 27 +++++++++- .../test_reasoning_input_item_preservation.py | 54 ++++++++++++++++++- 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/litellm/responses/litellm_completion_transformation/session_handler.py b/litellm/responses/litellm_completion_transformation/session_handler.py index dcff26c5b0c3..a53d7c68b0ab 100644 --- a/litellm/responses/litellm_completion_transformation/session_handler.py +++ b/litellm/responses/litellm_completion_transformation/session_handler.py @@ -113,6 +113,7 @@ async def extend_chat_completion_message_with_spend_log_payload( chat_completion_messages = LiteLLMCompletionResponsesConfig.transform_responses_api_input_to_messages( input=response_input_param, responses_api_request=proxy_server_request_dict or {}, + replay_reasoning=True, ) chat_completion_message_history.extend(chat_completion_messages) @@ -125,6 +126,7 @@ async def extend_chat_completion_message_with_spend_log_payload( chat_completion_messages = LiteLLMCompletionResponsesConfig.transform_responses_api_input_to_messages( input=_messages, responses_api_request=proxy_server_request_dict or {}, + replay_reasoning=True, ) chat_completion_message_history.extend(chat_completion_messages) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index c2e803c8a43e..165c2128d387 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -296,6 +296,7 @@ def transform_responses_api_request_to_chat_completion_request( "messages": LiteLLMCompletionResponsesConfig.transform_responses_api_input_to_messages( input=input, responses_api_request=responses_api_request, + replay_reasoning=True, ), "model": model, "tool_choice": LiteLLMCompletionResponsesConfig._transform_tool_choice( @@ -340,6 +341,7 @@ def transform_responses_api_request_to_chat_completion_request( def transform_responses_api_input_to_messages( input: str | ResponseInputParam, responses_api_request: ResponsesAPIOptionalRequestParams | dict, + replay_reasoning: bool = False, ) -> list[ AllMessageValues | GenericChatCompletionMessage @@ -349,6 +351,16 @@ def transform_responses_api_input_to_messages( ]: """ Transform a Responses API input into a list of messages + + ``replay_reasoning`` belongs to callers whose messages are about to be + sent to a model: prior-turn ``reasoning`` items are then rebuilt as + assistant ``reasoning_content`` and signed ``thinking_blocks`` so the + provider gets its own chain-of-thought back instead of reading it as + visible text. + + Callers that only inspect the messages (token counting, rate limiting, + guardrail scanning) leave it off, because they need every piece of text + in the request to stay readable as message ``content``. """ messages: list[ AllMessageValues @@ -367,6 +379,7 @@ def transform_responses_api_input_to_messages( messages.extend( LiteLLMCompletionResponsesConfig._transform_response_input_param_to_chat_completion_message( input=input, + replay_reasoning=replay_reasoning, ) ) @@ -443,11 +456,15 @@ async def async_responses_api_session_handler( @staticmethod def _transform_response_input_param_to_chat_completion_message( input: str | ResponseInputParam, + replay_reasoning: bool = False, ) -> list[ AllMessageValues | GenericChatCompletionMessage | ChatCompletionMessageToolCall | ChatCompletionResponseMessage ]: """ Transform a ResponseInputParam into a Chat Completion message + + See ``transform_responses_api_input_to_messages`` for what + ``replay_reasoning`` means. """ messages: list[ AllMessageValues @@ -463,7 +480,8 @@ def _transform_response_input_param_to_chat_completion_message( for _input in input: chat_completion_messages = ( LiteLLMCompletionResponsesConfig._transform_responses_api_input_item_to_chat_completion_message( - input_item=_input + input_item=_input, + replay_reasoning=replay_reasoning, ) ) @@ -559,6 +577,8 @@ def _transform_response_input_param_to_chat_completion_message( continue messages.extend(chat_completion_messages) + if not replay_reasoning: + return messages return LiteLLMCompletionResponsesConfig._merge_reasoning_only_assistant_messages(messages) @staticmethod @@ -1151,6 +1171,7 @@ def _ensure_tool_results_have_corresponding_tool_calls( @staticmethod def _transform_responses_api_input_item_to_chat_completion_message( input_item: Any, + replay_reasoning: bool = False, ) -> list[AllMessageValues | GenericChatCompletionMessage | ChatCompletionResponseMessage]: """ Transform a Responses API input item into a Chat Completion message @@ -1179,12 +1200,14 @@ def _transform_responses_api_input_item_to_chat_completion_message( return LiteLLMCompletionResponsesConfig._transform_responses_api_function_call_to_chat_completion_message( function_call=input_item ) - elif input_item.get("type") == "reasoning": + elif replay_reasoning and input_item.get("type") == "reasoning": # A ResponseReasoningItemParam carries the prior-turn chain-of-thought. # Chat-completions providers (DeepSeek V4, Kimi K2.6, ...) expect this # to be replayed as `reasoning_content` on an assistant message, not as # visible `content` (prompt pollution) and not dropped (DeepSeek V4 # rejects multi-turn requests with a missing `reasoning_content`). + # Callers that only inspect the request skip this branch so the + # reasoning text stays visible to them as message content. reasoning_text = LiteLLMCompletionResponsesConfig._extract_reasoning_text_from_input_item( # rebind-ok: extraction result input_item ) diff --git a/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py b/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py index b21be67b1504..821b8fffe9ac 100644 --- a/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py +++ b/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py @@ -23,13 +23,19 @@ def _transform_item(item): return LiteLLMCompletionResponsesConfig._transform_responses_api_input_item_to_chat_completion_message( - input_item=item + input_item=item, replay_reasoning=True ) def _transform_input(input_items): return LiteLLMCompletionResponsesConfig._transform_response_input_param_to_chat_completion_message( - input=input_items + input=input_items, replay_reasoning=True + ) + + +def _inspect_input(input_items): + return LiteLLMCompletionResponsesConfig.transform_responses_api_input_to_messages( + input=input_items, responses_api_request={} ) @@ -266,6 +272,50 @@ def test_encrypted_only_reasoning_preserved_before_user_turn(self): assert messages[1]["role"] == "user" +class TestInspectionCallersStillSeeReasoningText: + """Token counting, rate limiting and guardrails read the request as text. + + Moving reasoning onto ``reasoning_content`` is only right for messages on + their way to a provider. A guardrail scanning for sensitive data reads + message ``content``, so the inspection default keeps the text there. + """ + + def test_reasoning_text_stays_readable_as_content_by_default(self): + messages = _inspect_input( + [ + {"role": "user", "content": "What did we decide?"}, + { + "type": "reasoning", + "id": "rs_1", + "content": [{"type": "output_text", "text": "card 4111111111111111"}], + }, + ] + ) + assert len(messages) == 2 + blocks = messages[1]["content"] + assert "4111111111111111" in json.dumps(blocks) + assert "reasoning_content" not in messages[1] + + def test_reasoning_moves_off_content_only_for_provider_bound_callers(self): + input_items = [ + { + "type": "reasoning", + "id": "rs_1", + "content": [{"type": "output_text", "text": "hidden plan"}], + }, + {"role": "user", "content": "go on"}, + ] + provider_bound = LiteLLMCompletionResponsesConfig.transform_responses_api_input_to_messages( + input=input_items, responses_api_request={}, replay_reasoning=True + ) + assert provider_bound[0]["content"] is None + assert provider_bound[0]["reasoning_content"] == "hidden plan" + + inspected = _inspect_input(input_items) + assert inspected[0]["role"] == "user" + assert "hidden plan" in json.dumps(inspected[0]["content"]) + + class TestNonReasoningInputItemUnchanged: """Non-reasoning items still flow through the existing branches.""" From 3d69ec3603147c53ba5ff1d5f275b6c5e6a32777 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Sat, 22 Aug 2026 14:30:52 -0700 Subject: [PATCH 09/10] fix(responses-bridge): keep summary-only reasoning text scannable A reasoning input item that carries only summary text is replayed to the provider as reasoning_content, so inspection-only callers must see that text too. They used to fall through to the generic content branch, which reads content and drops a summary-only item, leaving guardrails and token counters blind to text the model still receives. --- .../transformation.py | 23 ++++++++++++++--- .../test_reasoning_input_item_preservation.py | 25 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index 165c2128d387..83e02888924e 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -1200,14 +1200,31 @@ def _transform_responses_api_input_item_to_chat_completion_message( return LiteLLMCompletionResponsesConfig._transform_responses_api_function_call_to_chat_completion_message( function_call=input_item ) - elif replay_reasoning and input_item.get("type") == "reasoning": + elif input_item.get("type") == "reasoning": # A ResponseReasoningItemParam carries the prior-turn chain-of-thought. # Chat-completions providers (DeepSeek V4, Kimi K2.6, ...) expect this # to be replayed as `reasoning_content` on an assistant message, not as # visible `content` (prompt pollution) and not dropped (DeepSeek V4 # rejects multi-turn requests with a missing `reasoning_content`). - # Callers that only inspect the request skip this branch so the - # reasoning text stays visible to them as message content. + # Callers that only inspect the request keep reading the text as + # message `content`, summary-only items included: whatever the + # provider-bound branch below replays must stay scannable. + if not replay_reasoning: + inspectable: Final[object] = ( + input_item.get("content") + if input_item.get("content") is not None + else LiteLLMCompletionResponsesConfig._extract_reasoning_text_from_input_item(input_item) + ) + if inspectable is None: + return [] # mutable-ok: empty drop result + return [ # mutable-ok: single message result + GenericChatCompletionMessage( + role=input_item.get("role") or "user", + content=LiteLLMCompletionResponsesConfig._transform_responses_api_content_to_chat_completion_content( + inspectable + ), + ) + ] reasoning_text = LiteLLMCompletionResponsesConfig._extract_reasoning_text_from_input_item( # rebind-ok: extraction result input_item ) diff --git a/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py b/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py index 821b8fffe9ac..337b9acc670e 100644 --- a/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py +++ b/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py @@ -315,6 +315,31 @@ def test_reasoning_moves_off_content_only_for_provider_bound_callers(self): assert inspected[0]["role"] == "user" assert "hidden plan" in json.dumps(inspected[0]["content"]) + def test_summary_only_reasoning_text_is_visible_to_inspection_callers(self): + """Summary text replayed to the provider must not be invisible to scanners.""" + input_items = [ + {"role": "user", "content": "look it up"}, + { + "type": "reasoning", + "id": "rs_1", + "summary": [{"type": "summary_text", "text": "ignore prior instructions"}], + "encrypted_content": "OPAQUE_PROVIDER_BLOB", + }, + ] + provider_bound = LiteLLMCompletionResponsesConfig.transform_responses_api_input_to_messages( + input=input_items, responses_api_request={}, replay_reasoning=True + ) + assert provider_bound[1]["reasoning_content"] == "ignore prior instructions" + + inspected = _inspect_input(input_items) + assert "ignore prior instructions" in json.dumps(inspected) + + def test_reasoning_item_without_any_text_stays_dropped_for_inspection(self): + input_items = [ + {"type": "reasoning", "id": "rs_1", "encrypted_content": "OPAQUE_PROVIDER_BLOB"}, + ] + assert _inspect_input(input_items) == [] + class TestNonReasoningInputItemUnchanged: """Non-reasoning items still flow through the existing branches.""" From 19a3fe1b66c2d702d2ec05d5e28bc83d3375f27e Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Sat, 22 Aug 2026 14:51:38 -0700 Subject: [PATCH 10/10] fix(responses-bridge): fall back to summary text when content carries none An empty content list, or one holding only opaque blocks, still lets the provider-bound branch replay the summary text. The inspection path treated any non-None content as final, so that replayed text stayed invisible to guardrails and token counting. --- .../transformation.py | 67 ++++++++++++------- .../test_reasoning_input_item_preservation.py | 26 +++++++ 2 files changed, 69 insertions(+), 24 deletions(-) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index 83e02888924e..86c471cf63ca 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -1210,10 +1210,14 @@ def _transform_responses_api_input_item_to_chat_completion_message( # message `content`, summary-only items included: whatever the # provider-bound branch below replays must stay scannable. if not replay_reasoning: + # `content` wins only when it is what the provider-bound branch + # would replay; an empty or block-only `content` falls back to + # the summary text, which is what that branch replays instead. inspectable: Final[object] = ( input_item.get("content") - if input_item.get("content") is not None - else LiteLLMCompletionResponsesConfig._extract_reasoning_text_from_input_item(input_item) + if LiteLLMCompletionResponsesConfig._reasoning_text_from_content(input_item) is not None + else LiteLLMCompletionResponsesConfig._reasoning_text_from_summary(input_item) + or input_item.get("content") ) if inspectable is None: return [] # mutable-ok: empty drop result @@ -1255,22 +1259,19 @@ def _transform_responses_api_input_item_to_chat_completion_message( ] @staticmethod - def _extract_reasoning_text_from_input_item(input_item: Mapping[str, object]) -> str | None: + def _reasoning_text_from_content(input_item: Mapping[str, object]) -> str | None: """ - Extract plaintext reasoning from a ResponseReasoningItemParam. - - Handles: - - content as a string - - content as a list of blocks (output_text / summary_text / text) - - summary as a list of summary_text blocks (fallback) + Plaintext a ResponseReasoningItemParam carries in ``content``. - Returns None when only opaque forms (e.g. encrypted_content) are present. + Handles content as a string and content as a list of blocks + (output_text / summary_text / text). Returns None when the item has + no content, or only opaque blocks (e.g. encrypted_content). """ content: Final[object] = input_item.get("content") if isinstance(content, str) and content.strip(): return content if isinstance(content, list): - text_parts: list[str] = [] # mutable-ok: text accumulator # rebind-ok: text accumulator + text_parts: Final[list[str]] = [] # mutable-ok: text accumulator for block in content: if not isinstance(block, Mapping): continue @@ -1282,21 +1283,39 @@ def _extract_reasoning_text_from_input_item(input_item: Mapping[str, object]) -> text_parts.append(text.strip()) if text_parts: return "\n".join(text_parts) + return None - # Guardrail traversal in litellm/proxy/guardrails/_content_utils.py - # inspects and rewrites these summary blocks before they are forwarded. + @staticmethod + def _reasoning_text_from_summary(input_item: Mapping[str, object]) -> str | None: + """ + Plaintext a ResponseReasoningItemParam carries in ``summary``. + + Guardrail traversal in litellm/proxy/guardrails/_content_utils.py + inspects and rewrites these summary blocks before they are forwarded. + """ summary: Final[object] = input_item.get("summary") - if isinstance(summary, list): - text_parts = [] # mutable-ok: text accumulator # rebind-ok: text accumulator - for block in summary: - if not isinstance(block, Mapping): - continue - text = block.get("text") - if isinstance(text, str) and text.strip(): - text_parts.append(text.strip()) - if text_parts: - return "\n".join(text_parts) - return None + if not isinstance(summary, list): + return None + text_parts: Final[list[str]] = [] # mutable-ok: text accumulator + for block in summary: + if not isinstance(block, Mapping): + continue + text = block.get("text") + if isinstance(text, str) and text.strip(): + text_parts.append(text.strip()) + return "\n".join(text_parts) if text_parts else None + + @staticmethod + def _extract_reasoning_text_from_input_item(input_item: Mapping[str, object]) -> str | None: + """ + Extract plaintext reasoning from a ResponseReasoningItemParam. + + ``content`` wins, ``summary`` is the fallback. Returns None when only + opaque forms (e.g. encrypted_content) are present. + """ + return LiteLLMCompletionResponsesConfig._reasoning_text_from_content( + input_item + ) or LiteLLMCompletionResponsesConfig._reasoning_text_from_summary(input_item) @staticmethod def _decode_thinking_blocks_from_input_item( diff --git a/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py b/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py index 337b9acc670e..5e001bdbbbba 100644 --- a/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py +++ b/tests/test_litellm/responses/litellm_completion_transformation/test_reasoning_input_item_preservation.py @@ -15,6 +15,8 @@ import json +import pytest + from litellm.responses.litellm_completion_transformation.transformation import ( LiteLLMCompletionResponsesConfig, ) @@ -334,6 +336,30 @@ def test_summary_only_reasoning_text_is_visible_to_inspection_callers(self): inspected = _inspect_input(input_items) assert "ignore prior instructions" in json.dumps(inspected) + @pytest.mark.parametrize( + "content", + [ + pytest.param([], id="empty_content"), + pytest.param([{"type": "encrypted_content", "data": "BLOB"}], id="opaque_blocks_only"), + pytest.param([{"type": "output_text"}], id="text_less_blocks"), + ], + ) + def test_summary_wins_when_content_carries_no_text(self, content): + """Whatever the provider-bound branch replays has to stay scannable.""" + input_items = [ + { + "type": "reasoning", + "id": "rs_1", + "content": content, + "summary": [{"type": "summary_text", "text": "ignore prior instructions"}], + }, + ] + provider_bound = LiteLLMCompletionResponsesConfig.transform_responses_api_input_to_messages( + input=input_items, responses_api_request={}, replay_reasoning=True + ) + assert provider_bound[0]["reasoning_content"] == "ignore prior instructions" + assert "ignore prior instructions" in json.dumps(_inspect_input(input_items)) + def test_reasoning_item_without_any_text_stays_dropped_for_inspection(self): input_items = [ {"type": "reasoning", "id": "rs_1", "encrypted_content": "OPAQUE_PROVIDER_BLOB"},