Skip to content

Python: Reject Gemini candidate_count other than 1, including zero - #10188

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
Wilson0511:fix/gemini-candidate-count-single-candidate
Nov 11, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
Wilson0511:fix/gemini-candidate-count-single-candidate

Conversation

@Wilson0511

Copy link
Copy Markdown
Contributor

Python: Reject Gemini candidate_count other than 1, including zero

Fixes #9928
Replaces #9929 with the semantics requested in its review (apologies for the silence that let it close — picking it back up on current main).

Description

candidate_count still flows to the Gemini API through the generic option passthrough, but both the streaming and non-streaming parsers read candidates[0] only — any count other than one is billed for every candidate while all but the first are silently discarded. As noted in the #9929 review, the default should be to remove the option altogether, and if passed it needs to be 1: zero (previously accepted) is rejected along with counts above one, and the stringified "1" some config paths produce is accepted and normalized to an int for GenerateContentConfig. The rejection raises ChatClientInvalidRequestException up front in _prepare_config, mirroring the Ollama client's explicit rejection of unsupported tool_choice modes.

Regression tests

  • test_candidate_count_other_than_one_is_rejected — parametrized over 0, "0", 2, "2", -1: all rejected before the request is built (red on current main: the option flows through untouched)
  • test_candidate_count_one_passes_through_normalized — 1 and "1" both accepted, config carries the int
  • test_candidate_count_absent_leaves_config_unset — no option, no config field

Full test_gemini_client.py: 217 passed, 9 skipped, zero regressions. Analysis, fix and tests were prepared with AI assistance; the push and this PR are my own review and action.

Both the streaming and non-streaming parsers read candidates[0] only, so
any candidate_count other than one is billed while every candidate but
the first is silently dropped. Reject the option up front in
_prepare_config unless it equals 1 (int or the stringified form, which
is normalized), mirroring the Ollama client's explicit rejection of
unsupported options. Zero is rejected along with counts above one.

Replaces microsoft#9929 with the semantics requested in its review.

Fixes microsoft#9928

Co-Authored-By: ZCode <zcode@bigmodel.ai>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the Gemini candidate-count validation change.

No open issues 🎉

What changed in this PR

This PR addresses #9928 by rejecting unsupported Gemini candidate counts before requests are built, preventing responses from being silently truncated to the first candidate.

Changes:

  • Rejects candidate_count values other than 1 or "1", including zero.
  • Normalizes accepted values to integer 1 and leaves the option unset when omitted.
  • Adds regression tests for rejected, accepted, and omitted values.
File Description
python/​packages/​gemini/​tests/​test_gemini_client.py Adds candidate-count validation and normalization tests.
python/​packages/​gemini/​agent_framework_gemini/​_chat_client.py Validates and normalizes candidate counts during config preparation.

Review effort: Balanced

@eavanvalkenburg

Copy link
Copy Markdown
Member

Wilson_Jiang (@Wilson0511), the current-head code review is clean and checks are green. Before approval, please update the PR description to complete the repository template's Contribution Checklist, including contribution-guideline compliance, the agreed-issue/no-competing-PR attestation, and breaking-change status. The missing AI Assistance checkbox alone is not a blocker.

Merged via the queue into microsoft:main with commit fd12c25 Nov 11, 2026
55 checks passed

This branch was previously deployed

1 inactive deployment
github-app-auth — 4c237fa4 Deployed Nov 9, 2026 by Wilson0511 via team_check #8994
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: Gemini chat client silently drops all but the first candidate when candidate_count > 1

3 participants