Fix Decode() of empty input for non-str return types (proto crash, wrong type for bytes/serialized_proto)#1289
Merged
taku910 merged 1 commit intoJul 18, 2026
Conversation
_decode_raw() short-circuited empty/None input by returning the literal str '' regardless of the requested return_type. This crashed Decode([], return_type='proto') with "TypeError: expected bytes, str found" (ParseFromString received a str) and returned a str instead of bytes for return_type=bytes and 'serialized_proto', inconsistent with the non-empty path. Normalize empty input to an empty id sequence and fall through to the regular dispatch, so the result comes from the C++ method and is typed according to return_type. Guard the batch check against indexing an empty sequence. Add a test covering empty input for all return types.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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.
Describe the change
SentencePieceProcessor._decode_raw()short-circuits empty/Noneinput by returning the literalstr''for everyreturn_type. Thestrcase happens to be correct, but the other return types are broken:Decode([], return_type='proto')crashes: thestr''is passed toSentencePieceText.ParseFromString(), which raisesTypeError: expected bytes, str found.Decode([], return_type=bytes)returnsstr''instead ofb''(the non-empty path returnsbytes).Decode([], return_type='serialized_proto')returnsstr''instead of the serialized empty proto (bytes).Root cause: the empty-input guard in
_decode_raw()hardcodesreturn ''and ignores thereturn_typeargument, bypassing the correctly-typed empty results the C++ layer already produces (_DecodeIdsAsBytes([])returnsb'',_DecodeIdsAsSerializedProto([])returns a valid serialized emptySentencePieceText).Fix: instead of returning a hardcoded
'', normalize empty/Noneinput to an empty id sequence and fall through to the regular dispatch, so the result comes from the C++ method and is typed according toreturn_type— exactly like non-empty input. The batch check now guardsinput[0]withlen(input) > 0. No behavior change for any non-empty input, and the defaultreturn_type=strstill returns''for empty input.Link to a related Issue
Fixes #1288
Testing Information
Added
test_decode_empty_inputtopython/test/sentencepiece_test.py, coveringDecode([])andDecode(None)forreturn_typein{str, bytes, 'serialized_proto', 'proto', 'offset_mapping'}. Without the fix it fails withTypeError: expected bytes, str foundon the'proto'case (and thebytestype assertions); with the fix it passes.Ran the full Python test suite with the extension built in place (
python3.11 setup.py build_ext --inplace):All tests pass (58 passed), including the numpy suite, confirming no regression on single/batch id/piece decoding or empty numpy arrays.