From 778a5ba297260dd3c59029b7ca542e987e9e0c56 Mon Sep 17 00:00:00 2001 From: Rohit <130643902+Rohit10jr@users.noreply.github.com> Date: Sun, 1 Jun 2025 09:11:22 +0530 Subject: [PATCH 1/4] Add internal documentation link to "Output Key" section of LLM Agent docs Updated the Markdown link to point directly to the relevant section (output_key) within the LLM Agent documentation, instead of linking to the top of the page. --- docs/agents/workflow-agents/sequential-agents.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/agents/workflow-agents/sequential-agents.md b/docs/agents/workflow-agents/sequential-agents.md index 96b6aec25..f05945b5f 100644 --- a/docs/agents/workflow-agents/sequential-agents.md +++ b/docs/agents/workflow-agents/sequential-agents.md @@ -35,7 +35,7 @@ A `SequentialAgent` is perfect for this: SequentialAgent(sub_agents=[CodeWriterAgent, CodeReviewerAgent, CodeRefactorerAgent]) ``` -This ensures the code is written, *then* reviewed, and *finally* refactored, in a strict, dependable order. **The output from each sub-agent is passed to the next by storing them in state via [Output Key](../llm-agents.md)**. +This ensures the code is written, *then* reviewed, and *finally* refactored, in a strict, dependable order. **The output from each sub-agent is passed to the next by storing them in state via [Output Key](../llm-agents.md#structuring-data-input_schema-output_schema-output_key)**. ???+ "Code" From 88e23b86c591d1c7988ac2a20f1c928a761fea8f Mon Sep 17 00:00:00 2001 From: Rohit <130643902+Rohit10jr@users.noreply.github.com> Date: Tue, 17 Jun 2025 22:43:27 +0530 Subject: [PATCH 2/4] Fix: Use correct variable and imports Replaced incorrect `APP_NAME` and `USER_ID` with `app_name` and `user_id`, and fixed the wrong import of `CallbackContext` from `google.adk.agents` in the documentation. --- docs/sessions/state.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/sessions/state.md b/docs/sessions/state.md index 2fda2bafa..7ced2a771 100644 --- a/docs/sessions/state.md +++ b/docs/sessions/state.md @@ -115,7 +115,7 @@ This is the simplest method for saving an agent's final text response directly i print(f"Agent responded.") # Response text is also in event.content # --- Check Updated State --- - updated_session = await session_service.get_session(app_name=APP_NAME, user_id=USER_ID, session_id=session_id) + updated_session = await session_service.get_session(app_name=app_name, user_id=user_id, session_id=session_id) print(f"State after agent run: {updated_session.state}") # Expected output might include: {'last_greeting': 'Hello there! How can I help you today?'} ``` @@ -207,7 +207,7 @@ For more comprehensive details on context objects, refer to the [Context documen ```python # In an agent callback or tool function - from google.adk.agents import CallbackContext # or ToolContext + from google.adk.agents.callback_context import CallbackContext # or ToolContext def my_callback_or_tool_function(context: CallbackContext, # Or ToolContext # ... other parameters ... From 81f6ada7fb5ea85ce2b8a82183666eb1dbe72352 Mon Sep 17 00:00:00 2001 From: Rohit <130643902+Rohit10jr@users.noreply.github.com> Date: Fri, 20 Jun 2025 22:31:39 +0530 Subject: [PATCH 3/4] fix: add await to session calls and async main entrypoint Added await to create_session and get_session calls. Moved session logic into async function. App now supports execution in both Jupyter notebooks and standard Python scripts. --- .../agents/llm-agent/capital_agent.py | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/examples/python/snippets/agents/llm-agent/capital_agent.py b/examples/python/snippets/agents/llm-agent/capital_agent.py index 9a9b9ec6f..184c264fb 100644 --- a/examples/python/snippets/agents/llm-agent/capital_agent.py +++ b/examples/python/snippets/agents/llm-agent/capital_agent.py @@ -79,10 +79,6 @@ def get_capital_city(country: str) -> str: # --- 5. Set up Session Management and Runners --- session_service = InMemorySessionService() -# Create separate sessions for clarity, though not strictly necessary if context is managed -session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID_TOOL_AGENT) -session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID_SCHEMA_AGENT) - # Create a runner for EACH agent capital_runner = Runner( agent=capital_agent_with_tool, @@ -116,7 +112,7 @@ async def call_agent_and_print( print(f"<<< Agent '{agent_instance.name}' Response: {final_response_content}") - current_session = session_service.get_session(app_name=APP_NAME, + await current_session = session_service.get_session(app_name=APP_NAME, user_id=USER_ID, session_id=session_id) stored_output = current_session.state.get(agent_instance.output_key) @@ -135,6 +131,10 @@ async def call_agent_and_print( # --- 7. Run Interactions --- async def main(): + # Create separate sessions for clarity, though not strictly necessary if context is managed + await session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID_TOOL_AGENT) + await session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID_SCHEMA_AGENT) + print("--- Testing Agent with Tool ---") await call_agent_and_print(capital_runner, capital_agent_with_tool, SESSION_ID_TOOL_AGENT, '{"country": "France"}') await call_agent_and_print(capital_runner, capital_agent_with_tool, SESSION_ID_TOOL_AGENT, '{"country": "Canada"}') @@ -143,5 +143,16 @@ async def main(): await call_agent_and_print(structured_runner, structured_info_agent_schema, SESSION_ID_SCHEMA_AGENT, '{"country": "France"}') await call_agent_and_print(structured_runner, structured_info_agent_schema, SESSION_ID_SCHEMA_AGENT, '{"country": "Japan"}') +# Run directly if using Jupyter notebooks or IPython shell that support top-level await if __name__ == "__main__": await main() + +# --- OR --- + +# Uncomment the following lines if running as a standard Python script (.py file): +# import asyncio +# if __name__ == "__main__": +# try: +# asyncio.run(main()) +# except Exception as e: +# print(f"An error occurred while running the agent demo: {e}") From 9950f95bbff36af19565725528e415cefd40cec7 Mon Sep 17 00:00:00 2001 From: Rohit <130643902+Rohit10jr@users.noreply.github.com> Date: Tue, 8 Jul 2025 22:16:28 +0530 Subject: [PATCH 4/4] add License Header in capital_agent.py This file was missing the license header and was giving a warning on the pull request --- .../snippets/agents/llm-agent/capital_agent.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/examples/python/snippets/agents/llm-agent/capital_agent.py b/examples/python/snippets/agents/llm-agent/capital_agent.py index 184c264fb..d8462bac2 100644 --- a/examples/python/snippets/agents/llm-agent/capital_agent.py +++ b/examples/python/snippets/agents/llm-agent/capital_agent.py @@ -1,3 +1,17 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + # --- Full example code demonstrating LlmAgent with Tools vs. Output Schema --- import json # Needed for pretty printing dicts