Skip to content

Commit

Permalink
Enable Tuples / prefixItems in build_regex_from_schema()
Browse files Browse the repository at this point in the history
  • Loading branch information
lapp0 committed May 22, 2024
1 parent 6f655ca commit fee10ba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions outlines/fsm/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ def to_regex(

return rf"({'|'.join(xor_patterns)})"

# Create pattern for Tuples, per JSON Schema spec, `prefixItems` determines types at each idx
elif "prefixItems" in instance:
element_patterns = [
to_regex(resolver, t, whitespace_pattern) for t in instance["prefixItems"]
]
comma_split_pattern = rf"{whitespace_pattern},{whitespace_pattern}"
tuple_inner = comma_split_pattern.join(element_patterns)
return rf"\[{whitespace_pattern}{tuple_inner}{whitespace_pattern}\]"

# The enum keyword is used to restrict a value to a fixed set of values. It
# must be an array with at least one element, where each element is unique.
elif "enum" in instance:
Expand Down
9 changes: 9 additions & 0 deletions tests/fsm/test_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,15 @@ def test_match_number(pattern, does_match):
rf"({STRING}{INTEGER})",
[('"a"1', True), ('"a"', False), ('"1"', False)],
),
# Tuple / prefixItems
(
{
"title": "Foo",
"prefixItems": [{"type": "string"}, {"type": "integer"}],
},
rf"\[{WHITESPACE}{STRING}{WHITESPACE},{WHITESPACE}{INTEGER}{WHITESPACE}\]",
[('["a", 1]', True), ('["a", 1, 1]', False), ("[]", False)],
),
# Nested schema
(
{
Expand Down

0 comments on commit fee10ba

Please sign in to comment.