Repository navigation
Python: fix(core): accept whole-number floats for JSON Schema integer type - #9852
Conversation
JSON Schema 'integer' means 'a number without a fraction', so 3.0 is valid. The _matches_json_schema_type check only accepted Python int, rejecting 3.0 with a TypeError. Accept floats whose is_integer() is true alongside int. Fixes microsoft#9843
There was a problem hiding this comment.
🟡 Changes recommended
Accepted whole-number floats remain unconverted in integer-typed tool arguments, causing TypeError in callables that require actual integers.
1 open issue
What changed in this PR
This PR updates Python core tool validation to accept whole-number floats for JSON Schema integer parameters, addressing the validation failure reported in #9843.
Changes:
- Accepts floats such as
3.0while continuing to reject booleans and fractional floats. - Adds a regression test covering acceptance of
3.0and rejection of3.5.
| File | Description |
|---|---|
python/packages/core/tests/core/test_tools.py |
Adds coverage for whole-number and fractional float arguments. |
python/packages/core/agent_framework/_tools.py |
Extends the integer type check to accept whole-number floats. |
Review effort: Balanced
|
The failing check here is The same test passes on the ubuntu 3.11 through 3.14 shards and only fails on the Windows shards plus 3.15, which is consistent with an environment specific or flaky test rather than this change. A pure Python type check in the |
…nteger-whole-number
… one Accepting 3.0 for a JSON Schema "integer" was only half of microsoft#9843. The value was validated and then passed through unchanged, so a tool that runs range(count) or indexes with it still raised TypeError on the float the model sent. The schema declares an integer, so the callable should receive one. _validate_arguments_against_schema now normalizes during the same top-level pass it validates in. A whole-number float becomes int when "integer" is the only numeric type allowed, which covers the plain case and the nullable one ("type": ["integer", "null"]). When "number" is allowed too the declaration permits fractions, so the value's own type is the answer and nothing is narrowed to fit a type the schema never exclusively asked for. This is the schema-supplied path only. Tools built from Python annotations go through the input model, which already converts to the annotated types, and that path returns before these checks run. The regression now asserts the type the callable receives rather than only that the call did not raise, and two more pin the nullable case and the number-alongside-integer one.
…nteger-whole-number

Fixes #9843
Problem
_matches_json_schema_typerejects3.0for"integer"schema types. JSON Schema "integer" means "a number without a fraction", so whole-number floats like3.0are valid. AFunctionTooldeclared with a JSON schema rejects provider arguments containing such values withTypeError: Invalid type for 'count'.Fix
Accept
floatvalues whoseis_integer()is true alongsideint, while still rejectingbooland non-whole floats like3.5.Testing
New test
test_tool_json_schema_integer_accepts_whole_number_float:3.0invokes without error (valid JSON Schema integer)3.5raisesTypeError(not a valid integer)AI Assistance
AI assistance details: implementation, regression tests, and this PR description. The author reviewed the final diff and ran the targeted core tool tests.