-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat: Added support for enums as arguments for function tools #3088
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
feat: Added support for enums as arguments for function tools #3088
Conversation
Summary of ChangesHello @SOORAJTS2001, I'm Gemini Code Assist1 ! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a significant enhancement by enabling the use of Python Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
@gemini-code-assist
gemini-code-assist
bot
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request adds support for using enums as parameters in function tools. The implementation correctly identifies enum types and extracts their values for the schema. However, there is a bug in handling default values for enum parameters, which will cause an exception. I've provided a code suggestion to fix this. Additionally, the unit tests for this new feature could be expanded to cover cases with default values to prevent such issues in the future. I've included examples of test cases that should be added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The handling of default values for Enum parameters is incorrect and will lead to a ValueError.
There are two issues with the current implementation:
- The
_is_default_value_compatiblefunction does not supportEnumtypes and will always returnFalse, causing an exception to be raised on line 154 if a default value is present. - On line 155,
schema.defaultis assigned theEnummember itself (param.default) instead of its value. Since the schema type isSTRING, the default value should also be a string (i.e.,param.default.value).
The suggested change below fixes both problems by performing the compatibility check locally and ensuring the correct value is assigned to schema.default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is due to reason that the default value get wipped of by this function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per this PR Gemini API now supports default value, hence commented the above function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new test for enums only covers the basic case without a default value. To ensure the implementation is robust and to prevent regressions, it's important to add test cases for function parameters with default values. This would have helped catch the bug with default value handling in the current implementation.
Please consider adding tests for the following scenarios:
- A valid default value provided as an enum member (e.g.,
param: MyEnum = MyEnum.VALUE). - A valid default value provided as a raw value (e.g.,
param: MyEnum = 'value'). - An invalid default value, which should raise a
ValueError.
Here's an example of how you could structure these tests:
import pytest def test_enum_with_default_member(): class InputEnum(Enum): AGENT = "agent" TOOL = "tool" def simple_function(input: InputEnum = InputEnum.AGENT): return input.value function_decl = _automatic_function_calling_util.build_function_declaration( func=simple_function ) assert function_decl.parameters.properties['input'].default == 'agent' def test_enum_with_invalid_default(): class InputEnum(Enum): AGENT = "agent" TOOL = "tool" def simple_function(input: InputEnum = "invalid_default"): return input.value with pytest.raises(ValueError): _automatic_function_calling_util.build_function_declaration( func=simple_function )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added test cases for default value as well as invalid value
fix: Add more test cases inside `test_build_function_declaration.py` for passing Enums as arguments
@seanzhou1023 please take a look at this
Hi @surajksharma07 !
Anything to be done from my side?
Hi @SOORAJTS2001,
Thank you for your contribution to the ADK project.
The changes in src/google/adk/tools/_function_parameter_parse_util.py to correctly parse Enum values and handle their defaults in the schema generation look correct now.
Before we proceed with the final review and merge, could you please address the following points?
-
Unit Test Enhancement: As noted in the review comments, robust testing is crucial. Please expand the unit tests in
test_build_function_declaration.pyto cover all scenarios forEnumparameters, including:- A case with a valid default value (e.g.,
param: MyEnum = MyEnum.VALUE). - A case that should fail when an invalid default value is provided.
- A case with a valid default value (e.g.,
-
Code Style: The code looks good and generally aligns with our style guide. Please just give it a final check to ensure all new code, including the tests, fully adheres to the project's formatting and linting rules.
Once these adjustments are made, we can proceed with the final testing and merge. Let us know if you have any questions.
Hi @surajksharma07
I have already added the test case for checking default value and invalid value as show here
@surajksharma07 @hangfei @seanzhou1023 any update on this?
@surajksharma07 @hangfei @seanzhou1023 @polong-lin
Anything to be done by my side?
Uh oh!
There was an error while loading. Please reload this page.
src/google/adk/tools/_function_parameter_parse_util.pyRan the whole unittests
image
For unittests/tools
image
Test result for test_build_function_declaration.py
image