fix: retry httpx timeout family as transient LLM errors

Live incident: the compatible-mode provider stalled mid-stream after
200 OK (~17s without data) and raised httpx.ReadTimeout, which was not
in the retriable exception-name set, so the call failed after 1
attempt and killed the whole run. Add ReadTimeout/ConnectTimeout/
WriteTimeout/PoolTimeout/TimeoutError/ConnectError to the transient
set in LLMErrorHandlingMiddleware (retry with backoff, then a
transient fallback message instead of generic), and add ReadTimeout/
ConnectTimeout to ai_writing's classify_exception for model
switching.
This commit is contained in:
董宏凯 committed 2026-08-30 15:53:51 +08:00
1 parent 4964967295
commit f7e6ed65bd
4 files changed
+88 -1

No files matched your search

@@ -57,7 +57,7 @@ _UPSTREAM_PATTERNS = ("bad_response_status_code", "bad response status",
"expecting ',' delimiter", "expecting value", "expecting property name",
"unexpected character", "unterminated string", "jsondecodeerror")
_TRANSIENT_EXC_NAMES = {"APITimeoutError", "APIConnectionError", "InternalServerError",
"ReadError", "RemoteProtocolError", "TimeoutError"}
"ReadError", "RemoteProtocolError", "ReadTimeout", "ConnectTimeout", "TimeoutError"}
def classify_exception(exc: BaseException) -> str:
@@ -226,6 +226,15 @@ class LLMErrorHandlingMiddleware(AgentMiddleware[AgentState]):
"InternalServerError",
"ReadError", # httpx.ReadError: connection dropped mid-stream
"RemoteProtocolError", # httpx: server closed connection unexpectedly
# httpx.TimeoutException family escapes the streaming path unwrapped
# (seen live: httpx.ReadTimeout mid-generation after 200 OK — the
# provider stalled ~17s; that must retry, not fail the whole run).
"ReadTimeout",
"ConnectTimeout",
"WriteTimeout",
"PoolTimeout",
"TimeoutError", # asyncio chunk timeout / builtin timeout
"ConnectError", # httpx: connection refused / DNS blip
}:
return True, "transient"
if status_code in _RETRIABLE_STATUS_CODES: