chore: import upstream snapshot with attribution
Continuous Integration / Pre-commit Linter (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.10) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.11) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.12) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.13) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.10) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.11) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.12) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.13) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.14) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.10) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.11) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.12) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.13) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.14) (push) Waiting to run
Copybara PR Handler / close-imported-pr (push) Waiting to run
Continuous Integration / Pre-commit Linter (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.10) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.11) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.12) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.13) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.10) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.11) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.12) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.13) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.14) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.10) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.11) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.12) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.13) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.14) (push) Waiting to run
Copybara PR Handler / close-imported-pr (push) Waiting to run
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
# Long Running Functions
|
||||
|
||||
## Overview
|
||||
|
||||
This sample demonstrates how to use `LongRunningFunctionTool` in ADK to handle operations that take a significant amount of time to complete.
|
||||
|
||||
When a tool is marked as long-running, the framework understands that the function may return a pending status and that the final result will be provided asynchronously. This is useful for tasks like starting a background job, requesting human approval, or any operation where the result is not immediately available.
|
||||
|
||||
## Sample Inputs
|
||||
|
||||
- `Export my data to CSV`
|
||||
|
||||
- `Start a JSON data export`
|
||||
|
||||
- `Export my data to both CSV and JSON simultaneously`
|
||||
|
||||
## Graph
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
actor User
|
||||
participant Agent
|
||||
participant Tool
|
||||
|
||||
User->>Agent: "Export my data to CSV"
|
||||
Agent->>Tool: export_data(export_type="csv")
|
||||
Tool-->>Agent: {"status": "pending", ...}
|
||||
Agent-->>User: "Started csv export. Ticket ID: export-12345"
|
||||
```
|
||||
|
||||
## How To
|
||||
|
||||
To create a long-running function tool:
|
||||
|
||||
1. Define your Python function. It can return a status indicating it is in-progress (e.g., `{"status": "in-progress"}`).
|
||||
1. Wrap the function using `LongRunningFunctionTool(func=your_function)`.
|
||||
1. Pass the wrapped tool to the `Agent`.
|
||||
|
||||
After the initial in-progress response, you can send additional function responses (e.g., containing progress updates like `{"status": "in-progress", "progress": "50%"}`) to the agent. This will trigger another model turn, allowing the agent to report the current progress back to the user.
|
||||
|
||||
In the ADK Web UI, you can send an additional response by hovering over the function response button and selecting 'Send another response' from the menu.
|
||||
|
||||
```python
|
||||
from google.adk import Agent
|
||||
from google.adk.tools.long_running_tool import LongRunningFunctionTool
|
||||
def export_data(export_type: str) -> dict[str, str]:
|
||||
# Start async task...
|
||||
return {
|
||||
"status": "in-progress",
|
||||
"progress": "0%",
|
||||
"message": f"Exporting {export_type} data. This may take some time.",
|
||||
}
|
||||
|
||||
|
||||
agent = Agent(
|
||||
name="my_agent",
|
||||
model="gemini-2.5-flash",
|
||||
tools=[LongRunningFunctionTool(func=export_data)],
|
||||
)
|
||||
```
|
||||
@@ -0,0 +1,15 @@
|
||||
# Copyright 2026 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.
|
||||
|
||||
from . import agent
|
||||
@@ -0,0 +1,44 @@
|
||||
# Copyright 2026 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.
|
||||
|
||||
from google.adk import Agent
|
||||
from google.adk.tools.long_running_tool import LongRunningFunctionTool
|
||||
|
||||
|
||||
def export_data(export_type: str) -> dict[str, str]:
|
||||
"""Exports user data.
|
||||
|
||||
Args:
|
||||
export_type: The type of data to export (e.g., 'csv', 'json').
|
||||
|
||||
Returns:
|
||||
A dict with the status.
|
||||
"""
|
||||
# In a real application, this would kick off a background job.
|
||||
# Here we just return a status.
|
||||
return {
|
||||
"status": "in-progress",
|
||||
"progress": "0%",
|
||||
"message": f"Exporting {export_type} data. This may take some time.",
|
||||
}
|
||||
|
||||
|
||||
root_agent = Agent(
|
||||
name="long_running_functions",
|
||||
instruction="""
|
||||
You are an assistant that can export user data.
|
||||
When the user asks to export data, call the `export_data` tool.
|
||||
""",
|
||||
tools=[LongRunningFunctionTool(func=export_data)],
|
||||
)
|
||||
@@ -0,0 +1,127 @@
|
||||
{
|
||||
"events": [
|
||||
{
|
||||
"author": "user",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "export to csv"
|
||||
}
|
||||
],
|
||||
"role": "user"
|
||||
},
|
||||
"id": "e-1",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "long_running_functions",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"functionCall": {
|
||||
"args": {
|
||||
"export_type": "csv"
|
||||
},
|
||||
"id": "fc-1",
|
||||
"name": "export_data"
|
||||
}
|
||||
}
|
||||
],
|
||||
"role": "model"
|
||||
},
|
||||
"finishReason": "STOP",
|
||||
"id": "e-2",
|
||||
"invocationId": "i-1",
|
||||
"longRunningToolIds": [
|
||||
"fc-1"
|
||||
],
|
||||
"nodeInfo": {
|
||||
"path": "long_running_functions@1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "long_running_functions",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"functionResponse": {
|
||||
"id": "fc-1",
|
||||
"name": "export_data",
|
||||
"response": {
|
||||
"message": "Exporting csv data. This may take some time.",
|
||||
"progress": "0%",
|
||||
"status": "in-progress"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"role": "user"
|
||||
},
|
||||
"id": "e-3",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": "long_running_functions@1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "long_running_functions",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "I'm exporting your data to CSV. This may take some time. I will let you know when it's done."
|
||||
}
|
||||
],
|
||||
"role": "model"
|
||||
},
|
||||
"finishReason": "STOP",
|
||||
"id": "e-4",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": "long_running_functions@1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "user",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"functionResponse": {
|
||||
"id": "fc-1",
|
||||
"name": "export_data",
|
||||
"response": {
|
||||
"progress": "50%",
|
||||
"status": "in-progress"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"role": "user"
|
||||
},
|
||||
"id": "e-5",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "long_running_functions",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "Your data is 50% exported. The export is still in progress."
|
||||
}
|
||||
],
|
||||
"role": "model"
|
||||
},
|
||||
"finishReason": "STOP",
|
||||
"id": "e-6",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": "long_running_functions@1"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
{
|
||||
"events": [
|
||||
{
|
||||
"author": "user",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "export to csv and json"
|
||||
}
|
||||
],
|
||||
"role": "user"
|
||||
},
|
||||
"id": "e-1",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "long_running_functions",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"functionCall": {
|
||||
"args": {
|
||||
"export_type": "csv"
|
||||
},
|
||||
"id": "fc-1",
|
||||
"name": "export_data"
|
||||
}
|
||||
},
|
||||
{
|
||||
"functionCall": {
|
||||
"args": {
|
||||
"export_type": "json"
|
||||
},
|
||||
"id": "fc-2",
|
||||
"name": "export_data"
|
||||
}
|
||||
}
|
||||
],
|
||||
"role": "model"
|
||||
},
|
||||
"finishReason": "STOP",
|
||||
"id": "e-2",
|
||||
"invocationId": "i-1",
|
||||
"longRunningToolIds": [
|
||||
"fc-1",
|
||||
"fc-2"
|
||||
],
|
||||
"nodeInfo": {
|
||||
"path": "long_running_functions@1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "long_running_functions",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"functionResponse": {
|
||||
"id": "fc-1",
|
||||
"name": "export_data",
|
||||
"response": {
|
||||
"message": "Exporting csv data. This may take some time.",
|
||||
"progress": "0%",
|
||||
"status": "in-progress"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"functionResponse": {
|
||||
"id": "fc-2",
|
||||
"name": "export_data",
|
||||
"response": {
|
||||
"message": "Exporting json data. This may take some time.",
|
||||
"progress": "0%",
|
||||
"status": "in-progress"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"role": "user"
|
||||
},
|
||||
"id": "e-3",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": "long_running_functions@1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "long_running_functions",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "I've started exporting your data to both CSV and JSON formats. This may take some time. I will notify you when it's complete."
|
||||
}
|
||||
],
|
||||
"role": "model"
|
||||
},
|
||||
"finishReason": "STOP",
|
||||
"id": "e-4",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": "long_running_functions@1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "user",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"functionResponse": {
|
||||
"id": "fc-1",
|
||||
"name": "export_data",
|
||||
"response": {
|
||||
"progress": "50%",
|
||||
"status": "in-progress"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"role": "user"
|
||||
},
|
||||
"id": "e-5",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "long_running_functions",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "I've started exporting your data to both CSV and JSON formats. This may take some time to complete."
|
||||
}
|
||||
],
|
||||
"role": "model"
|
||||
},
|
||||
"finishReason": "STOP",
|
||||
"id": "e-6",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": "long_running_functions@1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "user",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"functionResponse": {
|
||||
"id": "fc-2",
|
||||
"name": "export_data",
|
||||
"response": {
|
||||
"status": "completed"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"role": "user"
|
||||
},
|
||||
"id": "e-7",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "long_running_functions",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "I've started exporting your data to CSV and JSON. The JSON export is complete, and the CSV export is 50% complete. I'll let you know when the CSV export is finished."
|
||||
}
|
||||
],
|
||||
"role": "model"
|
||||
},
|
||||
"finishReason": "STOP",
|
||||
"id": "e-8",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": "long_running_functions@1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "user",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"functionResponse": {
|
||||
"id": "fc-1",
|
||||
"name": "export_data",
|
||||
"response": {
|
||||
"status": "completed"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"role": "user"
|
||||
},
|
||||
"id": "e-9",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "long_running_functions",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "I have exported the data to both CSV and JSON formats."
|
||||
}
|
||||
],
|
||||
"role": "model"
|
||||
},
|
||||
"finishReason": "STOP",
|
||||
"id": "e-10",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": "long_running_functions@1"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user