Skip to content

Agent Zero AI framework

Fallback when Responses mock parses SSE

Alessandro038f88848d53
diff --git a/helpers/litellm_transport.py b/helpers/litellm_transport.py
index d456e367f..a3dd40bf8 100644
--- a/helpers/litellm_transport.py
+++ b/helpers/litellm_transport.py
@@ -1715,6 +1715,8 @@ def _is_responses_not_supported_error(exc: Exception) -> bool:
text = _exception_text(exc).lower()
if any(marker in text for marker in ("429", "too many requests", "rate limit")):
return False
+ if _is_sse_json_decode_error(exc):
+ return True
if _is_bad_request_error(exc) and _looks_like_responses_request_rejected(text):
return True
if _is_server_error(exc) and _looks_like_responses_endpoint(text):
@@ -1777,6 +1779,26 @@ def _is_server_error(exc: Exception) -> bool:
)
+def _is_sse_json_decode_error(exc: Exception) -> bool:
+ current: BaseException | None = exc
+ while current is not None:
+ if isinstance(current, json.JSONDecodeError) and _looks_like_sse_payload(
+ current.doc
+ ):
+ return True
+ current = current.__cause__ or (
+ current.__context__ if current.__context__ is not current.__cause__ else None
+ )
+ return _looks_like_sse_payload(_exception_text(exc))
+
+
+def _looks_like_sse_payload(text: Any) -> bool:
+ if not isinstance(text, str):
+ return False
+ lowered = text.lstrip().lower()
+ return lowered.startswith("event:") and "\ndata:" in lowered
+
+
def _looks_like_responses_request_rejected(text: str) -> bool:
if "/v1/responses" in text or "responses api" in text:
return True
diff --git a/helpers/litellm_transport.py.dox.md b/helpers/litellm_transport.py.dox.md
index 2bfa0783c..add4be9ab 100644
--- a/helpers/litellm_transport.py.dox.md
+++ b/helpers/litellm_transport.py.dox.md
@@ -29,6 +29,7 @@
- Prefer Responses API when configured, but fallback to Chat Completions when the provider does not support Responses.
- Fall back to Chat Completions when a Responses request is rejected before any output by an endpoint-specific or shape-specific Bad Request indicating the provider cannot parse Responses payloads.
- Fall back to Chat Completions when a Responses endpoint fails before output with an endpoint-specific server error, proxy path-unavailable error, or LiteLLM proxy-extra import error.
+- Fall back to Chat Completions when LiteLLM's Responses mock streaming path tries to JSON-decode a real SSE stream before any output.
- Preserve Chat Completions tool calls from both non-streaming responses and streaming deltas as canonical `LLMResult` function-call items.
- Preserve provider-state metadata when Responses API calls succeed, and fall back to local replay when provider state is unsupported.
- Keep prompt-cache markers only for providers that accept them.
diff --git a/tests/test_stream_tool_early_stop.py b/tests/test_stream_tool_early_stop.py
index bdc9e5acb..854779d39 100644
--- a/tests/test_stream_tool_early_stop.py
+++ b/tests/test_stream_tool_early_stop.py
@@ -1,3 +1,4 @@
+import json
import sys
from pathlib import Path
@@ -643,6 +644,55 @@ async def test_unified_call_falls_back_for_proxy_responses_failures(
assert calls == ["responses", "chat"]
+async def test_unified_call_falls_back_when_responses_mock_reads_sse_as_json(
+ monkeypatch,
+):
+ calls: list[str] = []
+ sse_error = json.JSONDecodeError(
+ "Expecting value",
+ 'event: response.output_text.delta\ndata: {"delta":"hello"}\n\n',
+ 0,
+ )
+ failing_stream = _FailingAsyncChunkStream(sse_error)
+
+ async def fake_aresponses(*args, **kwargs):
+ calls.append("responses")
+ return failing_stream
+
+ async def fake_acompletion(*args, **kwargs):
+ calls.append("chat")
+ assert kwargs["stream"] is True
+ assert kwargs["drop_params"] is True
+ return _AsyncChunkStream([_chunk("fallback")])
+
+ async def fake_rate_limiter(*args, **kwargs):
+ return None
+
+ monkeypatch.setattr(litellm_transport, "aresponses", fake_aresponses)
+ monkeypatch.setattr(litellm_transport, "acompletion", fake_acompletion)
+ monkeypatch.setattr(models, "apply_rate_limiter", fake_rate_limiter)
+
+ wrapper = models.LiteLLMChatWrapper(
+ model="omniroute/test-model",
+ provider="openai",
+ model_config=None,
+ )
+
+ async def response_callback(chunk: str, full: str):
+ return None
+
+ response, reasoning = await wrapper.unified_call(
+ messages=[],
+ response_callback=response_callback,
+ )
+
+ assert response == "fallback"
+ assert reasoning == ""
+ assert calls == ["responses", "chat"]
+ assert failing_stream.closed is True
+
+
@pytest.mark.asyncio
async def test_unified_call_falls_back_when_responses_bad_request_rejects_shape(
monkeypatch,