Skip to content

Add hex_color and slug custom types#1934

Merged
RobinPicard merged 1 commit into
dottxt-ai:mainfrom
pepepeboom:add-hex-color-slug-types
Jul 20, 2026
Merged

Add hex_color and slug custom types#1934
RobinPicard merged 1 commit into
dottxt-ai:mainfrom
pepepeboom:add-hex-color-slug-types

Conversation

@pepepeboom

Copy link
Copy Markdown
Contributor

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, #rgb and #rrggbb
  • slug: lowercase-hyphenated URL slugs, e.g. my-post-title

Of the types listed in #1303, semver and mac_address (#1863) and ipv6 (#1867) are already merged, and ipv4/uuid4/hex_str predate 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_address accepts 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.
  • Both patterns are unanchored like every other type in the file; full-match semantics are applied by Term.validate/Term.matches and 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.

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>

@RobinPicard RobinPicard left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, thanks!

@github-actions

Copy link
Copy Markdown

📚 Documentation preview: https://dottxt-ai.github.io/outlines/pr-preview/pr-1934/

Preview updates automatically with each commit.

@RobinPicard
RobinPicard merged commit 3292a96 into dottxt-ai:main Jul 20, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants