fix: treat a reasoning-only stream drop as a drop, not a clean stop

The text-only drop guard in _finish_chat_stream required content_parts,
so a stream that died while still emitting delta.reasoning (no
finish_reason, no usage) fell through to the synthesized "stop". With
the reasoning-only clean-stop promotion in finish_text_response that
stamped "stop" turned the truncated thought into the final answer,
where main entered the continuation ladder. Extend the guard with
reasoning_parts so the drop yields the partial-stream stub and the
ladder still runs; a real clean stop carries finish_reason="stop" and
is unaffected.

Review follow-up on #110227.
This commit is contained in:
teknium1
2026-09-13 13:44:40 -07:00
committed by Teknium
parent 28931c5c04
commit a8843ab985
2 changed files with 35 additions and 3 deletions
+5 -3
View File
@@ -2948,9 +2948,11 @@ class _StreamingCall(StreamingWaitMonitor):
_dropped_names)
return _build_partial_stream_stub(
role, full_content, full_reasoning, model_name, usage_obj, dropped_tool_names=_dropped_names or None)
if finish_reason is None and content_parts and not tool_calls_acc and usage_obj is None:
# Text-only drop: otherwise the partial text is stamped "stop" and the next step is
# lost. A usage object proves the provider finished (include_usage's final chunk).
if finish_reason is None and (content_parts or reasoning_parts) and not tool_calls_acc and usage_obj is None:
# Text-only (or reasoning-only) drop: otherwise the partial text is stamped "stop"
# and the next step is lost — for reasoning-only, the clean-stop promotion in
# finish_text_response would then surface a truncated thought as the answer.
# A usage object proves the provider finished (include_usage's final chunk).
logger.warning(
"Stream ended with no finish_reason after delivering text with no tool calls; treating as a mid-stream drop.")
return _build_partial_stream_stub(role, full_content, full_reasoning, model_name, usage_obj)
@@ -975,6 +975,36 @@ class TestStreamIncludeUsageFinalChunk:
assert response.choices[0].finish_reason == FINISH_REASON_LENGTH
assert response.choices[0].message.content == "Partial text before drop"
@patch("run_agent.AIAgent._create_request_openai_client")
@patch("run_agent.AIAgent._close_request_openai_client")
def test_reasoning_only_abrupt_drop_without_usage_still_returns_stub(
self, _mock_close, mock_create, monkeypatch,
):
"""A stream severed while still reasoning (only ``delta.reasoning`` frames,
no finish_reason, no usage) is a drop, not a clean stop: stamping "stop"
would let the reasoning-only clean-stop promotion surface the truncated
thought as the final answer instead of entering the continuation ladder."""
def _dropped_stream():
for text in ("Let me think about", " the question carefully, first"):
chunk = _make_stream_chunk()
chunk.choices[0].delta.reasoning = text
yield chunk
mock_client = MagicMock()
mock_client.chat.completions.create.side_effect = lambda *a, **kw: _dropped_stream()
mock_create.return_value = mock_client
agent = _make_agent()
monkeypatch.setenv("HERMES_STREAM_RETRIES", "0")
response = agent._interruptible_streaming_api_call({})
assert response.id == PARTIAL_STREAM_STUB_ID
assert response.choices[0].finish_reason == FINISH_REASON_LENGTH
assert response.choices[0].message.content is None
assert response.choices[0].message.reasoning_content == (
"Let me think about the question carefully, first"
)
# ── Merged-finish content chunk swallowed by the SSE-echo guard (#94614) ──