Skip to content

fix: raise KeyError for negative and overflowing decode tokens - #631

Open
mohmedmm wants to merge 1 commit into
openai:mainfrom
mohmedmm:fix-decode-overflow-keyerror
Open

mohmedmm wants to merge 1 commit into
openai:mainfrom
mohmedmm:fix-decode-overflow-keyerror

Conversation

@mohmedmm

@mohmedmm mohmedmm commented Nov 3, 2026

Copy link
Copy Markdown

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

@mangeshraut712 mangeshraut712 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@mangeshraut712 mangeshraut712 left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Withdrawn: duplicate of my earlier review.

@mohmedmm
mohmedmm force-pushed the fix-decode-overflow-keyerror branch 2 times, most recently from e5f4b81 to dc74a0e Compare November 3, 2026 23:56

@justavibedev justavibedev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@mohmedmm
mohmedmm force-pushed the fix-decode-overflow-keyerror branch from dc74a0e to 2c2b810 Compare November 4, 2026 12:48
@mohmedmm
mohmedmm force-pushed the fix-decode-overflow-keyerror branch from 2c2b810 to c37113a Compare November 4, 2026 12:59
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.

decode() raises OverflowError instead of KeyError for negative/huge token IDs

3 participants