diff --git a/outlines/fsm/json_schema.py b/outlines/fsm/json_schema.py index e331d2ff7..639ae32f9 100644 --- a/outlines/fsm/json_schema.py +++ b/outlines/fsm/json_schema.py @@ -10,8 +10,10 @@ from referencing._core import Resolver from referencing.jsonschema import DRAFT202012 -STRING_INNER = r'([^"\\\x00-\x1f\x7f-\x9f]|\\\\)' +# allow `\"`, `\\`, or any character which isn't a control sequence +STRING_INNER = r'([^"\\\x00-\x1F\x7F-\x9F]|\\["\\])' STRING = f'"{STRING_INNER}*"' + INTEGER = r"(-)?(0|[1-9][0-9]*)" NUMBER = rf"({INTEGER})(\.[0-9]+)?([eE][+-][0-9]+)?" BOOLEAN = r"(true|false)" diff --git a/tests/fsm/test_json_schema.py b/tests/fsm/test_json_schema.py index ef3071f8a..0669f1a84 100644 --- a/tests/fsm/test_json_schema.py +++ b/tests/fsm/test_json_schema.py @@ -124,6 +124,10 @@ def test_match_number(pattern, does_match): ('"quoted_string"', True), (r'"escape_\character"', False), (r'"double_\\escape"', True), + (r'"\n"', False), + (r'"\\n"', True), + (r'"unescaped " quote"', False), + (r'"escaped \" quote"', True), ], ), # String with maximum length