Skip to content

fix(agents): reject candidate_count greater than one - #7039

Open
claxman wants to merge 1 commit into
google:mainfrom
claxman:fix/6518-reject-candidate-count
Open

fix(agents): reject candidate_count greater than one#7039
claxman wants to merge 1 commit into
google:mainfrom
claxman:fix/6518-reject-candidate-count

Conversation

@claxman

@claxman claxman commented Sep 7, 2026

Copy link
Copy Markdown

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:
LlmAgent(name='x', generate_content_config=GenerateContentConfig(candidate_count=2)) constructs with candidate_count 2. Extra candidates are dropped at LlmResponse.create, which keeps candidates[0]. 25f5214 (from #6519) logs the drop in the adapters but construction stays silent.

Issue script on b0180620, PYTHONPATH=src:

LiteLLM request params: None
LiteLLM returned texts: ['first', 'second']
ADK converted texts: ['first']

Solution:
Reject candidate_count > 1 in LlmAgent.validate_generate_content_config, the validator that already rejects tools, system_instruction, response_schema, and http_options.base_url. The error names generate_content_config= and says extras are discarded. candidate_count=1 stays allowed.

Adapters are left at 25f5214. #6519 put the check there and the import kept only the log. Mapping candidate_count to n and returning extras is out: LlmResponse is single-candidate. svfat filed the issue and the first raise.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

New tests: test_validate_generate_content_config_candidate_count_throw and test_validate_generate_content_config_candidate_count_one_allowed in tests/unittests/agents/test_llm_agent_fields.py, plus a case in tests/unittests/agents/test_llm_agent_error_messages.py::TestValidateGenerateContentConfigErrors.

PYTHONPATH=src .venv/bin/python -m pytest -q \
  tests/unittests/agents/test_llm_agent_fields.py::test_validate_generate_content_config_candidate_count_throw \
  tests/unittests/agents/test_llm_agent_fields.py::test_validate_generate_content_config_candidate_count_one_allowed \
  tests/unittests/agents/test_llm_agent_error_messages.py::TestValidateGenerateContentConfigErrors

6 passed.

Removed the validator block and reran the two throw tests. Both failed with DID NOT RAISE ValueError. Restored the block.

PYTHONPATH=src .venv/bin/python -m pytest -q tests/unittests -n auto

13981 passed, 84 skipped, 27 xfailed, 2 xpassed in 137s.

Manual End-to-End (E2E) Tests:

No live provider key. After the change, LlmAgent(name='x', generate_content_config=types.GenerateContentConfig(candidate_count=2)) raises a pydantic ValidationError whose value error is candidate_count must be 1 or unset. LlmResponse keeps one candidate, so extra values are requested then discarded. Pass generate_content_config=types.GenerateContentConfig() without candidate_count, or set candidate_count=1.

Additional context

Asked xuanyang15 on #6518 whether construct-time raise is wanted after 25f5214. #6518 (comment)

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

@google-cla

google-cla Bot commented Sep 7, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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
@claxman
claxman force-pushed the fix/6518-reject-candidate-count branch from 1971fe5 to 40864d4 Compare September 7, 2026 02:56
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.

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

2 participants