From bf131d2ec255dd3336bf9d47c30dd0ce5b3c29ea Mon Sep 17 00:00:00 2001 From: agustin18 Date: Thu, 15 Oct 2026 17:09:22 +0000 Subject: [PATCH 1/3] fix(responses): preserve all reasoning items in non-streaming bridge (#43620) --- .../transformation.py | 156 ++++++++++-------- ...responses_transformation_transformation.py | 129 +++++++++++++++ 2 files changed, 215 insertions(+), 70 deletions(-) diff --git a/litellm/completion_extras/litellm_responses_transformation/transformation.py b/litellm/completion_extras/litellm_responses_transformation/transformation.py index 979caa29832..1b6886c6f42 100644 --- a/litellm/completion_extras/litellm_responses_transformation/transformation.py +++ b/litellm/completion_extras/litellm_responses_transformation/transformation.py @@ -476,9 +476,10 @@ def _fold_choices_into_one( choices: Sequence["Choices"], tool_calls: Sequence[Mapping[str, object]], trailing_reasoning_content: str | None, - trailing_reasoning_item: _BuiltReasoningItem | None, - tool_search_items: Sequence[Mapping[str, object]], + trailing_reasoning_item: _BuiltReasoningItem | None = None, + tool_search_items: Sequence[Mapping[str, object]] = (), structured_output: bool = False, + trailing_reasoning_items: Sequence[_BuiltReasoningItem] = (), ) -> "Choices": """Clients that only read choices[0] must still see every text and tool call. @@ -493,9 +494,13 @@ def _fold_choices_into_one( offsets: Final = (0, *accumulate(len(text) for text in texts)) annotations: Final = tuple(_shifted_annotations(content_messages, offsets)) reasoning_contents: Final = (*(_message_reasoning_content(m) for m in messages), trailing_reasoning_content) + accumulated_trailing_items: Final = ( + *trailing_reasoning_items, + *(() if trailing_reasoning_item is None else (trailing_reasoning_item,)), + ) reasoning_items: Final = ( *chain.from_iterable(_message_reasoning_items(m) for m in messages), - *(() if trailing_reasoning_item is None else (trailing_reasoning_item,)), + *accumulated_trailing_items, ) return Choices( message=Message( @@ -512,6 +517,60 @@ def _fold_choices_into_one( ) +def _append_reasoning_if_present( + item: object, + pending: list[_BuiltReasoningItem], # mutable-ok: accumulator + current_content: str | None, +) -> tuple[bool, str | None]: + """Extract reasoning item if present, append to pending, and update concatenated summary.""" + reasoning_item: Final = _reasoning_item_from_output_item(item) + if reasoning_item is None: + return False, current_content + pending.append(reasoning_item) + summary_texts: Final = [s["text"] for s in reasoning_item["summary"] if s.get("text")] + if not summary_texts: + return True, current_content + step_text: Final = " ".join(summary_texts) + merged_content: Final = f"{current_content} {step_text}".strip() if current_content else step_text + return True, merged_content + + +def _tool_call_dict_from_output_item_candidate( + item: object, + index: int, + apply_patch_type: type | None, +) -> Mapping[str, object] | None: + """Convert typed or raw tool call items into chat completion tool call mapping.""" + from openai.types.responses import ResponseFunctionToolCall + + if isinstance(item, ResponseFunctionToolCall): + from litellm.responses.litellm_completion_transformation.transformation import ( + LiteLLMCompletionResponsesConfig, + ) + + return LiteLLMCompletionResponsesConfig.convert_response_function_tool_call_to_chat_completion_tool_call( + tool_call_item=item, + index=index, + ) + + if apply_patch_type is not None and isinstance(item, apply_patch_type): + from litellm.responses.litellm_completion_transformation.transformation import ( + LiteLLMCompletionResponsesConfig, + ) + + return LiteLLMCompletionResponsesConfig.convert_apply_patch_tool_call_to_chat_completion_tool_call( + tool_call_item=item, + index=index, + ) + + if isinstance(item, (dict, BaseModel)): + raw_item: Final = item if isinstance(item, dict) else item.model_dump() + if raw_item.get("type") in ("function_call", "custom_tool_call"): + return tool_call_dict_from_output_item(raw_item, index, include_namespace=True) + + return None + + def _responses_event_type(chunk: Mapping[str, object]) -> object: event_type: Final = chunk.get("type") return event_type.value if isinstance(event_type, ResponsesAPIStreamEvents) else event_type @@ -1160,11 +1219,7 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge): Returns: List of Choices objects """ - from openai.types.responses import ( - ResponseFunctionToolCall, - ResponseOutputMessage, - ResponseReasoningItem, - ) + from openai.types.responses import ResponseOutputMessage try: from openai.types.responses.response_output_item import ( @@ -1178,7 +1233,7 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge): choices: Final[list[Choices]] = [] index = 0 reasoning_content: str | None = None - pending_reasoning_item: _BuiltReasoningItem | None = None + pending_reasoning_items: Final[list[_BuiltReasoningItem]] = [] # mutable-ok: accumulator # Collect all tool calls to put them in a single choice # (Chat Completions API expects all tool calls in one message) @@ -1186,15 +1241,21 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge): tool_call_index = 0 for item in output_items: - if isinstance(item, ResponseReasoningItem): - pending_reasoning_item = _build_reasoning_item( - item_id=item.id, - encrypted_content=getattr(item, "encrypted_content", None), - summary_raw=item.summary, - ) - reasoning_content = " ".join(s["text"] for s in pending_reasoning_item["summary"] if s.get("text")) + is_reasoning, reasoning_content = _append_reasoning_if_present( + item, pending_reasoning_items, reasoning_content + ) + if is_reasoning: + continue - elif isinstance(item, ResponseOutputMessage): + tool_call_dict = _tool_call_dict_from_output_item_candidate( + item, tool_call_index, ResponseApplyPatchToolCall + ) + if tool_call_dict is not None: + accumulated_tool_calls.append(tool_call_dict) + tool_call_index += 1 + continue + + if isinstance(item, ResponseOutputMessage): for content in item.content: response_text = getattr(content, "text", "") # Extract annotations from content if present @@ -1207,10 +1268,7 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge): content=response_text if response_text else "", reasoning_content=reasoning_content, annotations=annotations, - reasoning_items=cast( - list[ChatCompletionReasoningItem] | None, - ([pending_reasoning_item] if pending_reasoning_item is not None else None), - ), + reasoning_items=_as_chat_reasoning_items(pending_reasoning_items), ) choices.append( @@ -1222,56 +1280,14 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge): ) reasoning_content = None # flush - pending_reasoning_item = None # flush + pending_reasoning_items.clear() # flush index += 1 - elif isinstance(item, ResponseFunctionToolCall): - from litellm.responses.litellm_completion_transformation.transformation import ( - LiteLLMCompletionResponsesConfig, - ) - - tool_call_dict = ( - LiteLLMCompletionResponsesConfig.convert_response_function_tool_call_to_chat_completion_tool_call( - tool_call_item=item, - index=tool_call_index, - ) - ) - accumulated_tool_calls.append(tool_call_dict) - tool_call_index += 1 - - elif ResponseApplyPatchToolCall is not None and isinstance(item, ResponseApplyPatchToolCall): - from litellm.responses.litellm_completion_transformation.transformation import ( - LiteLLMCompletionResponsesConfig, - ) - - tool_call_dict = ( - LiteLLMCompletionResponsesConfig.convert_apply_patch_tool_call_to_chat_completion_tool_call( - tool_call_item=item, - index=tool_call_index, - ) - ) - accumulated_tool_calls.append(tool_call_dict) - tool_call_index += 1 - - elif isinstance(item, (dict, BaseModel)): - # Raw dict items (e.g., from GPT-5 Codex) and pydantic items matching no - # openai SDK class above: typed ResponseCustomToolCall and litellm's own - # GenericResponseOutputItem from the completion bridge both land here - raw_item = item if isinstance(item, dict) else item.model_dump() - if raw_item.get("type") in ("function_call", "custom_tool_call"): - # Tool calls accumulate into the single trailing tool_calls choice - # like the typed branches above; a choice per call would hide every - # call after choices[0] from chat clients - accumulated_tool_calls.append( - tool_call_dict_from_output_item(raw_item, tool_call_index, include_namespace=True) - ) - tool_call_index += 1 - elif handle_raw_dict_callback is not None: - choice, index = handle_raw_dict_callback(item=raw_item, index=index) - if choice is not None: - choices.append(choice) - else: - pass # don't fail request if item in list is not supported + elif isinstance(item, (dict, BaseModel)) and handle_raw_dict_callback is not None: + raw_item: Final = item if isinstance(item, dict) else item.model_dump() + choice, index = handle_raw_dict_callback(item=raw_item, index=index) + if choice is not None: + choices.append(choice) tool_search_items: Final = _tool_search_items_from_output_items(output_items) if not choices and not accumulated_tool_calls: @@ -1287,7 +1303,7 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge): choices=choices, tool_calls=accumulated_tool_calls, trailing_reasoning_content=reasoning_content, - trailing_reasoning_item=pending_reasoning_item, + trailing_reasoning_items=pending_reasoning_items, tool_search_items=tool_search_items, structured_output=structured_output, ) diff --git a/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py b/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py index 83c59480d37..edf5751f4e1 100644 --- a/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py +++ b/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py @@ -5951,3 +5951,132 @@ def test_stream_structured_output_drops_text_after_first_message_item( content: Final = "".join(chunk.choices[0].delta.content or "" for chunk in chunks) assert content == expected_content assert chunks[-1].choices[0].finish_reason == "stop" + + +@pytest.mark.parametrize( + "raw_output_items, expected_reasoning_ids, expected_summary_text, target_type", + [ + pytest.param( + [ + { + "type": "reasoning", + "id": "rs_1", + "summary": [{"type": "summary_text", "text": "Step 1"}], + "encrypted_content": "enc1", + }, + { + "type": "reasoning", + "id": "rs_2", + "summary": [{"type": "summary_text", "text": "Step 2"}], + "encrypted_content": "enc2", + }, + { + "type": "function_call", + "id": "fc_1", + "call_id": "call_1", + "name": "search", + "arguments": "{}", + "status": "completed", + }, + ], + ["rs_1", "rs_2"], + "Step 1 Step 2", + "tool_calls", + id="multiple_reasoning_items_before_tool_call", + ), + pytest.param( + [ + { + "type": "reasoning", + "id": "rs_alpha", + "summary": [{"type": "summary_text", "text": "Analysis"}], + "encrypted_content": "enc_a", + }, + { + "type": "reasoning", + "id": "rs_beta", + "summary": [{"type": "summary_text", "text": "Conclusion"}], + "encrypted_content": "enc_b", + }, + ], + ["rs_alpha", "rs_beta"], + "Analysis Conclusion", + "message", + id="multiple_reasoning_items_before_output_message", + ), + pytest.param( + [ + { + "type": "reasoning", + "id": "rs_raw_1", + "summary": [{"type": "summary_text", "text": "Raw Step 1"}], + "encrypted_content": "enc_raw", + }, + { + "type": "message", + "role": "assistant", + "content": [{"type": "output_text", "text": "Raw response text"}], + }, + ], + ["rs_raw_1"], + "Raw Step 1", + "raw_dict_message", + id="reasoning_items_before_raw_dict_message", + ), + pytest.param( + [ + { + "type": "function_call", + "id": "fc_solo", + "call_id": "call_solo", + "name": "lookup", + "arguments": "{}", + "status": "completed", + } + ], + None, + None, + "tool_calls", + id="zero_reasoning_items_returns_none", + ), + ], +) +def test_convert_response_output_to_choices_preserves_all_reasoning_items( + raw_output_items: list[dict[str, object]], + expected_reasoning_ids: list[str] | None, + expected_summary_text: str | None, + target_type: str, +) -> None: + """Verify non-streaming Responses API bridge retains all reasoning items across message and tool call turns.""" + from openai.types.responses import ResponseOutputMessage + from openai.types.responses.response_output_message import ResponseOutputText + + items_to_pass: Final[list[object]] = list(raw_output_items) # mutable-ok: fixture setup + if target_type == "message": + items_to_pass.append( + ResponseOutputMessage( + id="msg_1", + role="assistant", + status="completed", + type="message", + content=[ResponseOutputText(annotations=[], text="Done", type="output_text", logprobs=[])], + ) + ) + + handler: Final = LiteLLMResponsesTransformationHandler() + choices: Final = handler._convert_response_output_to_choices( + items_to_pass, + handle_raw_dict_callback=handler._handle_raw_dict_response_item, + ) + + assert len(choices) == 1 + msg = choices[0].message + reasoning_items = getattr(msg, "reasoning_items", None) + reasoning_content = getattr(msg, "reasoning_content", None) + if expected_reasoning_ids is None: + assert reasoning_items is None + assert reasoning_content is None + else: + assert reasoning_items is not None + assert [item["id"] for item in reasoning_items] == expected_reasoning_ids + assert reasoning_content == expected_summary_text From 0b472edd2ab4a06cc406ad216bcad2b5fab3a269 Mon Sep 17 00:00:00 2001 From: agustin18 Date: Thu, 15 Oct 2026 23:25:12 +0000 Subject: [PATCH 2/3] fix(responses): resolve basedpyright ceiling breach in non-streaming bridge --- .../transformation.py | 100 +++++++++--------- 1 file changed, 52 insertions(+), 48 deletions(-) diff --git a/litellm/completion_extras/litellm_responses_transformation/transformation.py b/litellm/completion_extras/litellm_responses_transformation/transformation.py index 1b6886c6f42..51d3c33941d 100644 --- a/litellm/completion_extras/litellm_responses_transformation/transformation.py +++ b/litellm/completion_extras/litellm_responses_transformation/transformation.py @@ -535,40 +535,7 @@ def _append_reasoning_if_present( return True, merged_content -def _tool_call_dict_from_output_item_candidate( - item: object, - index: int, - apply_patch_type: type | None, -) -> Mapping[str, object] | None: - """Convert typed or raw tool call items into chat completion tool call mapping.""" - from openai.types.responses import ResponseFunctionToolCall - if isinstance(item, ResponseFunctionToolCall): - from litellm.responses.litellm_completion_transformation.transformation import ( - LiteLLMCompletionResponsesConfig, - ) - - return LiteLLMCompletionResponsesConfig.convert_response_function_tool_call_to_chat_completion_tool_call( - tool_call_item=item, - index=index, - ) - - if apply_patch_type is not None and isinstance(item, apply_patch_type): - from litellm.responses.litellm_completion_transformation.transformation import ( - LiteLLMCompletionResponsesConfig, - ) - - return LiteLLMCompletionResponsesConfig.convert_apply_patch_tool_call_to_chat_completion_tool_call( - tool_call_item=item, - index=index, - ) - - if isinstance(item, (dict, BaseModel)): - raw_item: Final = item if isinstance(item, dict) else item.model_dump() - if raw_item.get("type") in ("function_call", "custom_tool_call"): - return tool_call_dict_from_output_item(raw_item, index, include_namespace=True) - - return None def _responses_event_type(chunk: Mapping[str, object]) -> object: @@ -1219,7 +1186,10 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge): Returns: List of Choices objects """ - from openai.types.responses import ResponseOutputMessage + from openai.types.responses import ( + ResponseFunctionToolCall, + ResponseOutputMessage, + ) try: from openai.types.responses.response_output_item import ( @@ -1247,15 +1217,7 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge): if is_reasoning: continue - tool_call_dict = _tool_call_dict_from_output_item_candidate( - item, tool_call_index, ResponseApplyPatchToolCall - ) - if tool_call_dict is not None: - accumulated_tool_calls.append(tool_call_dict) - tool_call_index += 1 - continue - - if isinstance(item, ResponseOutputMessage): + elif isinstance(item, ResponseOutputMessage): for content in item.content: response_text = getattr(content, "text", "") # Extract annotations from content if present @@ -1283,11 +1245,53 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge): pending_reasoning_items.clear() # flush index += 1 - elif isinstance(item, (dict, BaseModel)) and handle_raw_dict_callback is not None: - raw_item: Final = item if isinstance(item, dict) else item.model_dump() - choice, index = handle_raw_dict_callback(item=raw_item, index=index) - if choice is not None: - choices.append(choice) + elif isinstance(item, ResponseFunctionToolCall): + from litellm.responses.litellm_completion_transformation.transformation import ( + LiteLLMCompletionResponsesConfig, + ) + + tool_call_dict = ( + LiteLLMCompletionResponsesConfig.convert_response_function_tool_call_to_chat_completion_tool_call( + tool_call_item=item, + index=tool_call_index, + ) + ) + accumulated_tool_calls.append(tool_call_dict) + tool_call_index += 1 + + elif ResponseApplyPatchToolCall is not None and isinstance(item, ResponseApplyPatchToolCall): + from litellm.responses.litellm_completion_transformation.transformation import ( + LiteLLMCompletionResponsesConfig, + ) + + tool_call_dict = ( + LiteLLMCompletionResponsesConfig.convert_apply_patch_tool_call_to_chat_completion_tool_call( + tool_call_item=item, + index=tool_call_index, + ) + ) + accumulated_tool_calls.append(tool_call_dict) + tool_call_index += 1 + + elif isinstance(item, (dict, BaseModel)): + # Raw dict items (e.g., from GPT-5 Codex) and pydantic items matching no + # openai SDK class above: typed ResponseCustomToolCall and litellm's own + # GenericResponseOutputItem from the completion bridge both land here + raw_item = item if isinstance(item, dict) else item.model_dump() + if raw_item.get("type") in ("function_call", "custom_tool_call"): + # Tool calls accumulate into the single trailing tool_calls choice + # like the typed branches above; a choice per call would hide every + # call after choices[0] from chat clients + accumulated_tool_calls.append( + tool_call_dict_from_output_item(raw_item, tool_call_index, include_namespace=True) + ) + tool_call_index += 1 + elif handle_raw_dict_callback is not None: + choice, index = handle_raw_dict_callback(item=raw_item, index=index) + if choice is not None: + choices.append(choice) + else: + pass # don't fail request if item in list is not supported tool_search_items: Final = _tool_search_items_from_output_items(output_items) if not choices and not accumulated_tool_calls: From 0b5b6ab3f534d5040a1306c7c91785446bdc5977 Mon Sep 17 00:00:00 2001 From: agustin18 Date: Thu, 15 Oct 2026 23:28:52 +0000 Subject: [PATCH 3/3] style: format transformation.py with ruff --- .../litellm_responses_transformation/transformation.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/litellm/completion_extras/litellm_responses_transformation/transformation.py b/litellm/completion_extras/litellm_responses_transformation/transformation.py index 51d3c33941d..3dcd2c65044 100644 --- a/litellm/completion_extras/litellm_responses_transformation/transformation.py +++ b/litellm/completion_extras/litellm_responses_transformation/transformation.py @@ -535,9 +535,6 @@ def _append_reasoning_if_present( return True, merged_content - - - def _responses_event_type(chunk: Mapping[str, object]) -> object: event_type: Final = chunk.get("type") return event_type.value if isinstance(event_type, ResponsesAPIStreamEvents) else event_type