Skip to content

Commit 40864d4

Browse files
committed
fix(agents): reject candidate_count greater than one
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
1 parent b018062 commit 40864d4

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎src/google/adk/agents/llm_agent.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,9 @@ class LlmAgent(BaseAgent, abc.ABC):
391391
"""The additional content generation configurations.
392392
393393
NOTE: not all fields are usable, e.g. tools must be configured via `tools`,
394-
thinking_config can be configured here or via the `planner`. If both are set, the planner's configuration takes precedence.
394+
candidate_count must be 1 or unset, and thinking_config can be configured
395+
here or via the `planner`. If both are set, the planner's configuration
396+
takes precedence.
395397
396398
For example: use this config to adjust model temperature, configure safety
397399
settings, etc.
@@ -1252,6 +1254,16 @@ def validate_generate_content_config(
12521254
' or its client, not via'
12531255
' LlmAgent.generate_content_config.http_options.base_url.'
12541256
)
1257+
if (
1258+
generate_content_config.candidate_count is not None
1259+
and generate_content_config.candidate_count > 1
1260+
):
1261+
raise ValueError(
1262+
'candidate_count must be 1 or unset. LlmResponse keeps one'
1263+
' candidate, so extra values are requested then discarded. Pass'
1264+
' generate_content_config=types.GenerateContentConfig() without'
1265+
' candidate_count, or set candidate_count=1.'
1266+
)
12551267
return generate_content_config
12561268

12571269
@override

‎tests/unittests/agents/test_llm_agent_error_messages.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ def test_response_schema_error_includes_move_guidance(self):
147147
with pytest.raises(ValueError, match=r'Move your schema'):
148148
LlmAgent.validate_generate_content_config(config)
149149

150+
def test_candidate_count_error_names_generate_content_config(self):
151+
"""candidate_count > 1 should name generate_content_config in the error."""
152+
config = types.GenerateContentConfig(candidate_count=2)
153+
with pytest.raises(
154+
ValueError, match=r'generate_content_config'
155+
) as exc_info:
156+
LlmAgent.validate_generate_content_config(config)
157+
assert 'candidate_count=1' in str(exc_info.value)
158+
150159

151160
class TestGenerateContentKwargErrors:
152161
"""Tests for misplaced GenerateContentConfig kwargs on LlmAgent."""

‎tests/unittests/agents/test_llm_agent_fields.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,24 @@ def test_validate_generate_content_config_http_options_base_url_throw():
449449
)
450450

451451

452+
def test_validate_generate_content_config_candidate_count_throw():
453+
"""candidate_count greater than 1 is rejected at LlmAgent construction."""
454+
with pytest.raises(ValueError, match=r'candidate_count must be 1 or unset'):
455+
LlmAgent(
456+
name='test_agent',
457+
generate_content_config=types.GenerateContentConfig(candidate_count=2),
458+
)
459+
460+
461+
def test_validate_generate_content_config_candidate_count_one_allowed():
462+
"""candidate_count=1 remains settable on generate_content_config."""
463+
agent = LlmAgent(
464+
name='test_agent',
465+
generate_content_config=types.GenerateContentConfig(candidate_count=1),
466+
)
467+
assert agent.generate_content_config.candidate_count == 1
468+
469+
452470
def test_validate_generate_content_config_http_options_allowed():
453471
"""Tests that request-time http options remain settable in config."""
454472
extra_body = {'tool_config': {'function_calling_config': {'mode': 'AUTO'}}}

0 commit comments

Comments
 (0)