Skip to content
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

Support boolean JSON schemas #1015

Merged
merged 7 commits into from
Sep 10, 2024
Merged

Conversation

hudson-ai
Copy link
Collaborator

We were previously supporting True/False schemas only when nested in certain places like items and additionalProperties. This expands our coverage to handle top-level boolean schemas as well.

Note that we will now raise a ValueError for False schemas, simply because there is nothing we can generate in this case. Alternatively, we return a null grammar... But this may have unforseen consequences.

Added some minimal tests, as we already have decent coverage for the nested cases in the TestEmptySchema class.

Note: developed alongside #1014, which made it much easier to tell whether I had full coverage of every type we accept :)

@codecov-commenter
Copy link

codecov-commenter commented Sep 9, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 87.50000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 61.33%. Comparing base (418fc03) to head (2ea9d50).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
guidance/library/_json.py 87.50% 2 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1015      +/-   ##
==========================================
- Coverage   70.06%   61.33%   -8.73%     
==========================================
  Files          62       62              
  Lines        4443     4449       +6     
==========================================
- Hits         3113     2729     -384     
- Misses       1330     1720     +390     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hudson-ai
Copy link
Collaborator Author

Will merge if there are no objections @Harsha-Nori & @riedgar-ms

if json_schema is True:
json_schema = {}
elif json_schema is False:
raise ValueError("No valid JSON can be generated from a schema of `False`")
Copy link
Collaborator

Choose a reason for hiding this comment

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

thoughts on raising error vs. returning empty string?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Intuitively I feel like warning + empty string is more "correct", but I'm easily convinced otherwise.

Copy link
Collaborator Author

@hudson-ai hudson-ai Sep 10, 2024

Choose a reason for hiding this comment

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

The empty string isn't valid against the False schema. I think we need to raise an exception if someone wants to generate against a schema with no valid instances. Another "bad" schema by this standard is

{
  "type": "object",
  "properties": {
    "name": false
  },
  "required": ["name"]
  }

Note that raising an exception upon encountering a false subschema isn't actually correct though. See the following example:

{
 "type": "object",
 "properties": {
   "name": false
 },
 "required": []
 }

This should validate against any object that does not have the key "name". Will not support for now -- I think I'll open an issue against this just to track it...

Edit: issue #1018

@hudson-ai hudson-ai merged commit 35591d8 into guidance-ai:main Sep 10, 2024
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants