fix(openai): support o-series models in get_num_tokens_from_messages - #38710
Merged
ccurme (ccurme) merged 3 commits intoAug 17, 2026
Merged
Conversation
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Anay Garodia (AnayGarodia)
requested review from
ccurme (ccurme) and
Mason Daugherty (mdrxy)
as code owners
July 8, 2026 03:35
This comment has been minimized.
This comment has been minimized.
Closed
29 tasks
ccurme (ccurme)
approved these changes
Aug 14, 2026
ccurme (ccurme)
enabled auto-merge (squash)
August 14, 2026 23:15
ccurme (ccurme)
disabled auto-merge
August 14, 2026 23:15
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #38700
Calling
get_num_tokens_from_messageson aChatOpenAIinstance configured with any o-series model (o1,o1-preview,o1-mini,o3,o3-mini,o4-mini) raisesNotImplementedError, because the model-prefix check that selects the per-message token format only recognizesgpt-3.5-turbo,gpt-4, andgpt-5prefixes. Anyone counting tokens for budgeting or context-window management with reasoning models hits this immediately.o-series chat models use the same message token accounting as the other post-
gpt-3.5-turbo-0301chat models (3 tokens per message, 1 per name), andtiktokenalready resolves their encodings, so the fix is to include theo1/o3/o4prefixes in the existing check.The new parametrized unit test asserts each o-series model produces the same count as the reference chat model for identical messages and fails with
NotImplementedErrorwithout the fix.Release note
get_num_tokens_from_messagesonChatOpenAInow supports o-series reasoning models (o1,o3,o4families) instead of raisingNotImplementedError.Disclaimer: this contribution was prepared with an AI coding agent (Claude Code), driven and reviewed by Anay Garodia (@AnayGarodia).