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 Email type #870

Merged
merged 5 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions docs/reference/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Outlines provides custom Pydantic types so you can focus on your use case rather
| | numeric code | `outlines.types.countries.Numeric` | Valid [country numeric codes][wiki-country-numeric] |
| | name | `outlines.types.countries.Name` | Valid country names |
| | flag | `outlines.types.countries.Flag` | Valid flag emojis |
| | email | `outlines.types.Email` | Valid email address |

Some types require localization. We currently only support US types, but please don't hesitate to create localized versions of the different types and open a Pull Request. Localized types are specified using `types.locale` in the following way:

Expand Down
1 change: 1 addition & 0 deletions outlines/types/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import airports, countries
from .email import Email
from .isbn import ISBN
from .locales import locale
11 changes: 11 additions & 0 deletions outlines/types/email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""Email Address types."""
from pydantic import WithJsonSchema
from typing_extensions import Annotated

# Taken from StackOverflow
# https://stackoverflow.com/a/201378/14773537
EMAIL_REGEX = r"""(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"""
Email = Annotated[
str,
WithJsonSchema({"type": "string", "pattern": EMAIL_REGEX}),
]
6 changes: 6 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
(types.ISBN, "9780596520687", True),
(types.ISBN, "ISBN-10: 0-596-52068-9", True),
(types.ISBN, "0-596-52068-9", True),
(types.Email, "eitan@gmail.com", True),
(types.Email, "99@yahoo.com", True),
(types.Email, "eitan@.gmail.com", False),
(types.Email, "myemail", False),
(types.Email, "eitan@gmail", False),
(types.Email, "eitan@my.custom.domain", True),
],
)
def test_type_regex(custom_type, test_string, should_match):
Expand Down
Loading