Skip to content

fix: reject multiple response candidates - #6519

Closed
svfat wants to merge 1 commit into
google:mainfrom
svfat:fix/validate-candidate-count
Closed

fix: reject multiple response candidates#6519
svfat wants to merge 1 commit into
google:mainfrom
svfat:fix/validate-candidate-count

Conversation

@svfat

@svfat svfat commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #6518.

  • reject candidate_count > 1 before sending model request
  • apply same single-candidate contract to Gemini, LiteLLM and Apigee paths
  • add regression tests for request validation and existing first-candidate response conversion

Why

ADK exposes one model result through LlmResponse, but behavior for
candidate_count > 1 was inconsistent:

  • LiteLLM ignored the option and requested one choice
  • native Gemini requested multiple candidates and ADK kept only first one
  • Apigee requested multiple choices and kept only first one

This can give false expectation that multiple results are available. It can
also waste billed candidate tokens when extra outputs are generated and then
discarded.

Complete multi-candidate support would require change to LlmResponse and
downstream event/streaming APIs. This patch keeps current single-candidate
contract and returns clear error instead of silently ignoring or discarding
results.

Testing

  • pytest -q tests/unittests/models/test_llm_response.py tests/unittests/models/test_google_llm.py tests/unittests/models/test_litellm.py tests/unittests/models/test_completions_http_client.py
    • 471 passed
  • focused tests on Python 3.10, 3.11, 3.12, 3.13 and 3.14
  • pre-commit hooks
  • Talisman

@adk-bot adk-bot added the models [Component] This issue is related to model support label Jul 30, 2026
copybara-service Bot pushed a commit that referenced this pull request Sep 5, 2026
Merge #6519

Only the first candidate was ever used; the extra ones were dropped
silently. In streaming (both LiteLLM and Gemini) this also narrows what
reaches the caller: candidates or choices carrying a non-zero index are
skipped instead of being concatenated into the first candidate's text.

PiperOrigin-RevId: 976670666
@adk-bot

adk-bot commented Sep 5, 2026

Copy link
Copy Markdown
Collaborator

Thank you @svfat for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit 25f5214.

Closing this PR as the changes are now in the main branch.

@adk-bot adk-bot added the merged [Status] This PR is merged label Sep 5, 2026
@adk-bot adk-bot closed this Sep 5, 2026
claxman added a commit to claxman/adk-python that referenced this pull request Sep 7, 2026
LlmAgent accepted GenerateContentConfig(candidate_count=2). Gemini and
Apigee then billed extra candidates and LlmResponse kept the first one.
25f5214 logs that drop. Construction now errors, same validator as
generate_content_config.tools.

svfat proposed a request-side raise in google#6519. The landed import kept the
log-only half. This check is on LlmAgent, not the adapters.

Fixes google#6518
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged [Status] This PR is merged models [Component] This issue is related to model support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

candidate_count > 1 is ignored by LiteLLM and discarded by other adapters

3 participants