Skip to content

Session often not accessible to subsequent agents, timing issue? #1119

Closed

Description

@hammertoe

Describe the bug
When you have two agents in sequence, in this case in a LoopAgent, the second agent is often unable to see items stored in the state of the previous agent.

To Reproduce
This has been hard to replicate as a timing issue, but seems to be OK on smaller prompts, but more noticable on a larger prompt.

e.g

graph_builder1 = LlmAgent(
    name="GraphBuilder1",
    model=LLM_MODEL1,
    generate_content_config=types.GenerateContentConfig(
        temperature=0.2
    ),
    instruction=GENERATE_KG_PROMPT.format(knowledge_graph_key="knowledge_graph1"),
    tools=[store_knowledge_graph],
    output_key='knowledge_graph1',
)

graph_reviewer1 = LlmAgent(
    name="GraphReviewer1",
    model=LLM_MODEL2,
    instruction=REVIEW_PROMPT.format(knowledge_graph_key="knowledge_graph1"),
    tools=[validate_turtle, load_knowledge_graph], # <--- can't find key knowledge_graph1
)

kg_refinement_loop1 = LoopAgent(
    name="KGRefinementLoop1",
    max_iterations=5,
    sub_agents=[
        graph_builder1,
        graph_reviewer1,
        StopIfComplete(name="StopChecker1", status_key="graph_status1"),
    ],
)

from: https://github.com/hammertoe/GraphCycle/blob/main/graphcycle/agent.py#L220C1-L235C2

**Expected

Image

**
A subsequent agent should be able to reliably access state keys from the previous agent

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: Darwin Matts-MacBook-Pro-2.local 23.6.0 Darwin Kernel Version 23.6.0: Thu Mar 6 22:01:15 PST 2025; root:xnu-10063.141.1.704.6~1/RELEASE_ARM64_T6000 arm64
  • Python version(python -V): Python 3.13.3
  • ADK version(pip show google-adk): 1.1.1

Additional context
Like I said, a timing issue... so intermittent. Seems like the second agent is starting before the first has finished saving state or something.

