Skip to content

fix(gemini): normalize thinkingConfig wire casing, safety OFF/civic_integrity, and per-model overrides - #11614

Open
JosephTian876 wants to merge 1 commit into
AstrBotDevs:masterfrom
JosephTian876:fix/gemini-thinking-camelcase-and-safety
Open

JosephTian876 wants to merge 1 commit into
AstrBotDevs:masterfrom
JosephTian876:fix/gemini-thinking-camelcase-and-safety

Conversation

@JosephTian876

Copy link
Copy Markdown
Contributor

Summary

Fixes five interoperability and configuration issues in the native Google Gemini provider (ProviderGoogleGenAI):

  1. Serialize generationConfig.thinkingConfig keys in strict camelCase on the wire

    • google-genai (verified on both 2.34.0 and 3.6.1) lacks _ThinkingConfig_to_mldev / _ThinkingConfig_to_vertex in google.genai.models._GenerateContentConfig_to_mldev, so convert_to_dict(..., convert_keys=False) emits snake_case keys (thinking_level, thinking_budget, include_thoughts) instead of the documented REST camelCase keys (thinkingLevel, thinkingBudget, includeThoughts).
    • While Google's official endpoint tolerates both casings via Proto3 JSON, strict camelCase OpenAI/Gemini relays (e.g., Go encoding/json proxies such as New-API / One-API) silently drop snake_case fields, causing thinking configuration to be ignored.
    • Introduces a lightweight _GeminiAsyncClient(httpx.AsyncClient) overriding the public build_request() hook to normalize snake_case keys inside generationConfig.thinkingConfig to camelCase for :generateContent and :streamGenerateContent POST bodies (fast-path byte guard when no snake_case markers exist; recomputes Content-Length cleanly; works transparently across direct connections, proxies, and custom transports).
  2. Add HarmBlockThreshold.OFF and HARM_CATEGORY_CIVIC_INTEGRITY safety settings

    • Adds "OFF": types.HarmBlockThreshold.OFF to THRESHOLD_MAPPING and "civic_integrity": types.HarmCategory.HARM_CATEGORY_CIVIC_INTEGRITY to CATEGORY_MAPPING.
    • Exposes OFF and civic_integrity in astrbot/core/config/default.py and all four dashboard i18n locale metadata files (zh-CN, en-US, ja-JP, ru-RU), without forcing civic_integrity onto existing configs unless explicitly configured.
  3. Guard thinking_budget=0 on gemini-2.5-pro*

    • Gemini 2.5 Pro does not allow disabling thinking (thinking_budget=0 triggers HTTP 400; legacy configs often retain budget: 0).
    • Detects thinking_budget == 0 on gemini-2.5-pro*, logs a warning suggesting -1 (dynamic) or >=128, and omits thinking_budget instead of sending an invalid value. gemini-2.5-flash / gemini-2.5-flash-lite continue to accept 0.
  4. Widen Gemini 2.5 thinking allow-list for -thinking and -latest relay aliases

    • Extends _GEMINI_25_THINKING_MODEL_RE to match -thinking and -latest variants (e.g., gemini-2.5-pro-thinking, gemini-2.5-flash-thinking-05-20, gemini-2.5-pro-preview-05-06-thinking, gemini-2.5-flash-latest) while preserving strict default-deny exclusion of non-thinking 2.5 variants (image, tts, native-audio, computer-use).
  5. Allow safe per-model thinking & safety overrides in custom_extra_body + tighten Gemini 3 level validation

    • Supports per-model overrides via custom_extra_body (thinking_config/thinkingConfig, thinking_level/thinkingLevel/reasoning_effort, thinking_budget/thinkingBudget, include_thoughts/includeThoughts, and safety_settings/safetySettings as either a category-merge dict or a Gemini REST list), while still raising ValueError on unknown parameters.
    • Tightens Gemini 3 allowed levels: gemini-3-pro* accepts LOW/HIGH; gemini-3.1-pro* / 3.7 / 3.8 accept LOW/MEDIUM/HIGH; gemini-3-flash* accepts MINIMAL/LOW/MEDIUM/HIGH. Preserves Gemini 4 behavior (ignoring legacy gm_thinking_config.level in favor of explicit per-model reasoning_effort / thinking_level).

Files Changed

  • astrbot/core/provider/sources/gemini_source.py
  • astrbot/core/config/default.py
  • dashboard/src/i18n/locales/{en-US,zh-CN,ja-JP,ru-RU}/features/config-metadata.json
  • tests/test_gemini_source.py

Verification

  • ruff format --check . & ruff check .: passed (606 files formatted, 0 lint warnings).
  • pytest tests/test_gemini_source.py tests/test_provider_user_agent.py tests/test_dashboard.py: 169 passed, 2 skipped.
  • pytest tests/test_fastapi_v1_dashboard.py -k "provider": 11 passed.
  • Verified real wire JSON payloads (both non-streaming :generateContent and streaming :streamGenerateContent) via httpx.MockTransport on both google-genai==3.6.1 and google-genai==2.34.0: confirmed strict camelCase (thinkingLevel, thinkingBudget, includeThoughts), zero snake_case leaks, matching Content-Length, and OFF/HARM_CATEGORY_CIVIC_INTEGRITY serialization.

建议合并顺序 (Recommended Merge Order)

  • All 6 AstrBot core PRs are mutually conflict-free (git merge-tree --write-tree verified across all 15 pairs):
    1. fix/plugin-service-closed-connector
    2. fix/t2i-local-skip-external-probe
    3. fix/aiocqhttp-at-member-fallback
    4. fix/system-one-decision-float32-softmax-tol
    5. fix/gemini-thinking-camelcase-and-safety (this PR) — recommended before fix/gemini-openai-extra-body-guard so native Gemini thinking & safety controls are already landed.
    6. fix/gemini-openai-extra-body-guard

@sourcery-ai

sourcery-ai Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Sorry @JosephTian876, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 1 day and 7 hours by commenting @sourcery-ai review. Upgrade to get a review now.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant