Skip to content

fix(openai): unwrap accidentally nested extra_body in custom_extra_body - #11615

Open
JosephTian876 wants to merge 1 commit into
AstrBotDevs:masterfrom
JosephTian876:fix/gemini-openai-extra-body-guard
Open

JosephTian876 wants to merge 1 commit into
AstrBotDevs:masterfrom
JosephTian876:fix/gemini-openai-extra-body-guard

Conversation

@JosephTian876

Copy link
Copy Markdown
Contributor

Summary

Guards ProviderOpenAIOfficial against accidental extra_body double-wrapping when users configure custom_extra_body from OpenAI SDK examples (particularly Google's Gemini OpenAI-compatibility docs).

Problem

Google's Gemini OpenAI-compatible documentation illustrates passing vendor options via the OpenAI Python SDK's extra_body keyword argument:

client.chat.completions.create(
    model="gemini-2.5-flash",
    messages=[...],
    extra_body={"google": {"thinking_config": {"thinking_budget": 1024}}},
)

Because AstrBot already forwards custom_extra_body as the SDK's extra_body= argument, users who paste {"extra_body": {"google": ...}} into custom_extra_body end up sending a double-wrapped wire payload:

{"model": "gemini-3-pro-preview", "messages": [...], "extra_body": {"google": {"thinking_config": {"thinking_level": "low"}}}}

Upstream endpoints ignore or reject the unexpected top-level "extra_body" key.

Changes

  • Adds ProviderOpenAIOfficial._unwrap_nested_extra_body() invoked at the start of _apply_provider_specific_request_overrides() (covering both _query() and _query_stream()):
    • Unwraps nested dict values under "extra_body" into top-level extra_body keys (bounded depth to guard against pathological nesting).
    • Preserves explicit top-level sibling keys (never overwrites them with inner duplicates) and leaves self.provider_config["custom_extra_body"] unmutated.
    • Leaves non-dict "extra_body" values untouched.
    • Emits a single per-instance warning guiding users to place keys directly in custom_extra_body.
  • Clarifies custom_extra_body schema hints in astrbot/core/config/default.py and all four dashboard locale files (zh-CN, en-US, ja-JP, ru-RU), explicitly noting not to wrap keys in another extra_body object and clarifying that OpenAI-compatible endpoints (including Gemini_OpenAI_API) do not support native Gemini safety_settings.
  • Adds focused unit and wire-level tests (both sync and streaming via httpx.MockTransport) directly in tests/test_openai_source.py.

Files Changed

  • astrbot/core/provider/sources/openai_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_openai_source.py

Verification

  • ruff format --check . & ruff check .: passed (606 files formatted, 0 lint warnings).
  • pytest tests/test_openai_source.py tests/test_provider_user_agent.py: 122 passed, 1 skipped.
  • pytest tests/test_fastapi_v1_dashboard.py -k "provider": 11 passed.

建议合并顺序 (Recommended Merge Order)

  • All 6 AstrBot core PRs are mutually conflict-free (git merge-tree --write-tree verified across all 15 pairs; hunks in default.py and locale JSONs are ~200–300 lines apart from fix/gemini-thinking-camelcase-and-safety):
    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
    6. fix/gemini-openai-extra-body-guard (this PR)

@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