Skip to content

Commit d9e7191

Browse files
committed
fix(brains): raise on Cohere upstream HTTP error instead of empty stream
A non-2xx response from Cohere's /v2/chat (429 rate limit, 401 bad key, 5xx) returns a JSON error body rather than newline-delimited content-delta events. The stream loop skipped every non-event line and ended yielding nothing, so the hub returned a silent empty completion with no diagnostic. The anthropic and openai-compat adapters already guard this; mirror that behaviour here and surface the status + body snippet as a RuntimeError.
1 parent e06dc03 commit d9e7191

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

zhub/brains/cohere.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ async def stream(
9191
async with self._http.stream(
9292
"POST", f"{_BASE_URL}/v2/chat", json=body, headers=headers,
9393
) as response:
94+
# Non-2xx (429 rate limit, 401 bad key, 5xx) returns a JSON error
95+
# body, not newline-delimited content-delta events, so the loop
96+
# below would skip every line and end yielding nothing — a silent
97+
# empty completion. Raise so the publisher surfaces a real error.
98+
# getattr default keeps test doubles working; real httpx responses
99+
# always have status_code.
100+
status = getattr(response, "status_code", 200)
101+
if status >= 400:
102+
try:
103+
detail = (await response.aread()).decode("utf-8", "replace").strip()
104+
except Exception:
105+
detail = ""
106+
snippet = f": {detail[:300]}" if detail else ""
107+
raise RuntimeError(f"upstream returned HTTP {status}{snippet}")
94108
async for line in response.aiter_lines():
95109
line = line.strip()
96110
if not line:

0 commit comments

Comments
 (0)