Repository navigation
Conversation
mangeshraut712
left a comment
There was a problem hiding this comment.
The OverflowError → KeyError conversion works for decode / decode_bytes / decode_single_token_bytes, but the KeyError payloads are inconsistent.
decode_single_token_bytes (and therefore decode_tokens_bytes / decode_with_offsets) raises KeyError(str(token)) — e.g. KeyError('-1') — while decode_bytes re-raises KeyError(f"Invalid token for decoding: {token}"). Callers that branch on e.args[0] will see different shapes depending on which API they hit.
Please use one message (and ideally cover decode_tokens_bytes([-1]) in test_decode_invalid_tokens so the list helpers stay aligned).
e5f4b81 to
dc74a0e
Compare
justavibedev
left a comment
There was a problem hiding this comment.
One case still escapes a caller's except KeyError on dc74a0e: an integer whose decimal representation exceeds Python's digit limit. On Python 3.12.11 with the default 4,300-digit limit:
enc = tiktoken.Encoding(
"bytes", pat_str=r"(?s).",
mergeable_ranks={bytes([i]): i for i in range(256)}, special_tokens={},
)
enc.decode([2**64]) # KeyError, as intended
enc.decode([10**5000]) # ValueError: Exceeds the limit (4300 digits) ...The second call reaches the new overflow handler, but interpolating {token} raises before the KeyError is constructed. I reproduced the same result for all five decode entry points in the new test, with the PR's Python source and the 0.14.0 native extension. A bounded out-of-range message for integers too large to display would preserve the exception contract without changing the process-wide digit limit. The regression can assert the exception type without interpolating the bad ID in the test itself.
Tested with Codex assistance.
dc74a0e to
2c2b810
Compare
2c2b810 to
c37113a
Compare
Negative token IDs and values exceeding unsigned 64-bit bounds failed during PyO3 int conversion before reaching the Rust vocabulary lookup, raising an unexpected OverflowError instead of KeyError. Catching OverflowError in decode_bytes and decode_single_token_bytes retains zero-cost fast path decoding while ensuring consistent KeyError exceptions across all out-of-range inputs.
Fixes #630