Skip to content

The agent that grows with you

fix: model_extra reasoning fallback in the streaming final-response branch

teknium1eed6fa2a0a5f
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index f4516390359..eff9026a9d1 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -3045,6 +3045,8 @@ class _StreamingCall(StreamingWaitMonitor):
message = getattr(choices[0] if isinstance(choices, (list, tuple)) and choices else None, "message", None)
if message is not None:
reasoning_text = getattr(message, "reasoning_content", None) or getattr(message, "reasoning", None)
+ if reasoning_text is None and isinstance(getattr(message, "model_extra", None), dict):
+ reasoning_text = message.model_extra.get("reasoning_content") or message.model_extra.get("reasoning")
if isinstance(reasoning_text, str) and reasoning_text:
self._emit_reasoning(reasoning_text)
content = getattr(message, "content", None)
diff --git a/tests/agent/test_run_agent.py b/tests/agent/test_run_agent.py
index ec21547df60..b020a21a458 100644
--- a/tests/agent/test_run_agent.py
+++ b/tests/agent/test_run_agent.py
@@ -6704,6 +6704,19 @@ class TestStreamingApiCall:
assert resp.choices[0].message.reasoning_content == "thinking only"
assert resp.choices[0].finish_reason == "length"
+ def test_final_response_object_replays_reasoning_from_model_extra(self, agent):
+ """The 'completed response instead of an iterator' branch reads reasoning through the
+ same ``model_extra`` fallback as the delta path, so it is still shown (#56516)."""
+ message = SimpleNamespace(content="done", tool_calls=None, model_extra={"reasoning": "thought"})
+ final = SimpleNamespace(model="m", choices=[SimpleNamespace(message=message, finish_reason="stop")])
+ agent.client.chat.completions.create.return_value = final
+ agent.reasoning_callback = MagicMock()
+
+ resp = agent._interruptible_streaming_api_call({"messages": []})
+
+ assert resp is final
+ agent.reasoning_callback.assert_called_once_with("thought")
+
def test_model_name_captured(self, agent):
chunks = [
_make_chunk(content="Hi", model="gpt-4o"),