Repository navigation
Conversation
Reviewer's GuideImplementationParse JSON decimals exactly and treat finite float or Decimal values without a fractional part as integers, while retaining existing primitive-type checks.
DocumentationDocument that integer schemas accept numeric values with no fractional part, including decimal notation.
TestsCover integral and fractional representations, nested pipeline validation, and compatibility for number and nullable numeric schemas.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
PR Summary by Qodofix(python-sdk): accept integral floats in integer schemas
AI Description
Merge Risk
|
Code Review by Qodo
1.
|
Linked issuesChecked at commit e1a4c33 #2017 Bug: Schema guard rejects integral JSON numbers written with a decimal point · 2 of 2 met
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="sdk/python/src/openlit/guard/schema.py" line_range="32" />
<code_context>
"null": type(None),
}
expected = type_map.get(schema_type)
+ if schema_type == "integer" and isinstance(data, float) and data.is_integer():
+ expected = (int, float)
# In Python, bool is a subclass of int, but JSON Schema treats booleans
</code_context>
<issue_to_address>
**Integer schemas accept invalid numbers**
When JSON number parsing rounds a fractional value to an integer-valued float or overflows an integral value to infinity, `data.is_integer()` accepts the rounded fractional value and rejects the overflowed integral value, so the integer schema allows invalid output and denies valid output.
Preserve exact JSON number values and validate integer-ness without relying on binary-float conversion.
</issue_to_address>
### Comment 2
<location path="sdk/python/tests/test_guard_schema.py" line_range="65" />
<code_context>
+)
+def test_integer_representations_work_in_postflight_pipeline(schema, output):
+ """Structured agent outputs should pass the same checks at nested paths."""
+ result = Pipeline(guards=[Schema(schema=schema)]).evaluate(output, phase="postflight")
+
+ assert result.action == GuardAction.ALLOW
</code_context>
<issue_to_address>
**Nested guard failures pass**
When the schema guard raises while validating a nested value, when `Schema.evaluate` raises while validating a nested value, `test_integer_representations_work_in_postflight_pipeline` asserts only `result.action` at line 65; the pipeline catches the exception and allows by default, so the test passes without validation succeeding.
Construct the pipeline with `fail_open=False` so guard exceptions fail the regression test.
</issue_to_address>Sourcery assessment
Approval pending. 2 findings to address first.
Blocking findings: sdk/python/src/openlit/guard/schema.py:32, sdk/python/tests/test_guard_schema.py:65
Issue number: Fixes #2017
Change description:
Accept numeric values with no fractional part as JSON Schema integers, including 1.0 and integral exponential notation. Keep rejecting fractional numbers, booleans, strings and null. Add root and nested pipeline regressions and document the numeric semantics in the Schema docstring.
Validation (Python 3.12, Linux): Seven regressions fail on main with 17 passing controls. With the fix, all 91 guard/pipeline/integration tests pass. Full SDK Pylint rates 10.00/10; poetry check and sdist/wheel builds pass. Expectations follow the JSON Schema numeric type reference. Live provider tests require credentials and were not run.
Checklist
Acknowledgment
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the project license.
Prepared and verified with OpenAI Codex assistance. The linked issue was opened with the executed reproduction before implementation.
JSON Schema reference: https://json-schema.org/understanding-json-schema/reference/numeric#integer
Summary by Sourcery
Align Python SDK schema validation with JSON Schema numeric type semantics.
Bug Fixes:
Enhancements:
Tests: