From 7c1681c5acf240f7396ca38c642d579c5c3eb78a Mon Sep 17 00:00:00 2001 From: yen0304 Date: Fri, 12 Jun 2026 21:08:58 +0800 Subject: [PATCH] fix(model): add exact mapping for gpt-4.5 `encoding_for_model("gpt-4.5")` raised a KeyError because only the "gpt-4.5-" prefix existed, which requires a trailing hyphen; the bare model name "gpt-4.5" does not start with "gpt-4.5-" and therefore falls through to the error path. Adds "gpt-4.5" to MODEL_TO_ENCODING (same pattern as "gpt-5", "gpt-4.1", and "gpt-4o" which all have both an exact entry and a versioned prefix). --- tests/test_misc.py | 7 +++++++ tiktoken/model.py | 1 + 2 files changed, 8 insertions(+) diff --git a/tests/test_misc.py b/tests/test_misc.py index 0832c8ee..f6ae6e84 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -19,6 +19,13 @@ def test_encoding_for_model(): assert enc.name == "o200k_base" enc = tiktoken.encoding_for_model("gpt-oss-120b") assert enc.name == "o200k_harmony" + # Regression: "gpt-4.5" (exact name without a version suffix) must resolve + # via MODEL_TO_ENCODING, not the "gpt-4.5-" prefix which requires a trailing + # hyphen and therefore doesn't match the bare model name. + enc = tiktoken.encoding_for_model("gpt-4.5") + assert enc.name == "o200k_base" + enc = tiktoken.encoding_for_model("gpt-4.5-preview") + assert enc.name == "o200k_base" def test_optional_blobfile_dependency(): diff --git a/tiktoken/model.py b/tiktoken/model.py index 5c669af4..a77b1db6 100644 --- a/tiktoken/model.py +++ b/tiktoken/model.py @@ -33,6 +33,7 @@ "o4-mini": "o200k_base", # chat "gpt-5": "o200k_base", + "gpt-4.5": "o200k_base", "gpt-4.1": "o200k_base", "gpt-4o": "o200k_base", "gpt-4": "cl100k_base",