From b334f1a843790cd302447d1118ee0ff7c82daa71 Mon Sep 17 00:00:00 2001 From: Otto Date: Mon, 9 Feb 2026 07:42:06 +0000 Subject: [PATCH] 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. --- .../backend/backend/api/features/chat/service.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/autogpt_platform/backend/backend/api/features/chat/service.py b/autogpt_platform/backend/backend/api/features/chat/service.py index 1074dda438..6d68c24dd0 100644 --- a/autogpt_platform/backend/backend/api/features/chat/service.py +++ b/autogpt_platform/backend/backend/api/features/chat/service.py @@ -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"