1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import collections .abc
16+ from typing import AsyncGenerator
1517from unittest .mock import MagicMock
1618
1719from google .adk .agents .invocation_context import InvocationContext
@@ -200,9 +202,11 @@ async def test_run_async_1_missing_arg_sync_func():
200202 args = {"arg1" : "test_value_1" }
201203 result = await tool .run_async (args = args , tool_context = MagicMock ())
202204 assert result == {
203- "error" : """Invoking `function_for_testing_with_2_arg_and_no_tool_context()` failed as the following mandatory input parameters are not present:
205+ "error" : (
206+ """Invoking `function_for_testing_with_2_arg_and_no_tool_context()` failed as the following mandatory input parameters are not present:
204207arg2
205208You could retry calling this tool, but it is IMPORTANT for you to provide all the mandatory parameters."""
209+ )
206210 }
207211
208212
@@ -213,9 +217,11 @@ async def test_run_async_1_missing_arg_async_func():
213217 args = {"arg2" : "test_value_1" }
214218 result = await tool .run_async (args = args , tool_context = MagicMock ())
215219 assert result == {
216- "error" : """Invoking `async_function_for_testing_with_2_arg_and_no_tool_context()` failed as the following mandatory input parameters are not present:
220+ "error" : (
221+ """Invoking `async_function_for_testing_with_2_arg_and_no_tool_context()` failed as the following mandatory input parameters are not present:
217222arg1
218223You could retry calling this tool, but it is IMPORTANT for you to provide all the mandatory parameters."""
224+ )
219225 }
220226
221227
@@ -226,11 +232,13 @@ async def test_run_async_3_missing_arg_sync_func():
226232 args = {"arg2" : "test_value_1" }
227233 result = await tool .run_async (args = args , tool_context = MagicMock ())
228234 assert result == {
229- "error" : """Invoking `function_for_testing_with_4_arg_and_no_tool_context()` failed as the following mandatory input parameters are not present:
235+ "error" : (
236+ """Invoking `function_for_testing_with_4_arg_and_no_tool_context()` failed as the following mandatory input parameters are not present:
230237arg1
231238arg3
232239arg4
233240You could retry calling this tool, but it is IMPORTANT for you to provide all the mandatory parameters."""
241+ )
234242 }
235243
236244
@@ -241,11 +249,13 @@ async def test_run_async_3_missing_arg_async_func():
241249 args = {"arg3" : "test_value_1" }
242250 result = await tool .run_async (args = args , tool_context = MagicMock ())
243251 assert result == {
244- "error" : """Invoking `async_function_for_testing_with_4_arg_and_no_tool_context()` failed as the following mandatory input parameters are not present:
252+ "error" : (
253+ """Invoking `async_function_for_testing_with_4_arg_and_no_tool_context()` failed as the following mandatory input parameters are not present:
245254arg1
246255arg2
247256arg4
248257You could retry calling this tool, but it is IMPORTANT for you to provide all the mandatory parameters."""
258+ )
249259 }
250260
251261
@@ -256,12 +266,14 @@ async def test_run_async_missing_all_arg_sync_func():
256266 args = {}
257267 result = await tool .run_async (args = args , tool_context = MagicMock ())
258268 assert result == {
259- "error" : """Invoking `function_for_testing_with_4_arg_and_no_tool_context()` failed as the following mandatory input parameters are not present:
269+ "error" : (
270+ """Invoking `function_for_testing_with_4_arg_and_no_tool_context()` failed as the following mandatory input parameters are not present:
260271arg1
261272arg2
262273arg3
263274arg4
264275You could retry calling this tool, but it is IMPORTANT for you to provide all the mandatory parameters."""
276+ )
265277 }
266278
267279
@@ -272,12 +284,14 @@ async def test_run_async_missing_all_arg_async_func():
272284 args = {}
273285 result = await tool .run_async (args = args , tool_context = MagicMock ())
274286 assert result == {
275- "error" : """Invoking `async_function_for_testing_with_4_arg_and_no_tool_context()` failed as the following mandatory input parameters are not present:
287+ "error" : (
288+ """Invoking `async_function_for_testing_with_4_arg_and_no_tool_context()` failed as the following mandatory input parameters are not present:
276289arg1
277290arg2
278291arg3
279292arg4
280293You could retry calling this tool, but it is IMPORTANT for you to provide all the mandatory parameters."""
294+ )
281295 }
282296
283297
@@ -428,3 +442,19 @@ def explicit_params_func(arg1: str, arg2: int):
428442 assert result == {"arg1" : "test" , "arg2" : 42 }
429443 # Explicitly verify that unexpected_param was filtered out and not passed to the function
430444 assert "unexpected_param" not in result
445+
446+
447+ @pytest .mark .asyncio
448+ async def test_run_async_streaming_generator ():
449+ """Test that run_async consumes the async generator and returns a list."""
450+
451+ async def streaming_tool (param : str ) -> AsyncGenerator [str , None ]:
452+ yield f"part 1 { param } "
453+ yield f"part 2 { param } "
454+
455+ tool = FunctionTool (streaming_tool )
456+
457+ result = await tool .run_async (args = {"param" : "test" }, tool_context = None )
458+
459+ assert isinstance (result , list )
460+ assert result == ["part 1 test" , "part 2 test" ]
0 commit comments