Repository navigation
fix: honor Schema guard json_mode flag #2001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -84,7 +84,7 @@ class Schema(Guard): | |
| ---------- | ||
| action : str | ||
| ``"deny"`` (default) or ``"warn"``. | ||
| json : bool | ||
| json_mode : bool | ||
| If ``True``, just verify the output is parseable JSON. | ||
| schema : dict, optional | ||
| A JSON-schema-like dict to validate against. | ||
|
|
@@ -118,6 +118,9 @@ def evaluate(self, text: str) -> GuardResult: | |
| explanation=f"Output is not valid JSON: {exc}", | ||
| ) | ||
|
|
||
| if self._json_mode: | ||
| return GuardResult(guard_name=self.name) | ||
|
Comment on lines
+121
to
+122
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · String false bypasses schema checks When Validate that Prompt for AI agentsIn `sdk/python/src/openlit/guard/schema.py` at lines 109-110:
**String false bypasses schema checks**
When `json_mode` comes from an unparsed configuration value such as the environment string `"false"`, `self._json_mode` is truthy for the string `"false"`, so `evaluate()` returns ALLOW without checking the schema and accepts schema-mismatched output.
Validate that `json_mode` is a boolean at construction, or explicitly parse supported string values before storing it.
Comment on lines
+121
to
+122
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Invalid JSON passes the guard When JSON-only mode receives Reject non-standard constants during JSON parsing before returning ALLOW. Prompt for AI agentsIn `sdk/python/src/openlit/guard/schema.py` at lines 109-110:
**Invalid JSON passes the guard**
When JSON-only mode receives `NaN`, `Infinity`, or `-Infinity`, `json.loads` accepts these non-standard constants, so `evaluate()` reaches the JSON-mode ALLOW return and callers see invalid JSON pass the guard.
Reject non-standard constants during JSON parsing before returning ALLOW. |
||
|
|
||
| if self._schema: | ||
| err = _validate_json_schema(parsed, self._schema) | ||
| if err: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.