Skip to content

fix(models): forward generation config parameters in AnthropicLlm - #5513

Closed
sebastienc wants to merge 3 commits into
google:mainfrom
sebastienc:feat/anthropic-generation-config
Closed

sebastienc wants to merge 3 commits into
google:mainfrom
sebastienc:feat/anthropic-generation-config

Conversation

@sebastienc

Copy link
Copy Markdown
Contributor

Summary

Fixes #5393

Disclaimer: I'm using Claude Code to generate the following text. I find the summary is better than what I would've wrote.

AnthropicLlm was silently ignoring temperature, top_p, top_k, and stop_sequences from GenerateContentConfig. This change wires them through to both the streaming and non-streaming messages.create calls, and adds output_config.effort support via a new AnthropicGenerateContentConfig escape hatch and a ThinkingLevel → effort mapping.

  • temperature / top_p / top_k / stop_sequences — forwarded to messages.create in both streaming and non-streaming paths; each is guarded by is not None so unset params remain NOT_GIVEN rather than None
  • Sampling params disabled when effort is set — temperature, top_p, and top_k are suppressed (sent as NOT_GIVEN) when output_config.effort is active, since Anthropic does not allow sampling parameters alongside effort-based generation
  • _build_effort_param — maps ThinkingConfig.thinking_level to Anthropic's output_config.effort string (MINIMAL → "low", LOW → "medium", MEDIUM → "high", HIGH → "xhigh"); also reads AnthropicGenerateContentConfig.effort directly for cases needing "max"
  • AnthropicGenerateContentConfig — subclass of GenerateContentConfig that adds a single effort field accepting "low" | "medium" | "high" | "xhigh" | "max"; callers who need the "max" effort level (not expressible via ThinkingLevel) can pass this config instead

Testing plan

12 new tests added to tests/unittests/models/test_anthropic_llm.py. Full suite: 71 passed.

tests/unittests/models/test_anthropic_llm.py::test_non_streaming_forwards_generation_params PASSED
tests/unittests/models/test_anthropic_llm.py::test_non_streaming_omits_unset_generation_params PASSED
tests/unittests/models/test_anthropic_llm.py::test_streaming_forwards_generation_params PASSED
tests/unittests/models/test_anthropic_llm.py::test_streaming_omits_unset_generation_params PASSED
tests/unittests/models/test_anthropic_llm.py::test_thinking_level_maps_to_effort[MINIMAL-low] PASSED
tests/unittests/models/test_anthropic_llm.py::test_thinking_level_maps_to_effort[LOW-medium] PASSED
tests/unittests/models/test_anthropic_llm.py::test_thinking_level_maps_to_effort[MEDIUM-high] PASSED
tests/unittests/models/test_anthropic_llm.py::test_thinking_level_maps_to_effort[HIGH-xhigh] PASSED
tests/unittests/models/test_anthropic_llm.py::test_anthropic_config_effort_max_non_streaming PASSED
tests/unittests/models/test_anthropic_llm.py::test_anthropic_config_effort_max_streaming PASSED
tests/unittests/models/test_anthropic_llm.py::test_no_effort_when_thinking_level_unset PASSED
tests/unittests/models/test_anthropic_llm.py::test_effort_takes_priority_over_sampling_params PASSED

@adk-bot adk-bot added the models [Component] This issue is related to model support label Apr 28, 2026
@rohityan rohityan self-assigned this Apr 30, 2026
@sebastienc
sebastienc force-pushed the feat/anthropic-generation-config branch from 3770e28 to ac78062 Compare May 5, 2026 13:45
@sebastienc

Copy link
Copy Markdown
Contributor Author

Fixed the pyink issue and rebased.

@rohityan
rohityan requested a review from xuanyang15 May 12, 2026 03:26
@rohityan

Copy link
Copy Markdown
Collaborator

Hi @xuanyang15 , can you please review this.

@xuanyang15

Copy link
Copy Markdown
Collaborator

@GWeale Could you please help take a look?

@rohityan rohityan closed this May 18, 2026
@sebastienc

Copy link
Copy Markdown
Contributor Author

Hi @rohityan , has someone else added these changes? Why has the PR been closed? Thanks.

@i-yliu i-yliu self-assigned this Jun 29, 2026
copybara-service Bot pushed a commit that referenced this pull request Jun 29, 2026
… propagation, and conflict mitigation

Key changes:
 - Reasoning Effort Support for anthropic model: Introduces AnthropicGenerateContentConfig to allow users to set the effort field directly ("low", "medium", "high", "xhigh", "max") for Anthropic model effort level.
- the standard thinking_config.thinking_level was not passed to anthropic effort due to mapping inconsistencies between the 4-level standard enum and Anthropic's 5 effort levels. It will raise a warning.
- Conflict Mitigation: Automatically excludes sampling parameters (temperature, top_p, top_k) when thinking or reasoning effort is enabled, avoiding Anthropic API 400 errors. Emits a warning log if these parameters were provided but ignored.
- Unified Parameter Propagation: Propagates core GenerateContentConfig parameters (temperature, top_p, top_k, stop_sequences, and max_output_tokens [mapped to max_tokens]) to the Anthropic client using the new _build_anthropic_kwargs helper.
- Type Annotation Updates: Adds ThinkingConfigAdaptiveParam to the thinking parameter type annotations across streaming and non-streaming interfaces.

Note for callers:
- This is a behavior change for existing Anthropic
  model callers. Previously, temperature, top_p, top_k, stop_sequences,
  and max_output_tokens were ignored (dropped) for Claude. They are now
  correctly propagated to the Anthropic client, and max_output_tokens
  will now correctly override the default max_tokens (8192).

Fixes: #5393
Closes: #5513

Co-authored-by: Yi Liu <yiliuly@google.com>
COPYBARA_INTEGRATE_REVIEW=#5513 from sebastienc:feat/anthropic-generation-config 0276484
PiperOrigin-RevId: 940074181
FrigaZzz pushed a commit to FrigaZzz/adk-python that referenced this pull request Aug 11, 2026
… propagation, and conflict mitigation

Key changes:
 - Reasoning Effort Support for anthropic model: Introduces AnthropicGenerateContentConfig to allow users to set the effort field directly ("low", "medium", "high", "xhigh", "max") for Anthropic model effort level.
- the standard thinking_config.thinking_level was not passed to anthropic effort due to mapping inconsistencies between the 4-level standard enum and Anthropic's 5 effort levels. It will raise a warning.
- Conflict Mitigation: Automatically excludes sampling parameters (temperature, top_p, top_k) when thinking or reasoning effort is enabled, avoiding Anthropic API 400 errors. Emits a warning log if these parameters were provided but ignored.
- Unified Parameter Propagation: Propagates core GenerateContentConfig parameters (temperature, top_p, top_k, stop_sequences, and max_output_tokens [mapped to max_tokens]) to the Anthropic client using the new _build_anthropic_kwargs helper.
- Type Annotation Updates: Adds ThinkingConfigAdaptiveParam to the thinking parameter type annotations across streaming and non-streaming interfaces.

Note for callers:
- This is a behavior change for existing Anthropic
  model callers. Previously, temperature, top_p, top_k, stop_sequences,
  and max_output_tokens were ignored (dropped) for Claude. They are now
  correctly propagated to the Anthropic client, and max_output_tokens
  will now correctly override the default max_tokens (8192).

Fixes: google#5393
Closes: google#5513

Co-authored-by: Yi Liu <yiliuly@google.com>
COPYBARA_INTEGRATE_REVIEW=google#5513 from sebastienc:feat/anthropic-generation-config 0276484
PiperOrigin-RevId: 940074181
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

models [Component] This issue is related to model support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AnthropicLlm silently ignores generation config parameters

6 participants