|
14 | 14 |
|
15 | 15 | from collections.abc import Sequence |
16 | 16 | from typing import Any |
| 17 | +from typing import AsyncGenerator |
17 | 18 | from typing import Dict |
| 19 | +from typing import Generator |
18 | 20 |
|
19 | 21 | from google.adk.tools import _automatic_function_calling_util |
20 | 22 | from google.adk.utils.variant_utils import GoogleLLMVariant |
@@ -242,3 +244,78 @@ def test_function( |
242 | 244 | assert declaration.name == 'test_function' |
243 | 245 | assert declaration.response.type == types.Type.ARRAY |
244 | 246 | assert declaration.response.items.type == types.Type.STRING |
| 247 | + |
| 248 | + |
| 249 | +def test_from_function_with_async_generator_return_vertex(): |
| 250 | + """Test from_function_with_options with AsyncGenerator return for VERTEX_AI.""" |
| 251 | + |
| 252 | + async def test_function(param: str) -> AsyncGenerator[str, None]: |
| 253 | + """A streaming function that yields strings.""" |
| 254 | + yield param |
| 255 | + |
| 256 | + declaration = _automatic_function_calling_util.from_function_with_options( |
| 257 | + test_function, GoogleLLMVariant.VERTEX_AI |
| 258 | + ) |
| 259 | + |
| 260 | + assert declaration.name == 'test_function' |
| 261 | + assert declaration.parameters.type == 'OBJECT' |
| 262 | + assert declaration.parameters.properties['param'].type == 'STRING' |
| 263 | + # VERTEX_AI should extract yield type (str) from AsyncGenerator[str, None] |
| 264 | + assert declaration.response is not None |
| 265 | + assert declaration.response.type == types.Type.STRING |
| 266 | + |
| 267 | + |
| 268 | +def test_from_function_with_async_generator_return_gemini(): |
| 269 | + """Test from_function_with_options with AsyncGenerator return for GEMINI_API.""" |
| 270 | + |
| 271 | + async def test_function(param: str) -> AsyncGenerator[str, None]: |
| 272 | + """A streaming function that yields strings.""" |
| 273 | + yield param |
| 274 | + |
| 275 | + declaration = _automatic_function_calling_util.from_function_with_options( |
| 276 | + test_function, GoogleLLMVariant.GEMINI_API |
| 277 | + ) |
| 278 | + |
| 279 | + assert declaration.name == 'test_function' |
| 280 | + assert declaration.parameters.type == 'OBJECT' |
| 281 | + assert declaration.parameters.properties['param'].type == 'STRING' |
| 282 | + # GEMINI_API should not have response schema |
| 283 | + assert declaration.response is None |
| 284 | + |
| 285 | + |
| 286 | +def test_from_function_with_generator_return_vertex(): |
| 287 | + """Test from_function_with_options with Generator return for VERTEX_AI.""" |
| 288 | + |
| 289 | + def test_function(param: str) -> Generator[int, None, None]: |
| 290 | + """A streaming function that yields integers.""" |
| 291 | + yield 42 |
| 292 | + |
| 293 | + declaration = _automatic_function_calling_util.from_function_with_options( |
| 294 | + test_function, GoogleLLMVariant.VERTEX_AI |
| 295 | + ) |
| 296 | + |
| 297 | + assert declaration.name == 'test_function' |
| 298 | + assert declaration.parameters.type == 'OBJECT' |
| 299 | + assert declaration.parameters.properties['param'].type == 'STRING' |
| 300 | + # VERTEX_AI should extract yield type (int) from Generator[int, None, None] |
| 301 | + assert declaration.response is not None |
| 302 | + assert declaration.response.type == types.Type.INTEGER |
| 303 | + |
| 304 | + |
| 305 | +def test_from_function_with_async_generator_complex_yield_type_vertex(): |
| 306 | + """Test from_function_with_options with AsyncGenerator yielding dict.""" |
| 307 | + |
| 308 | + async def test_function(param: str) -> AsyncGenerator[Dict[str, str], None]: |
| 309 | + """A streaming function that yields dicts.""" |
| 310 | + yield {'result': param} |
| 311 | + |
| 312 | + declaration = _automatic_function_calling_util.from_function_with_options( |
| 313 | + test_function, GoogleLLMVariant.VERTEX_AI |
| 314 | + ) |
| 315 | + |
| 316 | + assert declaration.name == 'test_function' |
| 317 | + assert declaration.parameters.type == 'OBJECT' |
| 318 | + assert declaration.parameters.properties['param'].type == 'STRING' |
| 319 | + # VERTEX_AI should extract yield type (Dict[str, str]) from AsyncGenerator |
| 320 | + assert declaration.response is not None |
| 321 | + assert declaration.response.type == types.Type.OBJECT |
0 commit comments