Skip to content

Commit

Permalink
Fixing Enum with only one element being ignored (#721)
Browse files Browse the repository at this point in the history
Fixes #720
  • Loading branch information
isamu-isozaki authored Mar 2, 2024
1 parent 42f465c commit 88dc97c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions outlines/fsm/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ def to_regex(

return f"({'|'.join(choices)})"

elif "const" in instance:
const = instance["const"]
if type(const) in [int, float, bool, None]:
const = re.escape(str(const))
elif type(const) == str:
const = f'"{re.escape(const)}"'
return const

elif "$ref" in instance:
path = f"{instance['$ref']}"
instance = resolver.lookup(path).contents
Expand Down
18 changes: 18 additions & 0 deletions tests/fsm/test_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,24 @@ def test_match_number(pattern, does_match):
("0", False),
],
),
# Const string
(
{"title": "Foo", "const": "Marc", "type": "string"},
'"Marc"',
[('"Marc"', True), ('"Jean"', False), ('"John"', False)],
),
# Make sure strings are escaped
(
{"title": "Foo", "const": ".*", "type": "string"},
r'"\.\*"',
[('".*"', True), (r'"\s*"', False), (r'"\.\*"', False)],
),
# Const integer
(
{"title": "Foo", "const": 0, "type": "integer"},
"0",
[("0", True), ("1", False), ("a", False)],
),
# Enum string
(
{"title": "Foo", "enum": ["Marc", "Jean"], "type": "string"},
Expand Down

0 comments on commit 88dc97c

Please sign in to comment.