Skip to content

Commit

Permalink
allow json schema of {}, resulting in unconstrained json value
Browse files Browse the repository at this point in the history
  • Loading branch information
lapp0 committed May 30, 2024
1 parent 538f77a commit df918a2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
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

0 comments on commit df918a2

Please sign in to comment.