Repository navigation
fix(tools): convert Enum tool arguments to the declared member - #7636
Open
Somay-kousis wants to merge 1 commit into
Open
Somay-kousis wants to merge 1 commit into
Somay-kousis wants to merge 1 commit into
Conversation
The function declaration lists an Enum parameter's values, so the model sends one of them back. With FUNCTION_TOOL_ARG_VALIDATION off (the default), preprocess_args passed that raw value through, so a tool that used the member (unit.value, unit is Unit.X) raised and ended the run. preprocess_args now looks up the member for Enum, Optional[Enum] and Enum | None parameters. None and existing members are left alone, and an unknown value is passed through with a warning, as a failed pydantic conversion is.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
2. Or, if no issue exists, describe the change:
Problem:
A function tool with an
Enumparameter got the raw value ("celsius") instead of the member on the default path (FUNCTION_TOOL_ARG_VALIDATIONoff). The declaration lists the enum's values, so the model sends one back, and tool code that uses the member (unit.value,is Unit.X) raised and ended the run.Solution:
_schema_utils.preprocess_argsnow converts the argument to its Enum member when the parameter is annotated with anEnumsubclass,Optional[...]of one, or... | None. It sits next to the existing pydantic model andinthandling on the default path."celsius") becomes the member (Unit.CELSIUS).IntEnumvalue that came back as a float (2.0, from a protoStructround trip) is matched too.Noneand values that already are the member are left alone.FUNCTION_TOOL_ARG_VALIDATION.Testing Plan
Unit Tests:
New tests in
tests/unittests/tools/test_function_tool.py:test_run_async_converts_enum_value_to_membertest_run_async_converts_optional_enum_value_to_member(also checks thatNonelogs no warning)test_run_async_converts_int_enum_value_from_floattest_run_async_passes_through_unknown_enum_valuetest_run_async_keeps_enum_member_argThe two that pass on main are guards (unknown value passes through, an existing member is kept). Breaking each part of the change (the lookup, the try/except, the
Noneguard) makes at least one new test fail.tests/unittests/toolsandtests/unittests/utilspass (3883 tests). isort and pyink are clean.Manual End-to-End (E2E) Tests:
The repro in the issue (
Agent+InMemoryRunner, scripted model):Checklist
Additional context
Containers of enums (
list[Unit]) are not converted here.