fix(tokenizers): raise ValueError instead of TypeError when Content-Length is absent#764
Open
xodn348 wants to merge 1 commit into
Open
fix(tokenizers): raise ValueError instead of TypeError when Content-Length is absent#764xodn348 wants to merge 1 commit into
xodn348 wants to merge 1 commit into
Conversation
andrewbcohere
approved these changes
May 6, 2026
| if size is None: | ||
| raise ValueError("Content-Length unavailable (server may use chunked transfer encoding)") | ||
|
|
||
| return round(int(size) / 1024 / 1024, 2) |
Contributor
There was a problem hiding this comment.
keep the typing cast here unless you had a specific reason to remove it
andrewbcohere
requested changes
May 6, 2026
| if size is None: | ||
| raise ValueError("Content-Length unavailable (server may use chunked transfer encoding)") | ||
|
|
||
| return round(int(size) / 1024 / 1024, 2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_get_tokenizer_config_sizeretrieves the size of a remote tokenizer config via an HTTPHEADrequest and converts theContent-Length(orx-goog-stored-content-length) header value to an integer. When a server responds with chunked transfer encoding it omits both headers, sosizeremainsNone. The subsequentint(typing.cast(int, size))call raises a crypticTypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'instead of a clear, actionable error. Although the caller already wraps the call intry/except Exception, theTypeErrorleaks a confusing message ("Failed to get the size of the tokenizer config: int() argument must…") that sends users debugging in the wrong direction. This PR replaces the implicitTypeErrorwith an explicitValueErrorthat clearly names the root cause, and also removes the misleadingtyping.cast(int, size)annotation (headers always returnstr | None, neverint).Issue
Fixes #762
Local verification
Risk
Only
_get_tokenizer_config_sizeis changed. The function is always invoked inside atry/except Exceptionblock in bothget_hf_tokenizerandasync_get_hf_tokenizer, so raisingValueErrorinstead of lettingTypeErrorpropagate does not change observable behavior for callers — the tokenizer still downloads successfully when size headers are absent. Thetyping.castremoval is a no-op at runtime (castis transparent) and makes the type annotation accurate.Note
Low Risk
Low risk: small error-handling change in non-critical logging code plus targeted unit tests; behavior remains within existing try/except paths.
Overview
Improves
_get_tokenizer_config_sizeto explicitly raiseValueErrorwhen neitherx-goog-stored-content-lengthnorContent-Lengthis present, avoiding a confusingTypeErrorfromint(None).Adds
tests/test_tokenizer_config_size.pyto validate size parsing for both headers (including priority) and to assert the new exception behavior when size headers are missing (e.g., chunked transfer encoding).Reviewed by Cursor Bugbot for commit 931e7e4. Bugbot is set up for automated code reviews on this repo. Configure here.