Skip to content

fix(python-sdk): accept integral floats in integer schemas - #2019

Open
Twiink wants to merge 3 commits into
openlit:mainfrom
Twiink:fix/schema-integral-numbers-20261125
Open

Twiink wants to merge 3 commits into
openlit:mainfrom
Twiink:fix/schema-integral-numbers-20261125

Conversation

@Twiink

@Twiink Twiink commented Nov 25, 2026 •

Copy link
Copy Markdown

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

  • PR title follows Conventional Commit format.
  • I have reviewed the contributing guidelines.
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • I have performed a self-review of this change
  • Changes have been tested
  • Changes are documented

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:

  • Align Python SDK integer schema validation with JSON Schema by accepting finite numeric values without a fractional part, including decimal and exponential representations, while continuing to reject fractional and non-numeric values.

Enhancements:

  • Preserve exact JSON decimal values during schema validation and document integer numeric semantics.

Tests:

  • Add regression coverage for root and nested integer and number schemas, nullable numeric types, exponential values, and precise fractional-value rejection.

@Twiink
Twiink requested review from a team and ishanjainn as code owners November 25, 2026 09:35
@Twiink
Twiink deployed to release-notes November 25, 2026 09:35 — with GitHub Actions Active
@sourcery-ai

sourcery-ai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Implementation

Parse JSON decimals exactly and treat finite float or Decimal values without a fractional part as integers, while retaining existing primitive-type checks.

  • sdk/python/src/openlit/guard/schema.py L47-50 (+3 small edits)

Documentation

Document that integer schemas accept numeric values with no fractional part, including decimal notation.

  • sdk/python/src/openlit/guard/schema.py L96

Tests

Cover integral and fractional representations, nested pipeline validation, and compatibility for number and nullable numeric schemas.

  • sdk/python/tests/test_guard_schema.py L111-191

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

PR Summary by Qodo

fix(python-sdk): accept integral floats in integer schemas

🐞 Bug fix 🧪 Tests 🕐 10-20 Minutes

Grey Divider

AI Description

• Accept JSON numbers without a fractional part in integer schemas, including decimal and
 exponential forms.
• Preserve rejection of fractional numbers, booleans, strings, and null.
• Add root and nested pipeline regressions and document the integer semantics.
Merge Risk 🟡 Medium

Blast radius 🟢 Low 💥 1 capability

  • 1 affected end-to-end capability after fault-specific containment; implementation boundaries do not widen its functional scope.
  • The changed parser and integer matcher determine whether a Schema guard accepts structured output, and the integration sends that result through the postflight pipeline before returning supported provider responses.
  • These are one bounded guard-integration behavior rather than separate parser, pipeline, and provider capabilities.
Behavioral complexity 🟡 Medium 🧩 Compatibility cases

  • Before, json.loads produced ordinary floats and integer schemas matched only int values.
  • After, parse_float=Decimal preserves decimal values and integer matching accepts finite values with no fractional part across nested schemas and type unions.
  • The new correctness condition is exact numeric-integrality testing without allowing booleans or tiny fractional values; this is a compatibility rule across numeric spellings, not a new algorithm or coordination protocol.
Potential damage 🟡 Medium 🩹 Incorrect results

  • If this capability regresses, a fractional value could be accepted as an integer or an integral value could be rejected.
  • Acceptance errors may let malformed structured output reach the caller; rejection errors deny otherwise usable responses.
  • The supported source shows no persistent storage or external side effect created by this guard.
  • A code revert restores the validation behavior for future evaluations.
  • Already returned responses must be retried or corrected by callers, but no stored state repair or external compensation is indicated.
Assessed commit e1a4c33
Diagram

graph TD
  P["Postflight pipeline"] --> S["Schema guard"] --> J["JSON parse"] --> V["Recursive validation"] --> M{"Schema matches?"}
  M -- "yes" --> A["Allow output"]
  M -- "no" --> N["Schema mismatch"]
Loading
High-Level Assessment

Keep the change in the existing recursive type check: it applies consistently at root and nested paths without converting parsed values. Normalizing floats before validation would change the data unnecessarily.

Files changed (2) +38 / -0

Bug fix (1) +3 / -0
schema.pyRecognize integral floats in integer schema checks +3/-0

Recognize integral floats in integer schema checks

• Integer schemas now accept Python floats with no fractional part, which covers JSON values such as 1.0 and 1e1. The Schema docstring records this behavior; the existing boolean exclusion remains in place.

sdk/python/src/openlit/guard/schema.py

Tests (1) +35 / -0
test_guard_schema.pyCover integer representations at root and nested paths +35/-0

Cover integer representations at root and nested paths

• Adds acceptance tests for integral decimal and exponential values and rejection tests for fractional and nonnumeric JSON values. Pipeline tests verify integer properties and array items in postflight output.

sdk/python/tests/test_guard_schema.py

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Fractional JSON numbers pass integer checks ✓ Resolved
Description
Schema.evaluate parses numbers as floats before the new is_integer() check, so a fractional
value such as 1e-9999 underflows to 0.0 and passes an integer schema. The guard then allows the
original JSON text through the pipeline, rather than replacing it with the rounded value it
validated.

Grey Divider

Tip of the day
💡 Did you know, you can switch off images and animations for a plain-text comment

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@sourcery-ai

sourcery-ai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Linked issues

Checked at commit e1a4c33

#2017 Bug: Schema guard rejects integral JSON numbers written with a decimal point · 2 of 2 met

# Requirement Status Notes
1 Accept finite numeric values with no fractional part as JSON Schema integers, including decimal and exponential representations. ✅ Met The validator preserves decimal values with Decimal and accepts only finite Decimal values equal to their integral value; nested object and array cases are also covered.
2 Reject fractional values, booleans, and strings as JSON Schema integers. ✅ Met The integer check rejects fractional Decimal values, and booleans are explicitly excluded from numeric types; regression tests cover these cases.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread sdk/python/src/openlit/guard/schema.py Resolved
Comment thread sdk/python/tests/test_guard_schema.py Resolved
sourcery-ai[bot]
sourcery-ai Bot previously approved these changes Nov 26, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sourcery assessment

Approved.

This branch was successfully deployed

1 active (outdated) deployment
release-notes — 4c22d260 Deployed Nov 25, 2026 by Twiink via Normalize / Conventional title #908
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Schema guard rejects integral JSON numbers written with a decimal point

1 participant