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 authored and rlouf committed May 31, 2024
1 parent 538f77a commit 7723ce8
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 7723ce8

Please sign in to comment.