Skip to content

fix(python): accept out_type in DecodeIds and DecodePieces - #1336

Merged
taku910 merged 1 commit into
google:masterfrom
kwy404:fix-decode-ids-pieces-out-type
Sep 27, 2026
Merged

taku910 merged 1 commit into
google:masterfrom
kwy404:fix-decode-ids-pieces-out-type

Conversation

@kwy404

@kwy404 kwy404 commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

Describe the change

DecodeIds and DecodePieces (and decode_ids / decode_pieces) default return_type to str and pass it to Decode together with **kwargs. So return_type is never None there, and any out_type from the caller trips the "both specified" check:

sp.DecodeIds(ids, out_type=bytes)
# ValueError: Cannot specify both out_type and return_type. Use return_type instead.

out_type worked on these methods before the pybind11 migration, the README says it is still supported as an alias, the .pyi stubs list it on both methods, and sp.decode(ids, out_type=bytes) works.

The fix defaults return_type to None in both wrappers. Decode already falls back to str, so calls without either argument still return str, and passing both still raises.

Testing Information

Extended test_legacy_out_type_compat with DecodeIds(ids, out_type=bytes) and decode_pieces(pieces, out_type=bytes). It fails with the ValueError above before the change and passes after.

Ran pytest test/ in python/ (Windows, Python 3.12). Every other test gives the same result with and without this change (test_train and test_train_iterator fail on my machine only because my checkout path contains spaces). tools/gen_stubs.py --check passes, so the stub is unchanged.

DecodeIds and DecodePieces default return_type to str and forward it to
Decode, so passing the documented legacy alias out_type always raised
"Cannot specify both out_type and return_type". Default return_type to
None instead; Decode already falls back to str.
@taku910

taku910 commented Sep 27, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the fix! Note that out_type is deprecated and we plan to remove it in a future release, so please migrate to return_type. That said, as long as it's still supported as an alias, DecodeIds/DecodePieces should behave consistently with decode, so I'll merge this. A DeprecationWarning for out_type will be added in a follow-up.

@taku910
taku910 merged commit 561c7e8 into google:master Sep 27, 2026
37 checks passed
kwy404 added a commit to kwy404/kwy404 that referenced this pull request Sep 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants