Handle model_max_length / max_length = 0 in tokenizer config (fixes #685) - #687
Handle model_max_length / max_length = 0 in tokenizer config (fixes #685)#687unusdon wants to merge 1 commit into
Conversation
…drant#685) HuggingFace tokenizer configs may set 'model_max_length' or 'max_length' to 0 — used e.g. when a tokenizer stores its padding token inline in tokenizer.json without an external file. The prior key-existence check would pick the 0 in the min(), then enable_truncation(max_length=0) silently disables truncation, and any input past the underlying model's context window then crashes downstream. Switch to truthiness checks so 0 falls back to the other key (or to AssertionError when both are 0/missing). Add tests/test_preprocessor_utils.py covering zero-fallback in each direction, both-non-zero (min still wins), either-key-missing, and both-zero (raises).
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review. 📝 WalkthroughWalkthrough
Estimated code review effort: 3 (Moderate) | ~20 minutes Merge Risk: ⚪ Minimal · up to The change makes zero-valued tokenizer limits fall back safely to a valid limit and adds coverage for the supported cases; no actionable merge-blocking risk remains after normal checks and review. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Fixes #685.
What changed
HuggingFace tokenizer configs may set
model_max_lengthormax_lengthto0— used e.g. when a tokenizer stores its padding token inline intokenizer.jsonwithout an external file. The prior key-existence check inload_tokenizerpicked the0in themin(), thentokenizer.enable_truncation(max_length=0)silently disabled truncation, and any input past the underlying model's context window crashed downstream.Switched to truthiness checks so
0falls back to the other key (or toAssertionErrorwhen both are 0/missing).Fix
with open(str(tokenizer_config_path)) as tokenizer_config_file: tokenizer_config = json.load(tokenizer_config_file) - assert "model_max_length" in tokenizer_config or "max_length" in tokenizer_config, ( - "Models without model_max_length or max_length are not supported." + model_max_length = tokenizer_config.get("model_max_length") + max_length = tokenizer_config.get("max_length") + assert model_max_length or max_length, ( + "Models without a non-zero model_max_length or max_length are not supported." ) - if "model_max_length" not in tokenizer_config: - max_context = tokenizer_config["max_length"] - elif "max_length" not in tokenizer_config: - max_context = tokenizer_config["model_max_length"] + if not model_max_length: + max_context = max_length + elif not max_length: + max_context = model_max_length else: - max_context = min(tokenizer_config["model_max_length"], tokenizer_config["max_length"]) + max_context = min(model_max_length, max_length)Handles both cases the reporter flagged (either key present as
0) as well as the missing-key cases the original code already covered.Tests
Added
tests/test_preprocessor_utils.py— 6 tests covering:model_max_length=0falls back tomax_lengthmax_length=0falls back tomodel_max_lengthmin()still winsAssertionError(still fails loud)All 6 pass locally against
fastembed@0.8.0.