Async Function Support for Tools Parameter in GenerativeModel #624
Labels
status:triaged
Issue/PR triaged to the corresponding sub-team
type:feature request
New feature request/enhancement
Description
The current implementation of the
GenerativeModel
class doesn't properly handle async functions when passed as tools, resulting in coroutine objects never being awaited and causing runtime errors.Problem
When passing async functions as tools to the GenerativeModel, the following errors occur:
RuntimeWarning: coroutine was never awaited
Error: Unable to coerce value: <coroutine object>
Parameter to MergeFrom() must be instance of same class: expected <class 'Part'> got <class 'coroutine'>
Root Cause
The
CallableFunctionDeclaration
class andFunctionLibrary
class in content_types.py don't properly handle coroutines returned by async functions. The current implementation attempts to use async function returns directly without awaiting them.Solution
Modified the following classes in content_types.py to properly handle async functions:
And the
FunctionLibrary
class:Key Changes
inspect.iscoroutinefunction()
CallableFunctionDeclaration.__call__
an async methodTesting
The solution was tested with async functions passed as tools to the GenerativeModel:
Impact
This fix allows developers to use async functions as tools in the GenerativeModel, enabling integration with asynchronous APIs and services while maintaining proper coroutine handling.
References
Actual vs expected behavior:
Expected Behavior:
Actual Behavior:
RuntimeWarning: coroutine was never awaited
Error: Unable to coerce value: <coroutine object>
Parameter to MergeFrom() must be instance of same class: expected <class 'Part'> got <class 'coroutine'>
Any other information you'd like to share?
The text was updated successfully, but these errors were encountered: