Skip to content

fix(python-sdk): reject non-JSON constants in schema guard - #1866

Open
WitBlue6 wants to merge 2 commits into
openlit:mainfrom
WitBlue6:fix/schema-json-constants
Open

WitBlue6 wants to merge 2 commits into
openlit:mainfrom
WitBlue6:fix/schema-json-constants

Conversation

@WitBlue6

@WitBlue6 WitBlue6 commented Oct 28, 2026 •

Copy link
Copy Markdown

Issue number: Closes #1865

Change description:

Reject unquoted NaN, Infinity, and -Infinity in the Python SDK's Schema guard via json.loads(parse_constant=...). Python accepts these extensions by default, so a postflight JSON guard currently allows output that a strict downstream JSON consumer rejects. The existing invalid-JSON result retains the configured deny/warn action. Quoted strings and object keys containing those spellings remain valid.

Found by code inspection and reproduced offline on main at 1ed8bb7; no production-incident claim. Related #1696 addresses bool-versus-number schema type checking, not JSON parsing; this change does not modify that validation function.

Validation on Python 3.13.7:

  • Before: new regression tests 12 failed, 6 passed.
  • After: python -m pytest tests/test_guard_schema_json.py tests/test_guard_pipeline.py tests/test_guard_integration.py -q: 49 passed.
  • Pylint on both changed Python files: 10.00/10, exit 0.
  • poetry check: exit 0, with existing metadata deprecation warnings.
  • poetry build: built the wheel and source distribution.
  • Full SDK Pylint was also run: its only errors were E1102 in the untouched live-provider tests test_groq.py:21 and test_together.py:19 in this installed-provider environment. No live-provider tests were run, and no live API keys or network calls were needed for the guard regressions.

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 (Schema docstring).

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.

AI assistance: Codex helped inspect the bug, implement the change and tests, and draft the issue and PR text. The diff and reported local results were reviewed before submission.

Summary by Sourcery

Enforce strict JSON parsing in the Python SDK Schema guard.

Bug Fixes:

  • Reject unquoted NaN, Infinity, and -Infinity in the Python SDK Schema guard as invalid JSON while preserving configured deny or warn behavior.

Documentation:

  • Document that the Schema guard rejects non-standard unquoted JSON numeric constants while allowing quoted values and keys.

Tests:

  • Add regression coverage for rejecting non-JSON constants at the Schema guard and pipeline boundaries, and for preserving valid JSON behavior.

@WitBlue6
WitBlue6 requested review from a team and ishanjainn as code owners October 28, 2026 11:05
@sourcery-ai

sourcery-ai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Implementation

Reject Python’s non-standard JSON constants during Schema parsing while preserving the existing invalid-JSON handling and documenting the behavior.

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

Tests

Cover rejection of unquoted non-JSON constants and acceptance of valid JSON values, quoted spellings, and configured warning actions.

  • sdk/python/tests/test_guard_schema_json.py L1-40

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

@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 reviewed your changes and they look great!

Sourcery assessment

Approved.


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

@chrikrah chrikrah 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.

@WitBlue6, nothing here blocks a merge from my side. With schema.py back at 1ed8bb7 and your tests kept, 12 of the 18 fail, so they pin the change.

$ cd <ol1866 tree>/sdk/python && python -I run.py $PWD/src tests/test_guard_schema_json.py -q -p no:cacheprovider   # head 0f29182, git archive export, openlit.guard.schema.__file__ printed from the tree, Python 3.12
18 passed in 0.02s
$ git show 1ed8bb7:sdk/python/src/openlit/guard/schema.py > src/openlit/guard/schema.py && python -I run.py $PWD/src tests/test_guard_schema_json.py -q -p no:cacheprovider
12 failed, 6 passed in 0.06s

$ python -I probe.py   # head again, Schema(json_mode=True).evaluate(text)
'1e999' GuardAction.ALLOW
'{"score": 1e999}' GuardAction.ALLOW
json.dumps(json.loads("1e999")) -> Infinity
# not run: the full SDK suite

non-blocking: 1e999 is valid JSON but parses to inf, so a consumer that runs the output through json.dumps gets Infinity back. Rejecting it would deny RFC-valid text, which is a range policy and not the syntax fix #1865 asks for, so it belongs outside this PR. Should the guard promise finite numbers at all?

@sourcery-ai

sourcery-ai Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Linked issues

Checked at commit 24628dc

#1865 Bug: Schema JSON guard accepts non-JSON NaN and Infinity constants · 5 of 5 met

# Requirement Status Notes
1 Reject unquoted NaN, Infinity, and -Infinity in JSON mode, including inside objects and arrays. ✅ Met Schema.evaluate passes json.loads a parse_constant callback that rejects these values; regression tests cover root, object, and array cases.
2 Reject the constants in schema-number mode as well. ✅ Met The callback is used for all Schema.evaluate parsing, including when a schema is provided.
3 Return invalid_json with the configured deny or warn action. ✅ Met The parse-error result uses self._action and classification invalid_json; tests cover pipeline deny and configured warn.
4 Allow quoted strings and object keys containing the reserved spellings. ✅ Met Regression tests cover quoted values and keys, which remain valid JSON strings.
5 Keep the change local to the Schema guard without changing unrelated JSON consumers or adding a dependency. ✅ Met The change adds a parse_constant callback in Schema and uses the existing json module; no dependency or unrelated consumer is changed.

This branch was previously deployed

1 inactive (outdated) deployment
release-notes — 0f291823 Deployed Oct 28, 2026 by WitBlue6 via Normalize / Conventional title #697
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 JSON guard accepts non-JSON NaN and Infinity constants

3 participants