From 33f60b640079b2384864a7608325478ed591e641 Mon Sep 17 00:00:00 2001 From: jesco-absolut Date: Fri, 3 Jul 2026 21:19:21 -0400 Subject: [PATCH] fix(proxy): preserve reasoning fields on streaming cache hits --- .../convert_dict_to_response.py | 7 +-- .../test_convert_dict_to_chat_completion.py | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py b/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py index a6f10e1ede3..87e50a3046c 100644 --- a/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py +++ b/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py @@ -204,12 +204,7 @@ async def convert_to_streaming_response_async( t["index"] = index pydantic_tool_calls.append(ChatCompletionDeltaToolCall(**t)) choice["message"]["tool_calls"] = pydantic_tool_calls - delta = Delta( - content=choice["message"].get("content", None), - role=choice["message"]["role"], - function_call=choice["message"].get("function_call", None), - tool_calls=choice["message"].get("tool_calls", None), - ) + delta = Delta(**choice["message"]) finish_reason = choice.get("finish_reason", None) if finish_reason is None: diff --git a/tests/llm_translation/test_llm_response_utils/test_convert_dict_to_chat_completion.py b/tests/llm_translation/test_llm_response_utils/test_convert_dict_to_chat_completion.py index 5683d973ac9..f091b4453e9 100644 --- a/tests/llm_translation/test_llm_response_utils/test_convert_dict_to_chat_completion.py +++ b/tests/llm_translation/test_llm_response_utils/test_convert_dict_to_chat_completion.py @@ -2005,6 +2005,60 @@ class TestConvertToStreamingResponseAsync: assert chunks[-1].choices[0].finish_reason == "stop" assert chunks[-1].usage.prompt_tokens == 3 + def test_preserves_reasoning_fields(self): + import asyncio + + from litellm.litellm_core_utils.llm_response_utils.convert_dict_to_response import ( + convert_to_streaming_response_async, + ) + + thinking_blocks = [ + { + "type": "thinking", + "thinking": "cached reasoning", + "signature": "sig-cache", + } + ] + response_object = { + "id": "msg_async_reasoning_cache", + "model": "claude-3", + "created": 1700000000, + "choices": [ + { + "finish_reason": "stop", + "index": 0, + "message": { + "content": "Final answer", + "role": "assistant", + "reasoning_content": "cached reasoning", + "thinking_blocks": thinking_blocks, + }, + } + ], + } + + async def run(): + return [ + chunk + async for chunk in convert_to_streaming_response_async( + response_object=response_object + ) + ] + + chunks = asyncio.run(run()) + + assert ( + "".join(c.choices[0].delta.content or "" for c in chunks) == "Final answer" + ) + assert chunks[0].choices[0].delta.reasoning_content == "cached reasoning" + assert chunks[0].choices[0].delta.thinking_blocks == thinking_blocks + assert not any( + hasattr(c.choices[0].delta, "reasoning_content") for c in chunks[1:] + ) + assert not any( + hasattr(c.choices[0].delta, "thinking_blocks") for c in chunks[1:] + ) + class TestHandleInvalidParallelToolCalls: def test_none_input(self):