Skip to content

The agent that grows with you

fix: detect in-stream error chunks in SSE streaming path

AlexFucuson948cca664c6cf
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index 3c5a90370ab..cc6299d39c8 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -3916,6 +3916,20 @@ def interruptible_streaming_api_call(agent, api_kwargs: dict, *, on_first_delta=
# Usage comes in the final chunk with empty choices
if hasattr(chunk, "usage") and chunk.usage:
usage_obj = chunk.usage
+ # Some OpenAI-compatible providers (DeepInfra, etc.)
+ # return validation errors as in-stream error chunks:
+ # choices=None with error_type/error_message in
+ # model_extra. Without this check the error is
+ # silently dropped and the stream ends empty →
+ # EmptyStreamError → misleading "empty stream" message
+ # and pointless retries on the same bad request. (#65631)
+ _err_type = getattr(chunk, "error_type", None)
+ _err_msg = getattr(chunk, "error_message", None)
+ if _err_type or _err_msg:
+ raise RuntimeError(
+ f"Provider in-stream error"
+ f" ({_err_type or 'unknown'}): {_err_msg or chunk}"
+ )
continue
delta = chunk.choices[0].delta