Skip to content

The agent that grows with you

fix(streaming): extract finish_reason/usage before content-shape continues

Jon Nielsend304422b3db5
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index 28cfb542bca..0175469ed65 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -4317,6 +4317,18 @@ def interruptible_streaming_api_call(agent, api_kwargs: dict, *, on_first_delta=
if hasattr(chunk, "model") and chunk.model:
model_name = chunk.model
+ # Extract terminal chunk fields BEFORE any content-shape `continue`.
+ # Backends that merge finish_reason into the final content chunk
+ # (e.g. vLLM >= 0.1.dev20051) can have that chunk swallowed by the
+ # SSE-echo guard below when the tokenizer emits standalone ':'
+ # tokens — the finish chunk would never register and the response
+ # would be falsely flagged as truncated (#94614).
+ chunk_finish_reason = getattr(chunk.choices[0], "finish_reason", None)
+ if chunk_finish_reason:
+ finish_reason = chunk_finish_reason
+ if hasattr(chunk, "usage") and chunk.usage:
+ usage_obj = chunk.usage
+
# Accumulate reasoning content
reasoning_text = getattr(delta, "reasoning_content", None) or getattr(delta, "reasoning", None)
if reasoning_text:
@@ -4452,13 +4464,10 @@ def interruptible_streaming_api_call(agent, api_kwargs: dict, *, on_first_delta=
# discarding the attempted action.
result["partial_tool_names"].append(name)
- chunk_finish_reason = getattr(chunk.choices[0], "finish_reason", None)
- if chunk_finish_reason:
- finish_reason = chunk_finish_reason
-
- # Usage in the final chunk
- if hasattr(chunk, "usage") and chunk.usage:
- usage_obj = chunk.usage
+ # (finish_reason/usage are now extracted at the top of the loop
+ # body. The old tail-side extraction sat after the SSE-echo
+ # guard's `continue` paths, so a merged finish chunk that tripped
+ # the guard never registered — false "stream truncated".)
_close_managed_stream()
diff --git a/tests/run_agent/test_partial_stream_finish_reason.py b/tests/run_agent/test_partial_stream_finish_reason.py
index 78f1fe89a3a..2fd563a5f03 100644
--- a/tests/run_agent/test_partial_stream_finish_reason.py
+++ b/tests/run_agent/test_partial_stream_finish_reason.py
@@ -885,3 +885,67 @@ class TestStreamIncludeUsageFinalChunk:
assert response.id == PARTIAL_STREAM_STUB_ID
assert response.choices[0].finish_reason == FINISH_REASON_LENGTH
assert response.choices[0].message.content == "Partial text before drop"
+
+
+# ── Merged-finish content chunk swallowed by the SSE-echo guard (#94614) ──
+
+class TestMergedFinishChunkSurvivesSSEGuard:
+ """vLLM >= 0.1.dev20051 merges finish_reason into the final CONTENT
+ chunk instead of a separate marker-only chunk. When the SSE-echo guard
+ is engaged at that moment (GLM-family tokenizers emit standalone ':'
+ tokens mid-prose), the guard's content-shape continue paths swallow the
+ terminal chunk and finish_reason is never captured — a complete stream
+ is misclassified as a mid-stream drop. Terminal fields must be
+ extracted before any content-shape continue."""
+
+ @patch("run_agent.AIAgent._create_request_openai_client")
+ @patch("run_agent.AIAgent._close_request_openai_client")
+ def test_merged_finish_chunk_after_sse_lookalike_prefix_completes(
+ self, _mock_close, mock_create, monkeypatch,
+ ):
+ def _vllm_merged_finish_stream():
+ # ':' then ' uniform' engage the SSE-echo guard (may_be_sse True)
+ yield _make_stream_chunk(content=":")
+ yield _make_stream_chunk(content=" uniform")
+ # Merged-finish terminal content chunk, as the new vLLM emits
+ yield _make_stream_chunk(content=".", finish_reason="stop")
+
+ mock_client = MagicMock()
+ mock_client.chat.completions.create.side_effect = (
+ lambda *a, **kw: _vllm_merged_finish_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 == "stop"
+ assert response.choices[0].message.content == ": uniform."
+
+ @patch("run_agent.AIAgent._create_request_openai_client")
+ @patch("run_agent.AIAgent._close_request_openai_client")
+ def test_merged_finish_chunk_without_sse_trigger_still_completes(
+ self, _mock_close, mock_create, monkeypatch,
+ ):
+ """Control: the same merged-finish shape without an SSE-lookalike
+ prefix must keep completing (guard never engages)."""
+
+ def _plain_merged_finish_stream():
+ yield _make_stream_chunk(content="Hello")
+ yield _make_stream_chunk(content=".", finish_reason="stop")
+
+ mock_client = MagicMock()
+ mock_client.chat.completions.create.side_effect = (
+ lambda *a, **kw: _plain_merged_finish_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 == "stop"
+ assert response.choices[0].message.content == "Hello."