Skip to content

Conversation

SOORAJTS2001
Copy link

@SOORAJTS2001 SOORAJTS2001 commented Oct 3, 2025

Ran the whole unittests
image

For unittests/tools
image

Test result for test_build_function_declaration.py
image

Copy link

Summary of Changes

Hello @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 Enum types as arguments for function tools. This change improves the robustness and expressiveness of tool definitions by allowing developers to specify a fixed set of valid choices for parameters, which is then correctly reflected in the generated tool schemas. The update ensures that Enum values are properly converted to string types with enumerated options, providing a more structured and error-resistant way to define tool interfaces.

Highlights

  • Enum Support for Function Tools: Added comprehensive support for using Python Enum types as arguments for function tools, enhancing type safety and clarity in tool definitions.
  • Automated Schema Generation: The function parameter parser (_function_parameter_parse_util.py) has been updated to correctly interpret Enum parameters, mapping them to STRING types in the generated tool schemas and populating the enum field with the value of each enum member.
  • Default Value Handling: Logic has been included to properly handle and validate default values for Enum parameters during schema generation.
  • New Unit Tests: A new unit test (test_enums) has been added to test_build_function_declaration.py to verify the accurate parsing and schema generation for functions utilizing Enum parameters.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@adk-bot adk-bot added bot triaged [Bot] This issue is triaged by ADK bot tools [Component] This issue is related to tools labels Oct 3, 2025
@adk-bot adk-bot requested a review from seanzhou1023 October 3, 2025 21:09
Copy link

@gemini-code-assist gemini-code-assist bot left a 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.

Comment on lines 153 to 155
if not _is_default_value_compatible(param.default, param.annotation):
raise ValueError(default_value_error_msg)
schema.default = param.default

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The handling of default values for Enum parameters is incorrect and will lead to a ValueError.

There are two issues with the current implementation:

  1. The _is_default_value_compatible function does not support Enum types and will always return False, causing an exception to be raised on line 154 if a default value is present.
  2. On line 155, schema.default is assigned the Enum member itself (param.default) instead of its value. Since the schema type is STRING, 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.

Suggested change
if not _is_default_value_compatible(param.default, param.annotation):
raise ValueError(default_value_error_msg)
schema.default = param.default
default_value = param.default.value if isinstance(param.default, Enum) else param.default
if default_value not in schema.enum:
raise ValueError(default_value_error_msg)
schema.default = default_value

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

This is due to reason that the default value get wipped of by this function

Copy link
Author

@SOORAJTS2001 SOORAJTS2001 Oct 4, 2025

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

Comment on lines 222 to 238
def test_enums():

class InputEnum(Enum):
AGENT = "agent"
TOOL = "tool"

def simple_function(input:InputEnum):
return input.value

function_decl = _automatic_function_calling_util.build_function_declaration(
func=simple_function
)

assert function_decl.name == 'simple_function'
assert function_decl.parameters.type == 'OBJECT'
assert function_decl.parameters.properties['input'].type == 'STRING'
assert function_decl.parameters.properties['input'].enum == ['agent', 'tool']

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
        )

Copy link
Author

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

SOORAJTS2001 and others added 2 commits October 4, 2025 15:16
fix: Add more test cases inside `test_build_function_declaration.py` for passing Enums as arguments
@SOORAJTS2001
Copy link
Author

@seanzhou1023 please take a look at this

@surajksharma07 surajksharma07 self-assigned this Oct 6, 2025
@SOORAJTS2001
Copy link
Author

Hi @surajksharma07 !
Anything to be done from my side?

@surajksharma07
Copy link
Collaborator

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?

  1. Unit Test Enhancement: As noted in the review comments, robust testing is crucial. Please expand the unit tests in test_build_function_declaration.py to cover all scenarios for Enum parameters, 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.
  2. 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.

@surajksharma07 surajksharma07 added the request clarification [Status] The maintainer need clarification or more information from the author label Oct 7, 2025
@SOORAJTS2001
Copy link
Author

image

Hi @surajksharma07
I have already added the test case for checking default value and invalid value as show here

@surajksharma07
Copy link
Collaborator

@seanzhou1023 please take a look into this

This PR from @SOORAJTS2001 adds support for Enum types in tool arguments.

The implementation looks good, and the contributor has added comprehensive unit tests covering the main logic and edge cases as requested.

Thanks.

@surajksharma07 surajksharma07 requested a review from hangfei October 7, 2025 18:50
@surajksharma07 surajksharma07 added need attention [Status] This issue is important but the maintainers haven't started working on it wip [Status] This issue is being worked on. Either there is a pending PR or is planned to be fixed labels Oct 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot triaged [Bot] This issue is triaged by ADK bot need attention [Status] This issue is important but the maintainers haven't started working on it request clarification [Status] The maintainer need clarification or more information from the author tools [Component] This issue is related to tools wip [Status] This issue is being worked on. Either there is a pending PR or is planned to be fixed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants