Three cheap checks for OCR output stuck in a loop
Working notes, tested on the data below and not much further. What I haven’t tested is at the end.
Here is OvisOCR2 on a blank page from an 1881 Russian entomology journal:
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
...
It writes that sentence 1,490 times, until it hits the output token limit. (Three other models write the same sentence once on a blank page and stop.) On the November 2026 BHL OCR leaderboard (26 models; live board), 19 of the 26 models get stuck like this on at least one of 2,165 pages, and the worst on more than 6% of them.

zlib tail ratio 0.116).If you have the API response, finish_reason: "length" tells you the model hit its output cap. These checks are for when you only have the saved text: a vendor pipeline, a script that kept only the output, or someone else’s dataset.
2. If you know the limit, count tokens
For a single output, tokenise it and compare with the run’s max_tokens.
from tokenizers import Tokenizer
def hit_token_cap(texts, model_id, max_tokens, margin=8):
"""True where a saved output used (almost) the whole token budget."""
tok = Tokenizer.from_pretrained(model_id)
tok.no_truncation() # some tokenizer files truncate by default
encs = tok.encode_batch_fast(texts, add_special_tokens=False)
return [len(e.ids) >= max_tokens - margin for e in encs]Re-tokenising saved text doesn’t give exactly the server’s count, hence the margin. With an exact match I missed 9 of 84 looped pages for dots.ocr. With a margin of 8 I got 84 of 84, and 55 of 55 for OvisOCR2, with no false flags for either. For both models the longest normal page was at most half the limit. Across 19 runs it found 721 of 749 with no false flags. The misses are degenerate loops, such as one character repeated thousands of times, which re-tokenise to a different count than the model generated. Tokenising 2,165 pages took about a third of a second on a laptop.
You do need the limit the run actually used. I tried looking limits up instead: for 70 OCR model repos on the Hub I collected every limit in the config files and model cards. None of the config files matched the limit a run actually used; the model cards gave it for 7 of 19 runs, and the small values in quick-start code (512, 1,024) produced false flags.
3. If you know nothing, compress the end
Repetitive text compresses well. Compress only the end of the output, where the loop is: a page can be a correct transcription followed by a loop, so the whole text catches fewer loops.
import zlib
def looks_looped(text, min_len=3000, tail=500, max_ratio=0.3):
"""Flag OCR output that probably ended in a repetition loop."""
if len(text) < min_len:
return False
t = text[-tail:].encode()
return len(zlib.compress(t)) / len(t) < max_ratioOn the leaderboard data this catches 736 of 749 looped pages, with 51 false flags in about 40,000 normal pages. I picked the thresholds on the same data, so that figure is optimistic. Picking them on all models but one and testing on the held-out model catches more loops (99.6%) at the cost of nearly three times as many false flags (143 in the same pages). On a second set (7,650 outputs from nine model configurations over books, an encyclopaedia and typescript) it found 392 of 412, with 75 false flags.
It gets these wrong:
- Tables with padding or rows of empty cells. Of 12 false flags I read on the second set, 11 were tables.
- Dot leaders on index and contents pages.
- Counting loops (
779. S / 780. E / 781. S ...). The numbers change on every pass, so the text doesn’t compress enough.

looks_looped: DeepSeek-OCR-2’s output on a printed index page from Birds of Great Britain and Ireland, vol. 2 (1907, p. 215; BHL PageID 8992652). The transcription finishes cleanly at 1,490 of 4,096 tokens, but the dot leaders compress the last 500 bytes to 114 bytes (ratio 0.228).Cut off is not the same as looped
Counting tokens tells you a page was cut off. Compression tells you it ended in repetition. On the leaderboard’s book pages these were nearly the same, because the limits were far above the length of a real page.
When the limit is close to real page length, they differ. On historical newspapers with a 16,384-token limit, 190 pages hit the limit and fewer than half were loops. The rest were dense pages with more text than the limit allowed. Tables of numbers at a 4,096 limit did the same.
A looped page needs a retry or a different model. A cut-off page needs a higher limit. Cutting a looped page at the start of the loop rarely saves it: on looped pages with real text, the median output before the loop is about 90 characters, against about 1,900 characters of text on the page. Only around one page in five has most of its text before the loop.
Not tested yet
- The margin for the token count depends on the output format. On newspapers,
dots.mocr’s layout JSON with bounding boxes needed a margin of 64 rather than 8. - Everything here is on raw model output, markup included. Cleaned text may behave differently.
- The compression thresholds come from historical books. Tables and typescript produce more false flags.
- Catching counting loops.
When a small model makes sense
If you need to separate real tables and dot-leader indexes from loops — or catch counting loops, missing text, and garbled layers that text heuristics miss — I trained ocr-decision-0.8b (Apache-2.0). It compares the page image against the OCR text in a single forward pass with zero generated tokens (0.998–1.000 AUROC on repetition loops across out-of-distribution historical books, newspapers, archival cards, and OmniDocBench).