From 7ded01710b2dff77f1ed86c0f59f79ee0c5e82c8 Mon Sep 17 00:00:00 2001 From: JosephTian876 Date: Sun, 22 Nov 2026 05:19:38 +0800 Subject: [PATCH] fix(provider): support Gemini relay aliases and dated 2.5 thinking models After #11219 and #11251 landed Gemini 4 reasoning_effort and Gemini 3.8 thinking level support on master, narrow this change to the two remaining matching gaps in ProviderGoogleGenAI._prepare_query_config(): - Normalize relay-decorated model aliases (such as [AN]gemini-4-hydrogen or full-width prefixed ids) down to their canonical lowercase Gemini id so Gemini 4 reasoning_effort / sampling-param stripping, Gemini 3 thinkingLevel rules, and Gemini 2.5 thinkingBudget all apply cleanly. - Match dated/preview/exp Gemini 2.5 thinking families (pro, flash, flash-lite) via a default-deny allow-list so dated preview ids receive thinkingBudget while non-thinking 2.5 variants (image, tts, native-audio, computer-use) remain excluded. --- .../core/provider/sources/gemini_source.py | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/astrbot/core/provider/sources/gemini_source.py b/astrbot/core/provider/sources/gemini_source.py index 99bd7b83b2f..2c256698c30 100644 --- a/astrbot/core/provider/sources/gemini_source.py +++ b/astrbot/core/provider/sources/gemini_source.py @@ -3,6 +3,7 @@ import json import logging import random +import re from collections.abc import AsyncGenerator from contextlib import AsyncExitStack, aclosing, asynccontextmanager from typing import Literal, cast @@ -48,6 +49,32 @@ "code_execution_result", ) +_GEMINI_MODEL_ID_RE = re.compile(r"(? str: + """Extract the canonical lowercase Gemini model id from a path or relay alias. + + Args: + model: Raw model identifier from the request payload or provider config, + which may include resource path prefixes or relay decorations. + + Returns: + Lowercase canonical Gemini model id when a ``gemini-`` token is + present, otherwise the stripped lowercase trailing path segment. + """ + name = model.rsplit("/", 1)[-1].strip().lower() + match = _GEMINI_MODEL_ID_RE.search(name) + return match.group(0) if match else name + class SuppressNonTextPartsWarning(logging.Filter): """过滤 Gemini SDK 中的非文本部分警告""" @@ -244,7 +271,7 @@ async def _prepare_query_config( fields, or the SDK rejects their values. """ model = cast(str, payloads.get("model", self.get_model())) - model_name = model.rsplit("/", 1)[-1].lower() + model_name = _normalize_gemini_model_name(model) is_gemini_4 = model_name.startswith("gemini-4") if not modalities or is_gemini_4: modalities = ["TEXT"] @@ -336,7 +363,7 @@ async def _prepare_query_config( "gemini-2.5-flash-lite-preview", "gemini-robotics-er-1.5-preview", "gemini-live-2.5-flash-preview-native-audio-09-2025", - ]: + ] or bool(_GEMINI_25_THINKING_MODEL_RE.fullmatch(model_name)): # The thinkingBudget parameter, introduced with the Gemini 2.5 series thinking_budget = thinking_settings.get("budget") if thinking_budget is not None: