Skip to content

Session is prematurely closed when using documented sync method #370

@jeff-got

Description

@jeff-got

Prerequisites

  • I've searched the current open issues
  • I've updated to the latest version of Toolbox
  • I've updated to the latest version of the SDK

Toolbox version

us-central1-docker.pkg.dev/database-toolbox/toolbox/toolbox:latest

Environment

  1. OS type and version: Linux penguin 6.6.76-08174-g2f3b34fb3650
  2. How are you running Toolbox:
  • As a container (e.g. us-central1-docker.pkg.dev/database-toolbox/toolbox/toolbox:latest)
  1. Python version :Python 3.11.2
  2. pip version :pip 23.0.1

Client

  1. Client: google-adk~=1.14.1.
  2. Version:
  • toolbox-core version 0.5.0
  • google-adk~=1.14.1

Expected Behavior

That when asking an agent multiple questions that the session remains

Current Behavior

The first question an answer is provided however when asking a second question I get an error message that the session is closed

Steps to reproduce?

  1. Follow ADK Quickstart
  2. Update agent.py to the following
from google.adk.agents import Agent

from toolbox_core import ToolboxSyncClient, auth_methods
from datetime import datetime
from .prompt import agent_instruction

URL = "https://************.app"

auth_token_provider = auth_methods.get_google_id_token(URL, 10)

with ToolboxSyncClient(
    URL,
    client_headers={"Authorization": auth_token_provider},
) as toolbox:

    toolbox_tools = toolbox.load_toolset("project_toolset")

# ----- Example of a Function tool -----
def get_current_date() -> dict:
    """
    Get the current date in the format YYYY-MM-DD
    """
    return {"current_date": datetime.now().strftime("%Y-%m-%d")}

root_agent = Agent(
    model="gemini-2.0-flash-001",
    name="test_agent",
    instruction=agent_instruction,
    tools=[*toolbox_tools,get_current_date]
)
  1. Run adk web
  2. Ask to list tools
  3. Ask a question that will use one of the tools provided by MCPToolbox and you get the following error

INFO: 127.0.0.1:60816 - "POST /run_sse HTTP/1.1" 200 OK
INFO:/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/cli/utils/envs.py:Loaded .env file for test_agent at /home/jeff/Agents/project-manager-assist/.env
INFO:google_adk.google.adk.models.google_llm:Sending out request, model: gemini-2.0-flash-001, backend: GoogleLLMVariant.VERTEX_AI, stream: False
INFO:google_genai.models:AFC is enabled with max remote calls: 10.
INFO:google_adk.google.adk.models.google_llm:Response received from the model.
WARNING:google_genai.types:Warning: there are non-text parts in the response: ['function_call'], returning concatenated text result from text parts. Check the full candidates.content.parts accessor to get the full model response.
ERROR:google_adk.google.adk.cli.adk_web_server:Error in event_generator: Session is closed
Traceback (most recent call last):
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/cli/adk_web_server.py", line 1066, in event_generator
async for event in agen:
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/runners.py", line 314, in run_async
async for event in agen:
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/runners.py", line 310, in _run_with_trace
async for event in agen:
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/runners.py", line 354, in _exec_with_plugin
async for event in agen:
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/runners.py", line 304, in execute
async for event in agen:
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/agents/base_agent.py", line 248, in run_async
async for event in agen:
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/agents/base_agent.py", line 238, in _run_with_trace
async for event in agen:
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/agents/llm_agent.py", line 288, in _run_async_impl
async for event in agen:
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 401, in run_async
async for event in agen:
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 447, in _run_one_step_async
async for event in agen:
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 539, in _postprocess_async
async for event in agen:
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 632, in _postprocess_handle_function_calls_async
if function_response_event := await functions.handle_function_calls_async(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/flows/llm_flows/functions.py", line 198, in handle_function_calls_async
return await handle_function_call_list_async(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/flows/llm_flows/functions.py", line 246, in handle_function_call_list_async
function_response_events = await asyncio.gather(*tasks)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/flows/llm_flows/functions.py", line 333, in _execute_single_function_call_async
raise tool_error
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/flows/llm_flows/functions.py", line 318, in _execute_single_function_call_async
function_response = await __call_tool_async(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/flows/llm_flows/functions.py", line 690, in __call_tool_async
return await tool.run_async(args=args, tool_context=tool_context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/tools/function_tool.py", line 157, in run_async
return await self._invoke_callable(self.func, args_to_call)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/google/adk/tools/function_tool.py", line 174, in _invoke_callable
return target(**args_to_call)
^^^^^^^^^^^^^^^^^^^^^^
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/toolbox_core/sync_tool.py", line 145, in call
return asyncio.run_coroutine_threadsafe(coro, self.__loop).result()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/concurrent/futures/_base.py", line 456, in result
return self.__get_result()
^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
raise self._exception
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/toolbox_core/tool.py", line 307, in call
async with self.__session.post(
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/aiohttp/client.py", line 1482, in aenter
self._resp: _RetType = await self._coro
^^^^^^^^^^^^^^^^
File "/home/jeff/Agents/project-manager-assist/venv/lib/python3.11/site-packages/aiohttp/client.py", line 527, in _request
raise RuntimeError("Session is closed")
RuntimeError: Session is closed

Additional Details

When using it in the following manner the session remains open

toolbox = ToolboxSyncClient(
    URL,
    client_headers={"Authorization": auth_token_provider},
)

toolbox_tools = toolbox.load_toolset("project_toolset")

Metadata

Metadata

Labels

priority: p1Important issue which blocks shipping the next release. Will be fixed prior to next release.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions