From 86ef758a9a13731661795fe9f2ef4aba9f16d34b Mon Sep 17 00:00:00 2001 From: eason <85663565+mango766@users.noreply.github.com> Date: Sat, 14 Mar 2026 18:22:14 +0800 Subject: [PATCH] fix: prevent ValueError when removing already-removed API key in retry loop (#6193) In _handle_api_error(), when a 429 rate-limit is encountered, the code calls available_api_keys.remove(chosen_key). If the same key was already removed in a previous retry iteration (e.g. the key rotated back to the same value), this raises ValueError which crashes the entire LLM request with an opaque error instead of a proper retry/fallback. Add a membership check before calling remove() to prevent the crash. Co-authored-by: easonysliu --- astrbot/core/provider/sources/openai_source.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/astrbot/core/provider/sources/openai_source.py b/astrbot/core/provider/sources/openai_source.py index adee24073..c40234ed4 100644 --- a/astrbot/core/provider/sources/openai_source.py +++ b/astrbot/core/provider/sources/openai_source.py @@ -629,7 +629,8 @@ class ProviderOpenAIOfficial(Provider): # 最后一次不等待 if retry_cnt < max_retries - 1: await asyncio.sleep(1) - available_api_keys.remove(chosen_key) + if chosen_key in available_api_keys: + available_api_keys.remove(chosen_key) if len(available_api_keys) > 0: chosen_key = random.choice(available_api_keys) return (