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 23, 2024
1 parent 6f655ca commit c7955b4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
19 changes: 17 additions & 2 deletions 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"}, # issue #913
{"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 Expand Up @@ -324,7 +339,7 @@ def to_regex(
{"type": "number"},
{"type": "boolean"},
{"type": "null"}
# { "type": "array" }, # TODO: enable arrays within object-types
# { "type": "array" }, # issue #913
]

# We set the object depth to 2 to keep the expression finite, but the "depth"
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 @@ -723,6 +723,20 @@ def test_format(schema, regex, examples):
("1234", False), # not an object
],
),
# No schema / unconstrained value
(
{},
[
('"aaabbuecuh"', True), # string
("5.554", True), # number
("true", True), # boolean
("null", True), # null
("5999", True), # integer
# ('["a", "b"]', True), # array # issue #913
('{"key": {"k2": "value"}}', True), # nested object
("this isnt valid json", False),
],
),
],
)
def test_format_without_regex(schema, examples):
Expand Down

0 comments on commit c7955b4

Please sign in to comment.