Activity

  1. added
    services[Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc
    on Jun 5, 2025
  2. self-assigned this
    on Jun 17, 2025
  3. added theissue type on Jul 2, 2025
  4. xuanyang15 commented on Jul 7, 2025

    @xuanyang15
    Collaborator

    Hi @hammertoe,

    Thanks for reporting this issue! I took a look at your code at https://github.com/hammertoe/GraphCycle/blob/main/graphcycle/agent.py and noticed something you might want to consider changing:

    1. It seems the knowledge_graph created by graph_builder1 will be stored in the state though both the store_knowledge_graph tool and the output_key?
    2. The GENERATE_KG_PROMPT doesn't mention the knowledge_graph_key at all
    GENERATE_KG_PROMPT = """
        You are an RDF engineer.
        Read state['raw_text'] and create **or refine** a Turtle knowledge
        graph covering every entity and relationship mentioned.
        If state['missing_items'] exists, be sure to add them.
        Output ONLY the Turtle RDF graph, nothing else.    
    """
    
    graph_builder1 = LlmAgent(
        name="GraphBuilder1",
        model=LLM_MODEL1,
        generate_content_config=types.GenerateContentConfig(
            temperature=0.5
        ),
        instruction=GENERATE_KG_PROMPT.format(knowledge_graph_key="knowledge_graph1"), <--- `knowledge_graph_key` is not mentioned in GENERATE_KG_PROMPT
        tools=[store_knowledge_graph],    <--- Is this used to store the graph?
        output_key="knowledge_graph1",  <--- Is this also used to store the graph?
    )
    

    My recommendation is to update the code to use only the tool to store the graph:

    1. Remove the output_key="knowledge_graph1"
    2. Add the instruction about using "knowledge_graph1" as the key to store the graph

    For example:

    GENERATE_KG_PROMPT = """
    You are an RDF engineer.
    Read state['raw_text'] and create **or refine** a Turtle knowledge
    graph covering every entity and relationship mentioned.
    If state['missing_items'] exists, be sure to add them.
    After generating the graph, you MUST use the `store_knowledge_graph` tool to save the result. Set the `graph_id` parameter to `{knowledge_graph_key}`.
    """
    
    graph_builder1 = LlmAgent(
        name="GraphBuilder1",
        model=LLM_MODEL1,
        generate_content_config=types.GenerateContentConfig(
            temperature=0.2
        ),
        instruction=GENERATE_KG_PROMPT.format(knowledge_graph_key="knowledge_graph1"),
        tools=[store_knowledge_graph],
        # You can remove the output_key here if the tool is responsible for saving state
    )
    

    Could you please try the updated code and see if it fixed the issue?

  5. hammertoe commented on Jul 7, 2025

    @hammertoe
    Author

    I shouldn't need the tool. According to the docs the output key should be sufficient. Either way the next stage is not able to reliably load the state and fails.

  6. xuanyang15 commented on Jul 7, 2025

    @xuanyang15
    Collaborator

    Thanks for the quick response! Do you mind trying the tool instead of the output key? I want to confirm if the issue is output key only or both.

  7. hammertoe commented on Jul 7, 2025

    @hammertoe
    Author

    I have tried both. First I just used the output key, then I added the tool, then I put in both. But regardless it is the next agent in the pipeline that cannot load from the state reliably. Sometimes it works, sometimes it doesn't.

  8. xuanyang15 commented on Jul 8, 2025

    @xuanyang15
    Collaborator

    Thanks a lot for the information! I will try to reproduce this issue and debug from there.

  9. seanzhou1023 commented on Jul 8, 2025

    @seanzhou1023
    Collaborator

    when you same somtime it works and sometime it doesn't. do you have the logs for both case ? Mainly I want to see whether the model generate the correct function call for loading the state.

    One thing I don't understand is that given your output key is fixed, why did you make it an argument of the load_knowledge_graph tool ? could you try hard code the key in the load tool ? ( that can verify whether it's related to model responding a correct function call )

  10. xuanyang15 commented on Jul 9, 2025

    @xuanyang15
    Collaborator

    Hi @hammertoe, I tried running your code at https://github.com/hammertoe/GraphCycle/blob/main/graphcycle/agent.py, and didn't hit the error. I ran about 20+ times.

    I also noticed the following potential mistakes in the code for which you might want to consider change:

    1. You have "store the content in state['raw_text']" in LOADER_PROMPT, which might not work. I doubt the LLM can store raw_text following the instruction.
    2. Although you specified output_key="raw_text" in the file_loader, its instruction is actually "Report the length of the text and the file path / url in the output.", which means what you end up storing in the state['raw_text'] is actually just the length of the text and file url, not the file content.
    3. (probably unrelated to the error, but it is something to fix the behavior) graph_reviewer1 and graph_reviewer2 do not have output_key set, which means their conclusion ("pass" or "fail") are never stored in the state.

    Here is a diff of change I made to your code to address the above issues:

    diff --git a/graphcycle/agent.py b/graphcycle/agent.py
    index 750bded..0ca2f59 100644
    --- a/graphcycle/agent.py
    +++ b/graphcycle/agent.py
    @@ -46,7 +46,6 @@ LOADER_PROMPT = """
         Use the following tools to load in text:
             - read_file_content tool to read the content of a file.
             - download_youtube_transcript tool to fetch the transcript of a YouTube video.
    -    store the content in state['raw_text'].
         Report the length of the text and the file path / url in the output.
     """
     
    @@ -92,7 +91,7 @@ MERGE_PROMPT = """
         If you cannot merge them, output 'fail' with an explanation.
         """
     
    -def read_file_content(file_path: str) -> dict:
    +def read_file_content(file_path: str, tool_context: ToolContext) -> dict:
         """
         Read the content of a file and return it as a string.
         If the file does not exist, return an empty string.
    @@ -103,6 +102,7 @@ def read_file_content(file_path: str) -> dict:
         
         with open(file_path, 'r', encoding='utf-8') as file:
             content = file.read()
    +        tool_context.state["raw_text"] = content
         
         return {"status": "success", 
                 "file_path": file_path,
    @@ -178,7 +178,7 @@ def validate_turtle(turtle_string: str) -> dict:
             return {"status": "error", 
                     "error_message": str(e)}
     
    -def store_knowledge_graph(knowledge_graph: str, tool_context: "ToolContext", graph_id: str, **_) -> dict:
    +def store_knowledge_graph(knowledge_graph: str, tool_context: ToolContext, graph_id: str, **_) -> dict:
         """
         Store the provided knowledge graph in the tool context state by its ID.
     
    @@ -213,7 +213,7 @@ file_loader = LlmAgent(
         name="TextLoader",
         model=LOADER_MODEL,
         instruction=LOADER_PROMPT,
    -    output_key="raw_text",
    +    # output_key="raw_text",
         tools=[read_file_content, download_youtube_transcript],
     )
     
    @@ -224,7 +224,7 @@ graph_builder1 = LlmAgent(
             temperature=0.5
         ),
         instruction=GENERATE_KG_PROMPT.format(knowledge_graph_key="knowledge_graph1"),
    -    tools=[store_knowledge_graph],
    +    # tools=[store_knowledge_graph],
         output_key="knowledge_graph1",
     )
     
    @@ -233,6 +233,7 @@ graph_reviewer1 = LlmAgent(
         model=LLM_MODEL2,
         instruction=REVIEW_PROMPT.format(knowledge_graph_key="knowledge_graph1"),
         tools=[validate_turtle, load_knowledge_graph],
    +    output_key="graph_status1"
     )
     
     graph_builder2 = LlmAgent(
    @@ -242,7 +243,7 @@ graph_builder2 = LlmAgent(
             temperature=0.5
         ),
         instruction=GENERATE_KG_PROMPT.format(knowledge_graph_key="knowledge_graph2"),
    -    tools=[store_knowledge_graph],
    +    # tools=[store_knowledge_graph],
         output_key="knowledge_graph2",
     )
     
    @@ -251,6 +252,7 @@ graph_reviewer2 = LlmAgent(
         model=LLM_MODEL1,
         instruction=REVIEW_PROMPT.format(knowledge_graph_key="knowledge_graph2"),
         tools=[validate_turtle, load_knowledge_graph],
    +    output_key="graph_status2"
     )
     
     merger_agent = LlmAgent(

    Could you please try these fixes? If you still hit the error, could you please share the logs so that we can locate the root cause of the problem as mentioned in the above comment #1119 (comment)?

  11. DeanChensj commented on Jul 16, 2025

    @DeanChensj
    Collaborator

    Hi @hammertoe , the fix by @iwknow was merged in, could you try again?

  12. added
    request clarification[Status] The maintainer need clarification or more information from the author
    on Jul 18, 2025
  13. jwiejulie commented on Aug 14, 2025

    @jwiejulie
    Collaborator

    Hi @hammertoe and thanks to everyone who participated in this thread.

    We're currently doing some housekeeping on our issue tracker. Given the rapid pace of development and the number of updates since this was last active, there's a good chance this issue has been resolved in a newer version. To help us keep our backlog focused on current issues, we are closing this as stale.

    If you're still facing this problem, could you please update to the very latest version of the library and confirm the issue still exists? If it does, please comment here, and we will be happy to reopen this issue and investigate further.

    Thanks for your contribution and understanding!

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

Metadata

Metadata

Assignees

Labels

request clarification[Status] The maintainer need clarification or more information from the authorservices[Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions