Repository navigation
fix(python-sdk): accept a list of types in the schema guard - #1998
Conversation
Reviewer's GuideImplementationUpdate schema validation to accept lists of JSON Schema types, preserve numeric boolean semantics, and validate nested objects and arrays when their allowed types include those structures.
TestsAdd coverage for nullable and union types, unknown type names, nested validation, and pipeline denial of mismatched values.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
PR Summary by QodoFix schema guard validation of lists of JSON types
AI Description
Merge Risk
|
Code Review by Qodo
1.
|
Linked issuesChecked at commit 7bc3e2b #1997 Bug: Schema guard crashes on a list of types, so the default pipeline allows every output · 3 of 3 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="40" />
<code_context>
+ def matches(name: Any) -> bool:
+ expected = type_map.get(name) if isinstance(name, str) else None
+ if expected is None:
+ return True
+ # In Python, bool is a subclass of int, but JSON Schema treats booleans
+ # as a separate primitive type from numbers.
</code_context>
<issue_to_address>
**Invalid schema types allow output**
When a schema type list contains an unsupported, misspelled, or non-string member, `matches` returns `True` for that member, so `any(matches(name) for name in types)` accepts values that match none of the recognized types and the schema guard allows invalid output.
Make `matches` reject unsupported or non-string type members instead of treating them as matches.
</issue_to_address>
### Comment 2
<location path="sdk/python/src/openlit/guard/schema.py" line_range="45" />
<code_context>
+ # as a separate primitive type from numbers.
+ if name in ("integer", "number") and isinstance(data, bool):
+ return False
+ return isinstance(data, expected)
+
+ if not any(matches(name) for name in types):
</code_context>
<issue_to_address>
**Integral JSON numbers are denied**
When a value such as `1.0` is checked against a type list containing `integer`, `matches` uses `isinstance(data, int)` for `integer`, but `json.loads` parses `1.0` as a float, so the schema guard denies valid output.
Treat JSON numbers with an integral value as integers while continuing to exclude booleans.
</issue_to_address>Sourcery assessment
Approval pending. 2 findings to address first.
Blocking findings: sdk/python/src/openlit/guard/schema.py:40, sdk/python/src/openlit/guard/schema.py:45
|
Qodo's "Malformed unions let invalid outputs through" finding is fixed in 7bc3e2b as well: unknown or non-string names in a type list are now ignored instead of treated as a match, so |
|
Hi @ishanjainn, gentle ping on this one when you have time. Both bot findings are addressed and the threads are resolved. Happy to change anything. |
Merged PR summary
Generated from the merged PR description and bounded, component-scoped file evidence. Release preparation reuses this validated summary. |
Issue number: Fixes #1997
Change description:
Schemacrashed withTypeError: unhashable type: 'list'when a schema used a list of types such as["string", "null"], the usual way to write a nullable field. With the defaultfail_open=Truethe pipeline turned that crash intoallow, so invalid outputs passed._validate_json_schemanow treatstypeas a list of names (a single name becomes a one-item list):integerornumberrequired,propertiesanditemsare still checked when the list containsobjectorarrayExpected string or null at .nickname, got intSingle-type schemas behave exactly as before.
Tests
New cases in
tests/test_guard_schema.py: list types acceptnulland a string, deny42,trueand[], keep rejecting booleans for["integer", "null"], check nested properties and array items, and aPipelinewith the defaultfail_opennow denies instead of allowing.TypeError); with the fix,tests/test_guard_schema.pyandtests/test_guard_pipeline.pypass (34 passed).jsonschemaon 4000 random schemas usingtype,required,propertiesanditems, including lists of types: no disagreements (main raises on them).pylinton the changed files: 10.00/10.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.
Summary by Sourcery
Update the Python schema guard to correctly validate fields declared with multiple JSON Schema types.
Bug Fixes:
Enhancements:
Tests: