Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for URI format in JSON schema validation #1165

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions outlines/fsm/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@
DATE = r'"(?:\d{4})-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2][0-9]|3[0-1])"'
TIME = r'"(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\\.[0-9]+)?(Z)?"'
UUID = r'"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"'
# URI only supports a subset of https://datatracker.ietf.org/doc/html/rfc3986, specifically https:// URLs with optional auth details
URI = r'"(https?:\/\/)?([-a-zA-Z0-9:%._\+~#=]+@)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}([-a-zA-Z0-9@:%_\+.~#?&//=]*)"'

format_to_regex = {
"uuid": UUID,
"date-time": DATE_TIME,
"date": DATE,
"time": TIME,
"uri": URI,
}


Expand Down Expand Up @@ -350,14 +353,8 @@ def to_regex(
return rf'("{pattern}")'
elif "format" in instance:
format = instance["format"]
if format == "date-time":
return format_to_regex["date-time"]
elif format == "uuid":
return format_to_regex["uuid"]
elif format == "date":
return format_to_regex["date"]
elif format == "time":
return format_to_regex["time"]
if format in format_to_regex:
return format_to_regex[format]
else:
raise NotImplementedError(
f"Format {format} is not supported by Outlines"
Expand Down
37 changes: 37 additions & 0 deletions tests/fsm/test_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
STRING_INNER,
TIME,
UUID,
URI,
WHITESPACE,
build_regex_from_schema,
get_schema_from_signature,
Expand Down Expand Up @@ -825,6 +826,42 @@ def test_match(schema, regex, examples):
('"15:30:00+01:00"', False), # incorrect separator
],
),
# URI
(
{"title": "Foo", "type": "string", "format": "uri"},
URI,
[
('"https://www.example.com"', True),
('"http://example.com"', True),
('"https://subdomain.example.co.uk/path?query=value#fragment"', True),
('"https://example.com:8080"', True), # With port
('"http://123.45.67.89"', True), # IP address
('"https://example.com/path/to/resource.html"', True), # With file extension
('"https://user:pass@example.com"', True), # With basic auth
('"https://example.com/?q=test&r=123"', True), # With multiple query parameters
('"https://example.co.uk"', True), # Different TLD
('"https://xn--bcher-kva.example"', True), # Punycode domain
('"https://example.com/path%20with%20spaces"', True), # Encoded spaces
('"ftp://example.com"', False), # FTP protocol
('"not a uri"', False),
('"https://"', False), # Incomplete URI
('""', False), # Empty string
('https://www.example.com', False), # Missing quotes
('"http:/example.com"', False), # Missing slash after protocol
('"https://example.com:abc"', False), # Invalid port
('"https://exa mple.com"', False), # Space in domain
('"https://.example.com"', False), # Domain starting with dot
('"https://example..com"', False), # Consecutive dots in domain
('"https://exam ple.com/path"', False), # Space in domain (but valid path)
('"https://example.com/path "', False), # Space at end of path
('"https://example.com#frag ment"', False), # Space in fragment
('"https://example.com/?q=va lue"', False), # Space in query
('"https://exa\nmple.com"', False), # Newline in domain
('"https://example.com/pat\nh"', False), # Newline in path
('"https://example.com#frag\nment"', False), # Newline in fragment
('"https://example.com/?q=val\nue"', False), # Newline in query
]
),
],
)
def test_format(schema, regex, examples):
Expand Down
Loading