Skip to content

[Bug]: Gemini thinkingConfig breaks Gemini 4 models — thinkingBudget sent unconditionally, no model gate #4220

Closed

Description

@iluv7

Describe the bug

The Gemini adapter builds ThinkingConfig using only the budget-era shape and sends it without any model awareness, so on newer Gemini versions requests either fail or cannot be configured at all. Three related gaps:

  1. No tri-state thinking switch in core — GenerateOptions only has thinkingBudget and reasoningEffort (which the Gemini formatter ignores). There is no way to express includeThoughts: false (disable thinking) or a thinkingLevel.
  2. The formatter only emits the budget-era shape — GeminiChatFormatter.applyOptions sends includeThoughts=true + thinkingBudget whenever a budget is set. On Gemini 4 models this shape is rejected by the API.
  3. No Gemini 4 model gate — the Gemini module has no gemini-4-* handling at all, so Gemini 4-specific behavior (thinkingLevel instead of budget, output-token defaults) is missing.

Official Gemini documentation

Key statements:

  • thinkingLevel — "Controls the maximum depth of the model's internal reasoning process before it produces a response." / "Recommended for Gemini 3 or later models. Use with earlier models results in an error."
  • thinkingConfig — "An error will be returned if this field is set for models that don't support thinking."
  • includeThoughts — "Indicates whether to include thoughts in the response. If true, thoughts are returned only when available."
  • thinkingBudget — "The number of thoughts tokens that the model should generate."
  • Thinking guide: thinking_level is the main control ("main control over quality, latency, and token use") with values minimal/low/medium/high/xhigh; minimal is "Not supported on Gemini 4 models or gemini-3.8-flash"; gemini-4-carbon / gemini-4-argon default to medium, gemini-4-hydrogen to high.
  • Gemini 4 limits: max_output_tokens counts thinking tokens and is a hard cutoff (finishes with status incomplete); Gemini 4 has a shared 1M context window and a foreground default maximum of 65,536 output tokens for streaming (32,768 for Carbon/Argon non-streaming).

The docs do not spell out the budget-era vs Gemini 4 wire split; that behavior is captured by agentscope-python's implementation experience (PR #3523, merged 2026-10-29, for the tri-state switch; PR #3675, commit 3fef100e, for the Gemini 4 gate), which this issue proposes to match.

To Reproduce

On current main (2.1.0-SNAPSHOT):

// 1) Budget against a Gemini 4 model -> the API rejects thinking_budget
GeminiChatModel model = GeminiChatModel.builder()
        .apiKey("...")
        .modelName("gemini-4-argon")
        .defaultOptions(GenerateOptions.builder().thinkingBudget(4096).build())
        .build();
model.call(List.of(Msg.builder().role(MsgRole.USER).textContent("hi").build()), null, null);
// -> remote error: thinking_budget is rejected by Gemini 4

// 2) Zero budget against a budget-era model -> the API rejects a zero thinking budget
// (gemini-2.5-pro / gemini-3.1-pro-preview reject thinkingBudget=0; Python PR #3523 finding)

// 3) thinkingLevel cannot be expressed at all on main:
// GenerateOptions has no thinkingLevel field, and reasoningEffort is ignored by the Gemini formatter

Current behavior (code locations, main @ 5525ac1)

  • agentscope-core/src/main/java/io/agentscope/core/model/GenerateOptions.java:47-48,244,256 — only thinkingBudget (Integer) and reasoningEffort (String, "o1 models" per its javadoc). No includeThoughts, no thinkingLevel.
  • agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-gemini/src/main/java/io/agentscope/extensions/model/gemini/formatter/GeminiChatFormatter.java:121-130:
if (thinkingBudget != null) {
    thinkingConfigBuilder.includeThoughts(true); // hard-coded
    thinkingConfigBuilder.thinkingBudget(thinkingBudget);
    configBuilder.thinkingConfig(thinkingConfigBuilder.build());
}
  • No gemini-4 / argon / carbon reference anywhere in the Gemini module.
  • The only related test (GeminiChatModelTest.java:345-358) asserts the builder constructs; there are no wire-level thinkingConfig assertions, so all of this passes CI.
  • The bundled google-genai SDK (2.2.0 on current main; 1.80.0 at the time this issue was first written) already exposes ThinkingConfig.thinkingLevel(), so no SDK upgrade is needed.

Expected behavior / Proposed fix (parity with agentscope-python PR #3523 / agentscope-go RFC #164)

Add includeThoughts (Boolean, tri-state: null = omit) and thinkingLevel (String) to GenerateOptions — an additive, backward-compatible public API change — and gate the wire mapping on the model name:

Case thinkingConfig sent
no thinking option set omitted entirely (model default)
budget-era model, enable includeThoughts: true, thinkingBudget: <budget or 1024>
budget-era model, disable includeThoughts: false, thinkingBudget: 0
Gemini 4, enable includeThoughts: true, thinkingLevel: <level> — no budget
Gemini 4, disable includeThoughts: false — no budget, no level
  • Model gate: exactly gemini-4-argon and gemini-4-carbon, mirroring Python _is_gemini_4.
  • Level validation: uppercase-normalized LOW/MEDIUM/HIGH (Python's validated set; the documented minimal/xhigh can be widened later additively).
  • Invalid combinations fail fast with a Chat/ChatStream error before dispatch instead of surfacing as an opaque remote error: budget on Gemini 4, level on a budget-era model.
  • Separate follow-up candidate (out of scope of this issue's core fix): Gemini 4 max_output_tokens cap of 65,536 (Python _prepare_gemini_4_config).

Note on placement: Formatter.applyOptions has no model name (Formatter.java:96-97), so the gate needs to live in GeminiChatModel.buildGenerateContentConfig or the model name must be passed into the formatter.

Verification plan

Wire-fixture tests in the style of the existing Gemini formatter tests (no live API), written to fail on the unfixed base:

  • applyOptions output asserted on the built GenerateContentConfig for each row of the table (nil omits, enable+budget, default 1024, disable, Gemini 4 level path, Gemini 4 disable)
  • Invalid combinations throw before dispatch (budget on Gemini 4, level on a budget-era model)
  • Both buffered and streamed paths build the config through buildGenerateContentConfig
  • Run mvn -T1C -pl agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-gemini -am test, Spotless, and the full check since GenerateOptions is a public contract change.

Related

Environment

  • AgentScope-Java Version: 2.1.0-SNAPSHOT (main @ 5525ac1aa09e597042a259d581c51807e89bb0f5, 2026-11-16)
  • Java Version: 17
  • OS: macOS (request-shaping bug, platform-independent)

Update (2026-11-17)

All three gaps were re-verified on the latest main (5525ac1aa, google-genai 2.2.0) — the SDK bump does not change them. Implementation is in progress on branch fix/4220-gemini-4-thinking-config based on that commit.

Plan

I will open a PR implementing the fix and the verification tests described above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions