From 10a9c24dae8d8544a2828fbbf73eae24ce7d5c0f Mon Sep 17 00:00:00 2001 From: ccurme Date: Thu, 24 Apr 2025 12:01:45 -0400 Subject: [PATCH] openai: fix streaming reasoning without summaries (#30999) Following https://github.com/langchain-ai/langchain/pull/30909: need to retain "empty" reasoning output when streaming, e.g., ```python {'id': 'rs_...', 'summary': [], 'type': 'reasoning'} ``` Tested by existing integration tests, which are currently failing. --- libs/partners/openai/langchain_openai/chat_models/base.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/libs/partners/openai/langchain_openai/chat_models/base.py b/libs/partners/openai/langchain_openai/chat_models/base.py index 3c6a9ba6871..6a5dc7a599d 100644 --- a/libs/partners/openai/langchain_openai/chat_models/base.py +++ b/libs/partners/openai/langchain_openai/chat_models/base.py @@ -3411,14 +3411,16 @@ def _convert_responses_chunk_to_generation_chunk( ) elif chunk.type == "response.refusal.done": additional_kwargs["refusal"] = chunk.refusal + elif chunk.type == "response.output_item.added" and chunk.item.type == "reasoning": + additional_kwargs["reasoning"] = chunk.item.model_dump( + exclude_none=True, mode="json" + ) elif chunk.type == "response.reasoning_summary_part.added": additional_kwargs["reasoning"] = { - "type": "reasoning", - "id": chunk.item_id, # langchain-core uses the `index` key to aggregate text blocks. "summary": [ {"index": chunk.summary_index, "type": "summary_text", "text": ""} - ], + ] } elif chunk.type == "response.reasoning_summary_text.delta": additional_kwargs["reasoning"] = {