Add hex_color and slug custom types#1934
Merged
Merged
Conversation
Add two regex-based custom types to outlines.types: - hex_color matches CSS hexadecimal color notation in the three-digit (#rgb) and six-digit (#rrggbb) forms. The four- and eight-digit alpha forms are intentionally excluded to keep the canonical opaque syntax. - slug matches lowercase-hyphenated URL slugs: ASCII lowercase alphanumeric groups separated by single hyphens, rejecting uppercase, underscores, empty strings, and leading, trailing, or doubled hyphens. Add accept and reject cases for both types to the parametrized regex test, following the pattern established for semver and mac_address. Refs dottxt-ai#1303 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
📚 Documentation preview: https://dottxt-ai.github.io/outlines/pr-preview/pr-1934/ Preview updates automatically with each commit. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Partially addresses #1303 by adding two more regex-based custom types, following the structure of #1863 (semver, mac_address):
hex_color: CSS hexadecimal color notation,#rgband#rrggbbslug: lowercase-hyphenated URL slugs, e.g.my-post-titleOf the types listed in #1303,
semverandmac_address(#1863) andipv6(#1867) are already merged, andipv4/uuid4/hex_strpredate the issue — this PR adds only the two missing ones above.Design notes
hex_color = Regex(r"#(?:[0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})")accepts only the three- and six-digit forms. The four- and eight-digit alpha forms from CSS Color Module Level 4 are intentionally excluded (and covered by reject tests), consistent with how the existing types stick to the single canonical textual form (e.g.mac_addressaccepts only the colon-separated form).slug = Regex(r"[a-z0-9]+(?:-[a-z0-9]+)*")structurally rejects uppercase, underscores, the empty string, and leading/trailing/consecutive hyphens, matching the output of common slugify helpers.Term.validate/Term.matchesand the structured-generation backends.Beyond the unit tests, I brute-force verified both patterns: hex_color against all payload lengths 0–9 and all printable ASCII characters in each digit position, and slug exhaustively against all strings of length ≤ 4 over the alphabet
{a, 1, -, _, A}compared to a reference predicate.Tests
python -m pytest tests/types/test_custom_types.py— 140 passed (18 new parametrized cases)This PR was prepared with AI assistance (Claude Code); all changes and test results were verified locally.