Skip to content

CitationMixin crashes with regex.error when a quote contains regex metacharacters #2431

Description

@RohitMudili
  • This is actually a bug report.
  • I am not getting good LLM Results
  • I have tried asking for help in the community on discord or discussions and have not received a response.
  • I have tried searching the documentation and have not found an answer.

What Model are you using?

  • Other (provider-agnostic — the bug is in CitationMixin validation, independent of the model)

Describe the bug

CitationMixin._get_span (instructor/v2/dsl/citation.py) interpolates the LLM-generated quote directly into a regex fuzzy-match pattern without escaping it:

minor = quote
s = regex.search(f"({minor}){{e<={errs_}}}", major)

Because quote comes straight from the model's substring_quotes, any regex metacharacter it contains — unbalanced parentheses/brackets, *, +, ?, \, etc., all common in ordinary prose — is treated as part of the pattern. This raises regex.error (crashing validation) or silently matches the wrong span, instead of the documented behavior of finding the span or dropping the quote.

To Reproduce

from instructor import CitationMixin


class Answer(CitationMixin):
    pass


context = "The margin is 50% (approx) this quarter."

# A quote with an unbalanced parenthesis, as an LLM might return:
Answer.model_validate(
    {"substring_quotes": ["50% (approx"]},
    context={"context": context},
)

Result:

regex.error: missing ) at position 19

Expected behavior

The span should be resolved (the quote normalized back to the matching substring of the context), or, if it does not match, the quote should be dropped from substring_quotes — never a regex.error. Fuzzy (edit-distance) matching should keep working.

The fix is to escape the quote with regex.escape() before building the pattern. Proposed in #2430.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions