Skip to content

optimization: clone_agent_with_params misses an agent shared across levels, drops planner thinking fields, and ignores duplicate names #8418

In review

Description

@AkshayShah03

Describe the bug

clone_agent_with_params (added in d723cb8 and used by GridSearchAgentOptimizer to build each candidate) has three cases where an override doesn't do what the path says. In each one the grid search scores candidates without any error or warning.

1. An agent wrapped by an AgentTool on a different agent is cloned twice.

The cloner already keeps sharing intact when an agent is in sub_agents and in an AgentTool on the same agent (cloned_subs, _agent_tree_cloner.py:426-457). When the AgentTool is on another agent, the tool gets its own separate clone, so a sub_agents.<name>.<field> override reaches only one of the two copies:

from google.adk.agents import LlmAgent
from google.adk.optimization._agent_tree_cloner import clone_agent_with_params
from google.adk.tools.agent_tool import AgentTool

a = LlmAgent(name="A", model="old")
b = LlmAgent(name="B", model="m", tools=[AgentTool(a)])
root = LlmAgent(name="root", model="m", sub_agents=[a, b])

c = clone_agent_with_params(root, {"sub_agents.A.model": "new"})
print(c.sub_agents[0].model)                 # new
print(c.sub_agents[1].tools[0].agent.model)  # old
print(c.sub_agents[0] is c.sub_agents[1].tools[0].agent)  # False (True in the original tree)

The same tree with the AgentTool on root instead of B gives new for both.

2. A dotted thinking override drops the planner's other thinking settings.

When an agent sets thinking_config both in generate_content_config and in a BuiltInPlanner (LlmAgent warns that the planner's config wins), a dotted override is merged onto the generate_content_config copy and the result then replaces the planner's config (_agent_tree_cloner.py:355-358, _sync_builtin_planner). Planner-only fields that were in effect are lost:

root = LlmAgent(
    name="root", model="gemini-2.5-flash",
    generate_content_config=types.GenerateContentConfig(
        thinking_config=types.ThinkingConfig(thinking_budget=64)),
    planner=BuiltInPlanner(thinking_config=types.ThinkingConfig(
        include_thoughts=True, thinking_budget=128)),
)
c = clone_agent_with_params(
    root, {"generate_content_config.thinking_config.thinking_budget": 1024})
print(c.planner.thinking_config)
# include_thoughts=None thinking_budget=1024

So sweeping thinking_budget also turns off include_thoughts. agent_tree_cloner_test.py:328 describes dotted updates as merging with the planner's thinking_config.

3. An ambiguous name changes every match.

Paths are resolved through dicts keyed by agent name, and ADK only warns about duplicate names. With two sub-agents (or two AgentTools) named helper, sub_agents.helper.model changes both instead of raising like an unknown name does:

root = SequentialAgent(name="root", sub_agents=[
    LlmAgent(name="helper", model="m1"), LlmAgent(name="helper", model="m2")])
c = clone_agent_with_params(root, {"sub_agents.helper.model": "X"})
print([s.model for s in c.sub_agents])  # ['X', 'X']

Expected behavior

  1. Every reference to one original agent maps to one clone, so the override applies everywhere that agent is used.
  2. The override starts from the config that is in effect (the planner's), so include_thoughts=True is kept.
  3. An ambiguous name raises ValueError.

Environment

main at 03725e8, Python 3.12.

I'm happy to send a PR with regression tests for all three, if this approach sounds right.

Activity

  1. tushar-2320 commented on Nov 28, 2026

    @tushar-2320

    I would like work on this

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions