fix: defensive error body extraction to handle None and non-dict cases

Addresses review feedback: body.get('error', {}).get('message') is unsafe
when body['error'] is None or not a dict. Now properly checks isinstance()
before accessing nested fields, and falls back to top-level message field.
This commit is contained in:
Otto
2026-02-09 07:42:06 +00:00
parent 5fd1482944
commit b334f1a843

View File

@@ -1119,10 +1119,12 @@ async def _stream_chat_chunks(
error_details = _extract_api_error_details(e)
if error_details.get("response_body"):
body = error_details["response_body"]
if isinstance(body, dict) and body.get("error", {}).get(
"message"
):
error_text = body["error"]["message"]
if isinstance(body, dict):
err = body.get("error")
if isinstance(err, dict) and err.get("message"):
error_text = err["message"]
elif body.get("message"):
error_text = body["message"]
if _is_region_blocked_error(e):
error_code = "MODEL_NOT_AVAILABLE_REGION"