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

Strip anchors from JSON regexes #1027

Merged
merged 1 commit into from
Sep 16, 2024
Merged
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
6 changes: 6 additions & 0 deletions guidance/library/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ def _gen_json_string(
if format is not None:
regex = _get_format_pattern(format)

elif regex is not None:
# Sanitize the regex, removing unnecessary anchors that may cause problems later
# NOTE/TODO: this could potentially be pushed further down into the lexeme function,
# but it's not immediately clear whether anchors in other contexts are superfluous.
regex = regex.lstrip("^").rstrip("$")

elif regex is None:
range_expr = f"{{{min_length},{max_length}}}" if max_length is not None else f"{{{min_length},}}"
regex = f"(?s:.{range_expr})"
Expand Down
Loading