Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
0883d5f
feat: allow thinking_config in generate_content_config (#4108)
Akshat8510 Jan 11, 2026
2d485cb
fix: address review comments regarding imports and logging
Akshat8510 Jan 11, 2026
25a9186
style: improve readability of precedence check in LlmAgent
Akshat8510 Jan 11, 2026
c5aa50e
docs: update LlmAgent docstring and use explicit None checks
Akshat8510 Jan 11, 2026
abce588
docs: update doc
Akshat8510 Jan 11, 2026
3a492da
docs: update doc
Akshat8510 Jan 11, 2026
d8dd6c9
test: update unit tests to allow thinking_config and verify precedenc…
Akshat8510 Jan 11, 2026
bec52bb
test: remove obsolete failing test and add new verification tests
Akshat8510 Jan 11, 2026
a631439
test: cleanup duplicate test definitions and fix imports
Akshat8510 Jan 11, 2026
412447a
refactor: simplify precedence check and remove duplicate tests
Akshat8510 Jan 11, 2026
8438ed6
test: cleanup unit tests and fix logging test placement
Akshat8510 Jan 11, 2026
4b23903
test:clean up
Akshat8510 Jan 11, 2026
072f8ad
style: move test imports to top level per PEP 8
Akshat8510 Jan 11, 2026
74600d8
refactor: simplify apply_thinking_config and cleanup test imports
Akshat8510 Jan 11, 2026
f137045
fix: address maintainer feedback on logic bug and redundant comments
Akshat8510 Jan 13, 2026
56526d9
Merge branch 'main' into feat/allow-thinking-config-4108
Akshat8510 Jan 13, 2026
978d7d1
fix: resolve logic bug in planner and refactor for consistency
Akshat8510 Jan 13, 2026
01462c4
Merge branch 'main' into feat/allow-thinking-config-4108
wuliang229 Jan 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/google/adk/agents/llm_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class LlmAgent(BaseAgent):
"""The additional content generation configurations.

NOTE: not all fields are usable, e.g. tools must be configured via `tools`,
thinking_config must be configured via `planner` in LlmAgent.
thinking_config can be configured here or via the `planner`. If both are set, the planner's configuration takes precedence.

For example: use this config to adjust model temperature, configure safety
settings, etc.
Expand Down Expand Up @@ -849,8 +849,6 @@ def validate_generate_content_config(
) -> types.GenerateContentConfig:
if not generate_content_config:
return types.GenerateContentConfig()
if generate_content_config.thinking_config:
raise ValueError('Thinking config should be set via LlmAgent.planner.')
if generate_content_config.tools:
raise ValueError('All tools must be set via LlmAgent.tools.')
if generate_content_config.system_instruction:
Expand All @@ -862,6 +860,28 @@ def validate_generate_content_config(
'Response schema must be set via LlmAgent.output_schema.'
)
return generate_content_config

def model_post_init(self, __context: Any) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

"""Provides a warning if multiple thinking configurations are found."""
super().model_post_init(__context)

# Check if thinking_config is set in both the model config and the planner.
# Using getattr for consistency in checking optional attributes.
has_manual_thinking_config = (
Copy link
Collaborator

Choose a reason for hiding this comment

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

"manual": This is not a good naming in my opinion. But see the other comment.

getattr(self.generate_content_config, 'thinking_config', None) is not None
)
planner_has_thinking_config = (
getattr(self.planner, 'thinking_config', None) is not None
)

if has_manual_thinking_config and planner_has_thinking_config:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just write if getattr(self.generate_content_config, 'thinking_config', None) and getattr(self.planner, 'thinking_config', None)

warnings.warn(
'Both `thinking_config` in `generate_content_config` and an '
'agent `planner` with thinking enabled are provided. The '
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would prefer phrasing it as "planner with thinking_config".

'planner\'s configuration will take precedence.',
UserWarning,
stacklevel=2,
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be 3 if you want to point to where user creates the LlmAgent.

)

@classmethod
@experimental
Expand Down
6 changes: 6 additions & 0 deletions src/google/adk/planners/built_in_planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from typing import List
from typing import Optional
import logging

from google.genai import types
from typing_extensions import override
Expand Down Expand Up @@ -57,6 +58,11 @@ def apply_thinking_config(self, llm_request: LlmRequest) -> None:
"""
if self.thinking_config:
llm_request.config = llm_request.config or types.GenerateContentConfig()
if llm_request.config.thinking_config:
logging.info(
Copy link
Collaborator

Choose a reason for hiding this comment

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

You should use hierarchical logger in ADK codebase instead of the root logger. https://google.github.io/adk-docs/observability/logging/#logging-philosophy

'Overwriting `thinking_config` from `generate_content_config` with '
'the one provided by the `BuiltInPlanner`.'
)
llm_request.config.thinking_config = self.thinking_config

@override
Expand Down
54 changes: 48 additions & 6 deletions tests/unittests/agents/test_llm_agent_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@

"""Unit tests for canonical_xxx fields in LlmAgent."""

import logging
from typing import Any
from typing import Optional
from unittest import mock


from google.adk.planners.built_in_planner import BuiltInPlanner
from google.adk.agents.callback_context import CallbackContext
from google.adk.agents.invocation_context import InvocationContext
from google.adk.agents.llm_agent import LlmAgent
Expand Down Expand Up @@ -234,17 +237,35 @@ def _before_model_callback(
assert agent.before_model_callback is not None


def test_validate_generate_content_config_thinking_config_throw():
with pytest.raises(ValueError):
_ = LlmAgent(
def test_validate_generate_content_config_thinking_config_allow():
"""Tests that thinking_config is now allowed directly in the agent init."""
agent = LlmAgent(
name='test_agent',
generate_content_config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(include_thoughts=True)
),
)
assert agent.generate_content_config.thinking_config.include_thoughts is True


def test_thinking_config_precedence_warning():
"""Tests that a UserWarning is issued when both manual config and planner exist."""

config = types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(include_thoughts=True)
)
planner = BuiltInPlanner(thinking_config=types.ThinkingConfig(include_thoughts=True))

with pytest.warns(UserWarning, match="planner's configuration will take precedence"):
LlmAgent(
name='test_agent',
generate_content_config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig()
),
generate_content_config=config,
planner=planner
)


def test_validate_generate_content_config_tools_throw():
"""Tests that tools cannot be set directly in config."""
with pytest.raises(ValueError):
_ = LlmAgent(
name='test_agent',
Expand All @@ -255,6 +276,7 @@ def test_validate_generate_content_config_tools_throw():


def test_validate_generate_content_config_system_instruction_throw():
"""Tests that system instructions cannot be set directly in config."""
with pytest.raises(ValueError):
_ = LlmAgent(
name='test_agent',
Expand All @@ -265,6 +287,7 @@ def test_validate_generate_content_config_system_instruction_throw():


def test_validate_generate_content_config_response_schema_throw():
"""Tests that response schema cannot be set directly in config."""
class Schema(BaseModel):
pass

Expand Down Expand Up @@ -471,3 +494,22 @@ def test_agent_with_litellm_string_model(model_name):
agent = LlmAgent(name='test_agent', model=model_name)
assert isinstance(agent.canonical_model, LiteLlm)
assert agent.canonical_model.model == model_name


def test_builtin_planner_overwrite_logging(caplog):
"""Tests that the planner logs an INFO message when overwriting a config."""

planner = BuiltInPlanner(thinking_config=types.ThinkingConfig(include_thoughts=True))

# Create a request that already has a thinking_config
req = LlmRequest(
contents=[],
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(include_thoughts=True)
)
)

with caplog.at_level(logging.INFO):
planner.apply_thinking_config(req)

assert "Overwriting `thinking_config` from `generate_content_config`" in caplog.text
Loading