Skip to content

fix(openai): support o-series models in get_num_tokens_from_messages - #38710

Merged
ccurme (ccurme) merged 3 commits into
langchain-ai:masterfrom
AnayGarodia:AnayGarodia/openai/o-series-token-count
Aug 17, 2026
Merged

fix(openai): support o-series models in get_num_tokens_from_messages#38710
ccurme (ccurme) merged 3 commits into
langchain-ai:masterfrom
AnayGarodia:AnayGarodia/openai/o-series-token-count

Conversation

@AnayGarodia

Copy link
Copy Markdown
Contributor

Closes #38700


Calling get_num_tokens_from_messages on a ChatOpenAI instance configured with any o-series model (o1, o1-preview, o1-mini, o3, o3-mini, o4-mini) raises NotImplementedError, because the model-prefix check that selects the per-message token format only recognizes gpt-3.5-turbo, gpt-4, and gpt-5 prefixes. 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-0301 chat models (3 tokens per message, 1 per name), and tiktoken already resolves their encodings, so the fix is to include the o1/o3/o4 prefixes 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 NotImplementedError without the fix.

Release note

get_num_tokens_from_messages on ChatOpenAI now supports o-series reasoning models (o1, o3, o4 families) instead of raising NotImplementedError.


Disclaimer: this contribution was prepared with an AI coding agent (Claude Code), driven and reviewed by Anay Garodia (@AnayGarodia).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 8, 2026 03:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions github-actions Bot added fix For PRs that implement a fix integration PR made that is related to a provider partner package integration openai `langchain-openai` package issues & PRs size: XS < 50 LOC labels Jul 8, 2026
@github-actions

This comment has been minimized.

@ccurme
ccurme (ccurme) enabled auto-merge (squash) August 14, 2026 23:15
@ccurme
ccurme (ccurme) merged commit 313bc54 into langchain-ai:master Aug 17, 2026
87 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external fix For PRs that implement a fix integration PR made that is related to a provider partner package integration new-contributor openai `langchain-openai` package issues & PRs size: XS < 50 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: get_num_tokens_from_messages raises NotImplementedError for OpenAI o1 and o3 models

3 participants