CitationMixin accepts any quote of five characters or fewer as a match
- [x] This is actually a bug report.
- [ ] I am not getting good LLM Results
- Validation rewrites the quote after the model returns.
- [ ] I have tried asking for help in the community on discord or discussions and have not received a response.
- Not asked; it reproduces in code with no model call.
- [ ] I have tried searching the documentation and have not found an answer.
- The tolerance lives in
instructor/v2/dsl/citation.py:91.
- The tolerance lives in
What Model are you using?
- [ ] gpt-3.5-turbo
- [ ] gpt-4-turbo
- [ ] gpt-4
- [x] Other (please specify)
- None.
CitationMixinvalidation alone reproduces it, with no model call.
- None.
Describe the bug
CitationMixin._get_span allows errs=5 edits whatever the quote length, so a quote of five characters or fewer always matches some span of a non-empty source. A made-up "Paris" against a source about Berlin passes as the citation "capit". Not #2769: that one and #2770 cover a quote deleted to an empty match; here the match is a non-empty, unrelated span.
To Reproduce
Measured on main 035c77b (1.18.0) and on the #2770 head 93338e1, regex 2.5.159, pydantic 2.11.7, Python 3.12.3, same output on both:
from instructor.v2.dsl.citation import CitationMixin as M
cases = [
("dog", "The cat sleeps."),
("zebra", "The cat sleeps."),
("1999", "Revenue grew 12% in 2021."),
("Paris", "The capital of Germany is Berlin."),
("London!", "The capital of Germany is Berlin."),
]
for q, c in cases:
print(repr(q), "->", M.model_validate({"substring_quotes": [q]}, context={"context": c}).substring_quotes)
'dog' -> ['The']
'zebra' -> ['he ca']
'1999' -> ['12% ']
'Paris' -> ['capit']
'London!' -> []
Expected behavior
All five print []. Capping the budget at a third of the quote does that and keeps the three-edit match test_quote_within_error_tolerance_matches pins, where a fifth fails it. The cost: quotes under 15 characters lose tolerance, so 'Revnu gr 12' stops citing 'Revenue grew 12'. #2495 tightened this budget once already (#2463's off-by-one), and the 5-edit ceiling stays for longer quotes.
# instructor/v2/dsl/citation.py, first line of _get_span's search
errs = min(errs, len(quote) // 3)
$ python -m pytest -q tests/v2/test_citation.py tests/coverage/test_dsl_small_coverage.py tests/dsl
173 passed, 2 skipped # main 035c77b with the line above
# not run: the full suite
Screenshots
Not applicable. The output above is the evidence.
@jxnl, would you take a third of the quote as the budget? I'd send the four probes as tests in tests/v2/test_citation.py as a PR this week, so you can fold them into yours.
All five short-quote examples in this report return an empty result on main. The regression suite also covers valid matches and the fuzzy edit limit. Shipped in https://github.com/567-labs/instructor/pull/2884.