Skip to content

fix(model): add exact mapping for gpt-4.5#569

Open
yen0304 wants to merge 1 commit into
openai:mainfrom
yen0304:fix/gpt-4.5-exact-mapping
Open

fix(model): add exact mapping for gpt-4.5#569
yen0304 wants to merge 1 commit into
openai:mainfrom
yen0304:fix/gpt-4.5-exact-mapping

Conversation

@yen0304

@yen0304 yen0304 commented Jun 12, 2026

Copy link
Copy Markdown

encoding_for_model("gpt-4.5") raises a KeyError today:

>>> import tiktoken
>>> tiktoken.encoding_for_model("gpt-4.5")
KeyError: 'Could not automatically map gpt-4.5 to a tokeniser. ...'

Root cause: MODEL_PREFIX_TO_ENCODING contains "gpt-4.5-" (note the trailing hyphen), which correctly handles versioned variants like gpt-4.5-preview. However, the bare model name "gpt-4.5" does not start with "gpt-4.5-" — the trailing hyphen is never matched — so it falls through to the KeyError path.

This is the same shape as the gpt-5.1 issue (#464 / PR #554). Every other dot-minor model (gpt-5, gpt-4.1, gpt-4o) has both an exact entry in MODEL_TO_ENCODING and a versioned prefix. gpt-4.5 was missing the exact entry.

Fix: add "gpt-4.5": "o200k_base" to MODEL_TO_ENCODING, consistent with all peer models.

Verified locally:

>>> tiktoken.encoding_for_model("gpt-4.5").name
'o200k_base'
>>> tiktoken.encoding_for_model("gpt-4.5-preview").name  # prefix still works
'o200k_base'

This contribution was made with AI assistance.

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

1 participant