diff --git a/examples/python/snippets/agents/llm-agent/capital_agent.py b/examples/python/snippets/agents/llm-agent/capital_agent.py index 9a9b9ec6..262b4174 100644 --- a/examples/python/snippets/agents/llm-agent/capital_agent.py +++ b/examples/python/snippets/agents/llm-agent/capital_agent.py @@ -1,5 +1,6 @@ # --- Full example code demonstrating LlmAgent with Tools vs. Output Schema --- import json # Needed for pretty printing dicts +import asyncio from google.adk.agents import LlmAgent from google.adk.runners import Runner @@ -79,10 +80,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 +113,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, + current_session = await 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 +132,11 @@ 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 + print("--- Creating Sessions ---") + 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"}') @@ -144,4 +146,4 @@ async def main(): await call_agent_and_print(structured_runner, structured_info_agent_schema, SESSION_ID_SCHEMA_AGENT, '{"country": "Japan"}') if __name__ == "__main__": - await main() + asyncio.run(main())