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

Allow json schema of {}, resulting in unconstrained json value #914

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion outlines/fsm/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,22 @@ def to_regex(
if whitespace_pattern is None:
whitespace_pattern = WHITESPACE

if "properties" in instance:
if instance == {}:
# JSON Schema Spec: Empty object means unconstrained, any json type is legal
types = [
{"type": "boolean"},
{"type": "null"},
{"type": "number"},
{"type": "integer"},
{"type": "string"},
{"type": "array"},
{"type": "object"},
]
regexes = [to_regex(resolver, t, whitespace_pattern) for t in types]
regexes = [rf"({r})" for r in regexes]
return rf"{'|'.join(regexes)}"

elif "properties" in instance:
regex = ""
regex += r"\{"
properties = instance["properties"]
Expand Down
14 changes: 14 additions & 0 deletions tests/fsm/test_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,20 @@ def test_format(schema, regex, examples):
('{"a": "a"}', False), # not an array
],
),
# No schema / unconstrained value
(
{},
[
('"aaabbuecuh"', True), # string
("5.554", True), # number
("true", True), # boolean
("null", True), # null
("5999", True), # integer
('["a", "b"]', True), # array
('{"key": {"k2": "value"}}', True), # nested object
("this isnt valid json", False),
],
),
],
)
def test_format_without_regex(schema, examples):
Expand Down
Loading