-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Feat/streaming tools non live #3848
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
sarojrout
wants to merge
14
commits into
google:main
Choose a base branch
from
sarojrout:feat/streaming-tools-non-live
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+902
−31
Open
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ad8b67f
feat(flows): Add streaming tools support for non-live mode
sarojrout 5e9a352
feat(samples): Add streaming tools non-live agent sample
sarojrout 9c6e115
docs(samples): Update README with instructions
sarojrout 69385a2
refactor(flows): review comments incorporated for streaming tools
sarojrout 1fda809
perf(flows): Execute all tools concurrently and fix race condition in…
sarojrout 93da43f
minor review coment incorporated #3848
sarojrout bc155e4
fix(flows): Checked and propagated exceptions from concurrent tool ex…
sarojrout f142af8
fix(flows): Correct result_queue type hint to Optional[Any]
sarojrout ab4df98
review comments incorporated and refactored by using getattr and made…
sarojrout d74b44d
Merge branch 'main' into feat/streaming-tools-non-live
sarojrout 66e7046
fixed the unit tests by wrapping _yield_function_response_events with…
sarojrout b3e98db
fix(flows): Apply FunctionTool argument preprocessing and confirmatio…
sarojrout d1fae7b
refactor(flows): Extracted duplicate agent transfer logic into helper…
sarojrout 1f47d4d
small fix to reduce duplication
sarojrout File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
contributing/samples/streaming_tools_non_live_agent/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # Streaming Tools Non-Live Agent | ||
|
|
||
| This agent demonstrates streaming tools in non-live mode (run_async/SSE). | ||
|
|
||
| ## Features | ||
|
|
||
| - **monitor_stock_price**: Monitors stock prices with real-time updates | ||
| - **process_large_dataset**: Processes datasets with progress updates | ||
| - **monitor_system_health**: Monitors system health metrics continuously | ||
|
|
||
| ## Testing | ||
|
|
||
| ### With ADK Web UI | ||
|
|
||
| ```bash | ||
| cd contributing/samples | ||
| adk web . | ||
| ``` | ||
|
|
||
| Then try: | ||
| - "Monitor the stock price for AAPL" | ||
| - "Process a large dataset at /tmp/data.csv" | ||
| - "Monitor system health" | ||
|
|
||
| ### With ADK CLI | ||
|
|
||
| ```bash | ||
| cd contributing/samples/streaming_tools_non_live_agent | ||
| adk run . | ||
| ``` | ||
|
|
||
| ### With API Server (SSE) | ||
|
|
||
| ```bash | ||
| cd contributing/samples | ||
| adk api_server . | ||
| ``` | ||
|
|
||
| Then send a POST request to `/run_sse` with `streaming: true` to see intermediate Events. |
15 changes: 15 additions & 0 deletions
15
contributing/samples/streaming_tools_non_live_agent/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # 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. | ||
|
|
||
| from . import agent |
128 changes: 128 additions & 0 deletions
128
contributing/samples/streaming_tools_non_live_agent/agent.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| # 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. | ||
|
|
||
| """Example agent demonstrating streaming tools in non-live mode (run_async/SSE). | ||
|
|
||
| This agent shows how to use streaming tools that yield intermediate results | ||
| in non-live mode. Streaming tools work with both run_async and SSE endpoints. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| from typing import AsyncGenerator | ||
|
|
||
| from google.adk.agents import Agent | ||
|
|
||
|
|
||
| async def monitor_stock_price(symbol: str) -> AsyncGenerator[dict, None]: | ||
| """Monitor stock price with real-time updates. | ||
|
|
||
| This is a streaming tool that yields intermediate results as the stock | ||
| price changes. The agent can react to these intermediate results. | ||
|
|
||
| Args: | ||
| symbol: The stock symbol to monitor (e.g., 'AAPL', 'GOOGL'). | ||
|
|
||
| Yields: | ||
| Dictionary containing stock price updates with status indicators. | ||
| """ | ||
| # Simulate stock price changes | ||
| prices = [100, 105, 110, 108, 112, 115] | ||
| for i, price in enumerate(prices): | ||
| await asyncio.sleep(1) # Simulate real-time updates | ||
| yield { | ||
| 'symbol': symbol, | ||
| 'price': price, | ||
| 'update': i + 1, | ||
| 'status': 'streaming' if i < len(prices) - 1 else 'complete', | ||
| } | ||
|
|
||
|
|
||
| async def process_large_dataset(file_path: str) -> AsyncGenerator[dict, None]: | ||
| """Process dataset with progress updates. | ||
|
|
||
| This streaming tool demonstrates how to provide progress feedback | ||
| for long-running operations. | ||
|
|
||
| Args: | ||
| file_path: Path to the dataset file to process. | ||
|
|
||
| Yields: | ||
| Dictionary containing progress information and final result. | ||
| """ | ||
| total_rows = 100 | ||
| processed = 0 | ||
|
|
||
| # Simulate processing in batches | ||
| for batch in range(10): | ||
| await asyncio.sleep(0.5) # Simulate processing time | ||
| processed += 10 | ||
| yield { | ||
| 'progress': processed / total_rows, | ||
| 'processed': processed, | ||
| 'total': total_rows, | ||
| 'status': 'streaming', | ||
| 'message': f'Processed {processed}/{total_rows} rows', | ||
| } | ||
|
|
||
| # Final result | ||
| yield { | ||
| 'result': 'Processing complete', | ||
| 'status': 'complete', | ||
| 'file_path': file_path, | ||
| 'total_processed': total_rows, | ||
| } | ||
|
|
||
|
|
||
| async def monitor_system_health() -> AsyncGenerator[dict, None]: | ||
| """Monitor system health metrics with continuous updates. | ||
|
|
||
| This streaming tool demonstrates continuous monitoring that can be | ||
| stopped by the agent when thresholds are reached. | ||
|
|
||
| Yields: | ||
| Dictionary containing system health metrics. | ||
| """ | ||
| metrics = [ | ||
| {'cpu': 45, 'memory': 60, 'disk': 70}, | ||
| {'cpu': 50, 'memory': 65, 'disk': 72}, | ||
| {'cpu': 55, 'memory': 70, 'disk': 75}, | ||
| {'cpu': 60, 'memory': 75, 'disk': 78}, | ||
| ] | ||
|
|
||
| for i, metric in enumerate(metrics): | ||
| await asyncio.sleep(2) # Check every 2 seconds | ||
| yield { | ||
| 'metrics': metric, | ||
| 'timestamp': i + 1, | ||
| 'status': 'streaming' if i < len(metrics) - 1 else 'complete', | ||
| 'alert': 'high' if metric['cpu'] > 55 else 'normal', | ||
| } | ||
|
|
||
|
|
||
| root_agent = Agent( | ||
| name='streaming_tools_agent', | ||
| model='gemini-2.5-flash-lite', | ||
| instruction=( | ||
| 'You are a helpful assistant that can monitor stock prices, process' | ||
| ' datasets, and monitor system health using streaming tools. When' | ||
| ' using streaming tools, you will receive intermediate results that' | ||
| ' you can react to. For example, if monitoring stock prices, you can' | ||
| ' alert the user when prices change significantly. If processing a' | ||
| ' dataset, you can provide progress updates. If monitoring system' | ||
| ' health, you can alert when metrics exceed thresholds.' | ||
| ), | ||
| tools=[monitor_stock_price, process_large_dataset, monitor_system_health], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.