Skip to content

The agent that grows with you

fix(anthropic): drop dead Iterable guard; fold eventless-stream message into message_stop gate (#121320)

kshitijk4poor7f94891c3523
diff --git a/agent/anthropic_adapter.py b/agent/anthropic_adapter.py
index f49885436a3..559af3acc2a 100644
--- a/agent/anthropic_adapter.py
+++ b/agent/anthropic_adapter.py
@@ -10,7 +10,6 @@ import os
import re
import shutil
import subprocess
-from collections.abc import Iterable
from contextlib import suppress
from typing import Any, Dict, List, Optional
@@ -735,11 +734,10 @@ def _stream_final_message(stream_fn, api_kwargs, log_prefix, on_stream_event, on
# has given up, so abandon the stream (``with`` closes it) instead of streaming an answer
# nobody reads.
# Some SDK versions drop optional message_delta metadata from the final snapshot.
- # An iterable stream must end in message_stop; a get_final_message-only shim cannot
- # prove completion and is treated as a retryable incomplete response.
+ # The stream must end in message_stop; anything else is a retryable incomplete response.
stop_details = None
saw_message_stop = False
- for event in (stream if isinstance(stream, Iterable) else ()):
+ for event in stream:
if getattr(event, "type", None) == "message_stop":
saw_message_stop = True
if getattr(event, "type", None) == "message_delta":
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index b368c569ac4..46411c1a795 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -3313,8 +3313,7 @@ class _StreamingCall(StreamingWaitMonitor):
per-request ``request_client`` so the watchdog can abort this socket
without closing the shared client mid-flight."""
has_tool_use = False
- # Eventless stream: the SDK's get_final_message() raises AssertionError (no
- # message_start); shims may fabricate a contentless Message. All -> EmptyStreamError.
+ # No message_stop -> EmptyStreamError; saw_stream_event only picks the message.
saw_stream_event = False
saw_message_stop = False
self.last_chunk_time["t"] = time.time()
@@ -3382,19 +3381,14 @@ class _StreamingCall(StreamingWaitMonitor):
if not saw_message_stop:
raise EmptyStreamError(
"Anthropic Messages stream ended before message_stop (possible upstream stream drop)."
- )
- try:
- base_final_message = raw_stream.get_final_message()
- # The SDK snapshot keeps only stop_reason/stop_sequence from message_delta; the
- # refusal's stop_details (category/explanation) survives only in our accumulator.
- _stop_details = accumulator.finalize().get("stop_details")
- if _stop_details is not None and getattr(base_final_message, "stop_details", None) is None:
- base_final_message.stop_details = _stop_details
- except AssertionError:
- if not saw_stream_event:
- raise EmptyStreamError(
- "Provider returned an empty stream with no events (possible upstream error or malformed event stream).") from None
- raise
+ if saw_stream_event else
+ "Provider returned an empty stream with no events (possible upstream error or malformed event stream).")
+ base_final_message = raw_stream.get_final_message()
+ # The SDK snapshot keeps only stop_reason/stop_sequence from message_delta; the
+ # refusal's stop_details (category/explanation) survives only in our accumulator.
+ _stop_details = accumulator.finalize().get("stop_details")
+ if _stop_details is not None and getattr(base_final_message, "stop_details", None) is None:
+ base_final_message.stop_details = _stop_details
finally:
try:
self._close_managed_stream()