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

Bug in the treatment of the anyOf keyword in JSON Schema #497

Closed
RobinPicard opened this issue Jan 2, 2024 · 0 comments · Fixed by #552
Closed

Bug in the treatment of the anyOf keyword in JSON Schema #497

RobinPicard opened this issue Jan 2, 2024 · 0 comments · Fixed by #552
Labels

Comments

@RobinPicard
Copy link
Contributor

Describe the issue as clearly as possible:

I ran into this issue while working on #495. It seems to be caused by the fact that the subregex generated for the anyOf keyword includes all permutations of the patterns of the types in question concatenated. I don't understand why we need to do such permutations. I have the impression that a simple "or" between the patterns associated to each type would work

Steps/code to reproduce the bug:

import json
import jsonschema
import re
from outlines.fsm.json_schema import build_regex_from_object

schema = {
    "properties": {
        "foo": {"anyOf": [{"type": "null"}, {"type": "boolean"}]},
    },
    "required": ["foo"],
    "type": "object",
}

regex = build_regex_from_object(json.dumps(schema))
print(regex)

wrong_response = '{ "foo" : nulltrue }'

if re.fullmatch(regex, wrong_response):
    print("match")

try:
    jsonschema.validate(instance=wrong_response, schema=schema)
except jsonschema.exceptions.ValidationError as err:
    print("no match")
    
# \{[\n ]*"foo"[\n ]*:[\n ]*((null)|((true|false))|(null(true|false))|((true|false)null))[\n ]*\}
# match
# no match

Expected result:

The wrong_response should not match with the regex

Error message:

No response

Outlines/Python version information:

Outlines: current main branch
Python: 3.9.6

Context for the issue:

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants