4xx in an LLM Stream, Retried as Empty Replies
The model API kept returning 402, yet the logs showed a batch of empty-reply warnings and backoff retries. Errors and content share one async generator; a middle layer that keeps only content turns the error into an empty stream. One caller was fixed in July; six more have the same code.
Table of Contents
On July 30 the logs filled with more than 70 ERROR lines in two minutes. The model API was returning 402, but each 402 was followed by the profile-extraction task reporting an empty reply, backing off, retrying, and getting 402 again. A 402 is a billing state; retrying never changes it. The extraction task, though, never saw a 402.
Model calls in Autarkia return an async generator. Content is yielded piece by piece, and errors travel the same stream as a dict with an error key. The task scheduling layer in the middle forwarded it like this:
async for chunk in llm_module.stream_chat(messages):
if isinstance(chunk, dict):
content = chunk.get("content") or chunk.get("text") or ""
if content:
yield contentAll it does is normalize chunks into text, written with ordinary chunks in mind. A dict with only an error key finds neither content nor text and yields nothing. By the time it reaches the extraction task the stream is empty. Retrying an empty reply is a reasonable reaction, so it retried twice, and since this was a batch of tasks submitted concurrently, every one of them did the same.
Three things changed that day: the client marks 401, 402 and 403 as non_retryable while other 4xx and 5xx still retry; the scheduling layer passes dicts with error through unchanged; and the extraction task stops retrying when it sees the mark.
01How errors ended up in the content stream
Errors and content sharing one stream dates back to last December. Before that there wasn’t even a dict: when the local model ran out of context, the scheduling engine put a user-facing sentence into the queue.
queue.put("这次对话的内容有点长了……") # 上下文超限
# 上层
async for token in stream:
if first:
logger.info("First token received")
first = False
text += tokenThe caller logged First token received and Stream generation completed as usual. The user saw a notice; the logs showed a successful generation. That fix gave errors their own shape, {"error": message, "done": True}, passed up unchanged through every layer. Strings and dicts were now distinct, but they still came through the same async for.
02The other six callers
The scheduling layer now passes errors through, so the stream mixes two types: content is a string, errors are dicts. I listed every consumer of the stream. Three of nine check for errors. The other six look like the journal generator:
for attempt in range(3):
raw_out = ""
async for chunk in scheduler.submit_llm_task(messages, ...):
if isinstance(chunk, str):
raw_out += chunk
elif isinstance(chunk, dict) and chunk.get("content"):
raw_out += str(chunk.get("content") or "")
if raw_out.strip():
return raw_out
logger.warning(f"Journal LLM 返回空内容 (attempt {attempt + 1})")
await asyncio.sleep(2 * (attempt + 1))A 401, 402 or 403 arrives, the dict has no content and is skipped; raw_out is empty, the log says the reply was empty, and it backs off and retries three times. The non_retryable mark is right there in the dict, and nobody reads it. July’s fix covered only the extraction task.
As long as errors and content share a channel, every consumer has to remember to pick the errors out, and whoever writes the normal path is thinking only about content. If the scheduling layer raised an exception on error instead, the stream would carry only content, the callers’ existing except blocks would catch it, and nothing would depend on each caller remembering.