From ba902e30a79d47bbc2c52b9a27ecfca669d237ee Mon Sep 17 00:00:00 2001 From: Kirill Kirilenko Date: Sun, 15 Feb 2026 22:40:15 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D0=BE=D0=B7=D0=B2=D1=80=D0=B0=D1=82=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE=D0=B9=20=D0=BE=D1=88?= =?UTF-8?q?=D0=B8=D0=B1=D0=BA=D0=B8=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B0=D0=B9?= =?UTF-8?q?=D0=B4=D0=B5=D1=80=D0=B0=20=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=BE?= =?UTF-8?q?=20"Provider=20returned=20error".?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ai_agent.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/ai_agent.py b/ai_agent.py index 303fb3d..c2d66b0 100644 --- a/ai_agent.py +++ b/ai_agent.py @@ -12,7 +12,7 @@ from typing import List, Tuple, Any, Optional, Union, Dict, Awaitable from openrouter import OpenRouter, RetryConfig from openrouter.components import AssistantMessage, AssistantMessageTypedDict, ChatMessageContentItemTypedDict, \ ChatMessageToolCall, MessageTypedDict, ToolDefinitionJSONTypedDict -from openrouter.errors import ResponseValidationError +from openrouter.errors import ResponseValidationError, ChatError from openrouter.utils import BackoffStrategy from database import BasicDatabase @@ -364,11 +364,23 @@ class AiAgent: # https://github.com/OpenRouterTeam/python-sdk/issues/44 body = json.loads(e.body) if "error" in body: - raw_response = json.loads(body["error"]["metadata"]["raw"]) - message = str(raw_response["error"]["message"]) - raise RuntimeError(message) - else: - raise RuntimeError("Ошибка валидации ответа от провайдера.") from e + try: + raw_response = json.loads(body["error"]["metadata"]["raw"]) + message = str(raw_response["error"]["message"]) + e = RuntimeError(message) + except Exception: + pass + raise e + except ChatError as e: + if e.message == "Provider returned error": + body = json.loads(e.body) + try: + raw_response = json.loads(body["error"]["metadata"]["raw"]) + message = str(raw_response["error"]["message"]) + e = RuntimeError(message) + except Exception: + pass + raise e agent: AiAgent