langchain - ✅(Solved) Fix Not able to get max context size for newer OpenAI models [1 pull requests, 3 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
langchain-ai/langchain#35205Fetched 2026-04-08 00:27:14
View on GitHub
Comments
3
Participants
3
Timeline
13
Reactions
0
Author
Timeline (top)
commented ×3labeled ×3mentioned ×2subscribed ×2

The langchain_openai package has a static model name to context size mapping for openai models at the following location:

https://github.com/langchain-ai/langchain/blob/b97c629f9ade098236aa56d861a21706a9830f54/libs/partners/openai/langchain_openai/llms/base.py#L658-L701

As we can see, the mapping is only available till gpt-4o. All the models after that e.g gpt-4.1, o* series and gpt-5 series are missing from the mapping. This in turns lead to ValueError when someone tries to get the contextsize for a model which is not present here.

Error Message


ValueError Traceback (most recent call last) ----> 1 OpenAI.modelname_to_contextsize('gpt-5')

File /.venv/lib/python3.12/site-packages/langchain_openai/llms/base.py:710, in BaseOpenAI.modelname_to_contextsize(modelname) 707 context_size = model_token_mapping.get(modelname) 709 if context_size is None: --> 710 raise ValueError( 711 f"Unknown model: {modelname}. Please provide a valid OpenAI model name." 712 "Known models are: " + ", ".join(model_token_mapping.keys()) 713 ) 715 return context_size

ValueError: Unknown model: gpt-5. Please provide a valid OpenAI model name.Known models are: gpt-4o-mini, gpt-4o, gpt-4o-2024-05-13, gpt-4, gpt-4-0314, gpt-4-0613, gpt-4-32k, gpt-4-32k-0314, gpt-4-32k-0613, gpt-3.5-turbo, gpt-3.5-turbo-0301, gpt-3.5-turbo-0613, gpt-3.5-turbo-16k, gpt-3.5-turbo-16k-0613, gpt-3.5-turbo-instruct, text-ada-001, ada, text-babbage-001, babbage, text-curie-001, curie, davinci, text-davinci-003, text-davinci-002, code-davinci-002, code-davinci-001, code-cushman-002, code-cushman-001

Root Cause

The langchain_openai package has a static model name to context size mapping for openai models at the following location:

https://github.com/langchain-ai/langchain/blob/b97c629f9ade098236aa56d861a21706a9830f54/libs/partners/openai/langchain_openai/llms/base.py#L658-L701

As we can see, the mapping is only available till gpt-4o. All the models after that e.g gpt-4.1, o* series and gpt-5 series are missing from the mapping. This in turns lead to ValueError when someone tries to get the contextsize for a model which is not present here.

Fix Action

Fix / Workaround

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Other Dependencies

filetype: 1.2.0 httpx: 0.28.1 jsonpatch: 1.33 openai: 2.16.0 orjson: 3.11.7 packaging: 26.0 pydantic: 2.12.5 pytest: 9.0.2 pyyaml: 6.0.3 requests: 2.32.5 requests-toolbelt: 1.0.0 rich: 14.3.2 tenacity: 9.1.2 tiktoken: 0.12.0 typing-extensions: 4.15.0 uuid-utils: 0.14.0 xxhash: 3.6.0 zstandard: 0.25.0

PR fix notes

PR #35214: chore(openai): extend model_token_mapping till gpt-5.2 for modelname_to_contextsize

Description (problem / solution / changelog)

Very simple fix, to update model_token_mapping for newer openai models. Fixes: #35205

Changed files

  • libs/partners/openai/langchain_openai/llms/base.py (modified, +10/-0)

Code Example

from langchain_openai.llms.base import OpenAI

OpenAI.modelname_to_contextsize('gpt-5')

---

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
----> 1 OpenAI.modelname_to_contextsize('gpt-5')

File /.venv/lib/python3.12/site-packages/langchain_openai/llms/base.py:710, in BaseOpenAI.modelname_to_contextsize(modelname)
    707 context_size = model_token_mapping.get(modelname)
    709 if context_size is None:
--> 710     raise ValueError(
    711         f"Unknown model: {modelname}. Please provide a valid OpenAI model name."
    712         "Known models are: " + ", ".join(model_token_mapping.keys())
    713     )
    715 return context_size

ValueError: Unknown model: gpt-5. Please provide a valid OpenAI model name.Known models are: gpt-4o-mini, gpt-4o, gpt-4o-2024-05-13, gpt-4, gpt-4-0314, gpt-4-0613, gpt-4-32k, gpt-4-32k-0314, gpt-4-32k-0613, gpt-3.5-turbo, gpt-3.5-turbo-0301, gpt-3.5-turbo-0613, gpt-3.5-turbo-16k, gpt-3.5-turbo-16k-0613, gpt-3.5-turbo-instruct, text-ada-001, ada, text-babbage-001, babbage, text-curie-001, curie, davinci, text-davinci-003, text-davinci-002, code-davinci-002, code-davinci-001, code-cushman-002, code-cushman-001
RAW_BUFFERClick to expand / collapse

Checked other resources

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-perplexity
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

Related Issues / PRs

No response

Reproduction Steps / Example Code (Python)

from langchain_openai.llms.base import OpenAI

OpenAI.modelname_to_contextsize('gpt-5')

Error Message and Stack Trace (if applicable)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
----> 1 OpenAI.modelname_to_contextsize('gpt-5')

File /.venv/lib/python3.12/site-packages/langchain_openai/llms/base.py:710, in BaseOpenAI.modelname_to_contextsize(modelname)
    707 context_size = model_token_mapping.get(modelname)
    709 if context_size is None:
--> 710     raise ValueError(
    711         f"Unknown model: {modelname}. Please provide a valid OpenAI model name."
    712         "Known models are: " + ", ".join(model_token_mapping.keys())
    713     )
    715 return context_size

ValueError: Unknown model: gpt-5. Please provide a valid OpenAI model name.Known models are: gpt-4o-mini, gpt-4o, gpt-4o-2024-05-13, gpt-4, gpt-4-0314, gpt-4-0613, gpt-4-32k, gpt-4-32k-0314, gpt-4-32k-0613, gpt-3.5-turbo, gpt-3.5-turbo-0301, gpt-3.5-turbo-0613, gpt-3.5-turbo-16k, gpt-3.5-turbo-16k-0613, gpt-3.5-turbo-instruct, text-ada-001, ada, text-babbage-001, babbage, text-curie-001, curie, davinci, text-davinci-003, text-davinci-002, code-davinci-002, code-davinci-001, code-cushman-002, code-cushman-001

Description

The langchain_openai package has a static model name to context size mapping for openai models at the following location:

https://github.com/langchain-ai/langchain/blob/b97c629f9ade098236aa56d861a21706a9830f54/libs/partners/openai/langchain_openai/llms/base.py#L658-L701

As we can see, the mapping is only available till gpt-4o. All the models after that e.g gpt-4.1, o* series and gpt-5 series are missing from the mapping. This in turns lead to ValueError when someone tries to get the contextsize for a model which is not present here.

System Info

System Information

OS: Linux OS Version: #149~20.04.1-Ubuntu SMP Wed Apr 16 08:29:56 UTC 2025 Python Version: 3.12.12 (main, Feb 4 2026, 20:21:31) [GCC 14.2.0]

Package Information

langchain_core: 1.2.8 langchain: 1.2.8 langchain_openai: 1.1.7

Other Dependencies

filetype: 1.2.0 httpx: 0.28.1 jsonpatch: 1.33 openai: 2.16.0 orjson: 3.11.7 packaging: 26.0 pydantic: 2.12.5 pytest: 9.0.2 pyyaml: 6.0.3 requests: 2.32.5 requests-toolbelt: 1.0.0 rich: 14.3.2 tenacity: 9.1.2 tiktoken: 0.12.0 typing-extensions: 4.15.0 uuid-utils: 0.14.0 xxhash: 3.6.0 zstandard: 0.25.0

extent analysis

Problem Summary

The langchain_openai package has a static model name to context size mapping that is incomplete, leading to a ValueError when trying to get the context size for models not present in the mapping.

Root Cause Analysis

The root cause is the incomplete model name to context size mapping in the langchain_openai package.

Fix Plan

Update the model name to context size mapping

  1. Clone the langchain repository from GitHub.
  2. Update the model_token_mapping dictionary in langchain_openai/llms/base.py to include the missing models.
  3. Run pip install -e . to install the updated package.
  4. Update the langchain_openai package in your project to the latest version.

Example Code

# langchain_openai/llms/base.py
model_token_mapping = {
    # ... existing mappings ...
    'gpt-4.1': 2048,
    'gpt-4.2': 2048,
    'gpt-5': 4096,
    'gpt-5.1': 4096,
    # ... add more missing models ...
}

Verification

  1. Run the reproduction code again to verify that the ValueError is resolved.
  2. Check the context size returned for the updated models.

Extra Tips

  • Make sure to update the langchain_openai package in your project to the latest version.
  • Consider using a dynamic model name to context size mapping instead of a static one.
  • If you're using a CI/CD pipeline, update the pipeline to use the updated package.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING