Skip to content
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion instructor/v2/dsl/citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def _get_span(

errs_ = 0
s = regex.search(f"({minor}){{e<={errs_}}}", major)
while s is None and errs_ <= errs:
while s is None and errs_ < errs:
errs_ += 1
s = regex.search(f"({minor}){{e<={errs_}}}", major)

Expand Down
26 changes: 26 additions & 0 deletions tests/dsl/test_citation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Tests for CitationMixin fuzzy span matching tolerance."""

from instructor.v2.dsl.citation import CitationMixin


class _Answer(CitationMixin):
pass


def test_quote_within_error_tolerance_matches():
# 3 substitutions from the 10-char source (<= errs default of 5): should match.
context = "0123456789"
result = _Answer.model_validate(
{"substring_quotes": ["0123456ZZZ"]}, context={"context": context}
)
assert result.substring_quotes == ["0123456789"]


def test_quote_beyond_error_tolerance_is_dropped():
# 6 substitutions from the 10-char source (> errs default of 5): should be
# dropped as "not found", not accepted and rewritten to an unrelated span.
context = "0123456789"
result = _Answer.model_validate(
{"substring_quotes": ["0123ZZZZZZ"]}, context={"context": context}
)
assert result.substring_quotes == []