Skip to content

Fix long running tool to handle list objects #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/google/adk/tests/unittests/runners/test_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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.

import pytest
from google.adk.runners import Runner
from google.adk.sessions.in_memory_session_service import InMemorySessionService
from google.adk.agents.llm_agent import LlmAgent
from google.adk.models.llm_request import LlmRequest
from google.adk.models.llm_response import LlmResponse
from google.genai import types
from google.adk.tools.long_running_tool import LongRunningFunctionTool
from google.adk.tools.tool_context import ToolContext


@pytest.mark.asyncio
async def test_runner_with_long_running_function_tool():
async def long_running_function(file_path: str) -> list:
return [
{"status": "pending", "message": f"Starting processing for {file_path}..."},
{"status": "pending", "progress": "20%", "estimated_completion_time": "~4 seconds remaining"},
{"status": "pending", "progress": "40%", "estimated_completion_time": "~3 seconds remaining"},
{"status": "pending", "progress": "60%", "estimated_completion_time": "~2 seconds remaining"},
{"status": "pending", "progress": "80%", "estimated_completion_time": "~1 second remaining"},
{"status": "completed", "result": f"Successfully processed file: {file_path}"}
]

tool = LongRunningFunctionTool(func=long_running_function)
agent = LlmAgent(
model="mock",
name='file_processor_agent',
instruction="You are an agent that processes large files.",
tools=[tool]
)

session_service = InMemorySessionService()
session = session_service.create_session(app_name="file_processor", user_id="1234", session_id="session1234")
runner = Runner(agent=agent, app_name="file_processor", session_service=session_service)

content = types.Content(role='user', parts=[types.Part(text="/path/to/file")])
events = [event async for event in runner.run_async(user_id="1234", session_id="session1234", new_message=content)]

assert len(events) == 7
assert events[-1].content.parts[0].text == "Successfully processed file: /path/to/file"
63 changes: 63 additions & 0 deletions src/google/adk/tests/unittests/tools/test_long_running_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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.

import pytest
from google.adk.tools.long_running_tool import LongRunningFunctionTool
from google.adk.tools.tool_context import ToolContext
from google.adk.models.llm_request import LlmRequest
from google.genai import types


@pytest.mark.asyncio
async def test_long_running_function_tool_with_list():
async def long_running_function(file_path: str) -> list:
return [
{"status": "pending", "message": f"Starting processing for {file_path}..."},
{"status": "pending", "progress": "20%", "estimated_completion_time": "~4 seconds remaining"},
{"status": "pending", "progress": "40%", "estimated_completion_time": "~3 seconds remaining"},
{"status": "pending", "progress": "60%", "estimated_completion_time": "~2 seconds remaining"},
{"status": "pending", "progress": "80%", "estimated_completion_time": "~1 second remaining"},
{"status": "completed", "result": f"Successfully processed file: {file_path}"}
]

tool = LongRunningFunctionTool(func=long_running_function)
tool_context = ToolContext(invocation_id="test_invocation_id")
args = {"file_path": "/path/to/file"}

result = await tool.run_async(args=args, tool_context=tool_context)

assert isinstance(result, list)
assert len(result) == 6
assert result[-1]["status"] == "completed"


@pytest.mark.asyncio
async def test_long_running_function_tool_with_generator():
async def long_running_function(file_path: str):
yield {"status": "pending", "message": f"Starting processing for {file_path}..."}
yield {"status": "pending", "progress": "20%", "estimated_completion_time": "~4 seconds remaining"}
yield {"status": "pending", "progress": "40%", "estimated_completion_time": "~3 seconds remaining"}
yield {"status": "pending", "progress": "60%", "estimated_completion_time": "~2 seconds remaining"}
yield {"status": "pending", "progress": "80%", "estimated_completion_time": "~1 second remaining"}
yield {"status": "completed", "result": f"Successfully processed file: {file_path}"}

tool = LongRunningFunctionTool(func=long_running_function)
tool_context = ToolContext(invocation_id="test_invocation_id")
args = {"file_path": "/path/to/file"}

result = await tool.run_async(args=args, tool_context=tool_context)

assert isinstance(result, list)
assert len(result) == 6
assert result[-1]["status"] == "completed"
9 changes: 9 additions & 0 deletions src/google/adk/tools/long_running_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ class LongRunningFunctionTool(FunctionTool):
def __init__(self, func: Callable):
super().__init__(func)
self.is_long_running = True

async def run_async(
self, *, args: dict[str, Any], tool_context: ToolContext
) -> Any:
result = await super().run_async(args=args, tool_context=tool_context)
if isinstance(result, list):
return result
else:
return list(result)