fix(utils): exclude parameters with default values from required#666
Open
Ghraven wants to merge 1 commit into
Open
fix(utils): exclude parameters with default values from required#666Ghraven wants to merge 1 commit into
Ghraven wants to merge 1 commit into
Conversation
`convert_function_to_tool` previously listed every signature parameter
in `required`, even ones with default values. This is incorrect under
both the JSON Schema and OpenAI tool schema spec: a parameter with a
default is optional.
Example:
def get_weather(city: str, units: str = 'celsius'): ...
# Before: required = ['city', 'units']
# After: required = ['city']
The fix collects parameters that have a non-empty `default` from
`inspect.signature(func)` and removes them from the `required` list
after pydantic generates the schema. The existing `Union[X, None]`
handling is preserved and now guards against double-removal.
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.
Summary
convert_function_to_toolcurrently lists every signature parameter inrequired, even ones that have a default value. Under both the JSON Schema and OpenAI tool schema spec, a parameter with a default is optional and should not be inrequired.Repro
Before:
['city', 'units']After:
['city']In practice this confuses some tool-calling models into always passing every argument (often with the literal default repeated back), and it doesn't match what the upstream OpenAI / Anthropic-compat endpoints expect.
Fix
inspect.Parameter.defaultis notinspect.Parameter.empty.schema['required'].Union[X, None]block to check membership before calling.remove()so the two paths don't double-remove.The pydantic model is still built from
__annotations__+__signature__, so introspection metadata is unchanged.Tests
test_function_with_default_values_not_required— covers a mix of typed defaults includingboolandstr.test_function_with_all_defaults_has_empty_required— covers the all-optional case.tests/test_utils.pypass locally.Happy to adjust naming, split commits, or fold the regression test into the existing parametrized case if that would fit the codebase better.