Skip to content

Sub-agent not returning control to root or sub-agent not calling tools to save state variables #620

@Helleeni

Description

@Helleeni

Bug description
Can't make sub-agents to both use tools and return control to root agent. No combination of sub-agent as a tool nor sub-agent as an agent seems to work (nor any combination with "disallow_transfer_to_parent" parameter).

To Reproduce
Steps to reproduce the behavior:

  1. Run the code with prompts: 'Hi', 'New document', 'Test title', 'Test ID'
  2. Depending if you define sub-agents as tools or use them as sub-agents you get the behaviour explained below.

Expected and observed behavior
Expected result: Sub-agent can use tools to save state variables and return control to root agent.
Observed result: Sub-agent as a tool returns the control to root agent, but is not calling tools to save state variables (as pictured below with adk web).
Observed result: Sub-agent as a sub-agent does not return the control to root agent, but is calling tools to save state variables.

Code example

from google.adk.tools.agent_tool import AgentTool
from google.adk.tools import ToolContext
from google.adk.tools import FunctionTool

def set_document_id(tool_context: ToolContext, document_id: str) -> dict:
    """Stores the provided document ID into the session state."""
    if not document_id:
        return {"status": "error", "message": "No document ID provided."}
    tool_context.state["document_id"] = document_id
    return {"document_id_status": "Document ID stored successfully."}


def set_document_title(tool_context: ToolContext, document_title: str) -> dict:
    """Stores the provided document title into the session state."""
    if not document_title:
        return {"status": "error", "message": "No document title provided."}
    tool_context.state["document_title"] = document_title
    return {"document_title_status": "Document title stored successfully."}


def use_document_details(document_details: str, tool_context: ToolContext) -> dict:
    """Retrieves the document details from the state and returns them."""
    document_detail_to_use = tool_context.state.get(document_details)
    if document_detail_to_use:
        return {"status": "success", "message": f"Document detail is {document_detail_to_use}"}
    else:
        return {"status": "error", "message": "Could not find document details"}


# --- Tool Definitions ---
set_document_id_tool = FunctionTool(func=set_document_id)
set_document_title_tool = FunctionTool(func=set_document_title)
use_document_details_tool = FunctionTool(func=use_document_details)


# --- Agent Definitions ---
document_title_agent = Agent(
    name="document_title_agent",
    description="Agent responsible for getting the document title from the user and storing it.",
    model="gemini-2.0-flash",
    instruction="""You are a helpful assistant. Your sole task is to get and store the document title.
                1. Ask the user: 'What is the title for this document?'
                2. Once the user provides the title, call the 'set_document_title_tool' tool with the title as the argument to store it.
                3. Then move the control back to the parent agent.
                """,
    tools=[set_document_title_tool],
)

document_id_agent = Agent(
    name="document_id_agent",
    description="Agent responsible for getting the document ID from the user and storing it.",
    model="gemini-2.0-flash",
    instruction="""
                You are a helpful assistant. Your sole task is to get and store the document ID.
                Ask the user: 'What is the ID for this document?'
                Wait for the user to provide the ID and then call the 'set_document_id' tool to store it.
                """,
    tools=[set_document_id_tool],
)

# --- Root Agent ---
root_agent = Agent(  # 'root_agent' needs to be imported in __init__.py
    name="librarian",
    description="You are main agent using sub-agents to create and review documents for a user.",
    model="gemini-2.0-flash", # Use a valid model
    instruction="You are a helpful agent that helps user to fill in all the fields of a document."
                "Ask the user what he wants to do. Tell the user that you can create a new document, or review either title or ID."
                "Don't ask the user to provide the title or ID. Just ask the user what he wants to do and invoke the appropriate sub-agents to get the title and ID."
                "Once the title and ID are stored, inform user that his task has been completed and show the result."
                "If the user wants to review either title or ID, use the 'use_document_details_tool' with the argument 'document_title' or 'document_id' to show the result.",
    tools=[use_document_details_tool, AgentTool(agent=document_title_agent), AgentTool(agent=document_id_agent)],
    #sub_agents=[document_title_agent, document_id_agent]
)

Desktop (please complete the following information):

  • OS: MacOS Sequoia 15.3.2
  • Python version(python -V): 3.13.0
  • ADK version(pip show google-adk): v0.4.0

Edit added a screenshot from adk web how sub-agent as a tool does not call the tool function

Image

Metadata

Metadata

Assignees

Labels

core[Component] This issue is related to the core interface and implementation

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions