langchain · difficulty ◆◆
One Exception Class to Catch Them All
Write one except block that works whether the model is OpenAI, Anthropic, or anything else.
Your except blocks used to break the moment you swapped providers — now one class catches them all.
$ pip install -U langchain-core==1.6.0 langchain-openai>=1.6.0What it does
langchain-core 1.6.0 introduces a standard set of exception types for chat models (feat(core): add standard model exception types #39538). Instead of each provider — OpenAI, Anthropic, and the rest — raising its own bespoke error classes, LangChain now defines a common hierarchy, most notably ModelError and ContextWindowExceededError, that every model integration raises. This means a ContextWindowExceededError from OpenAI and one from Anthropic are now the same exception class, so your error-handling code no longer needs to import provider-specific exceptions.
Why it matters
In real projects you almost always call models behind a provider-agnostic abstraction — a ChatOpenAI today, ChatAnthropic tomorrow. Before this change, catching "the context window is too full" required importing openai.BadRequestError, anthropic.BadRequestError, and so on — and your code broke the moment you swapped providers. With standard exception types you write one except ContextWindowExceededError block that works across every provider. This is the foundation for robust retry logic, graceful degradation (auto-truncating history and retrying), and clean observability in production LLM apps.
Example
$ A safe_invoke wrapper that catches the standard errors uniformlyfrom langchain_core.exceptions import (
ModelError,
ContextWindowExceededError,
)
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o")
def safe_invoke(prompt: str) -> str:
try:
return llm.invoke(prompt).content
except ContextWindowExceededError as e:
print(f"[context] window exceeded: {e}")
return llm.invoke(prompt[-2000:]).content
except ModelError as e:
print(f"[model] error: {e}")
raise
print(safe_invoke("Summarize the LangChain 1.6.0 changelog in one line."))The same except blocks work unchanged if you swap ChatOpenAI for ChatAnthropic.
$ Confirm the standard exceptions resolve after upgradingfrom langchain_core.exceptions import ModelError, ContextWindowExceededError
print(ModelError.__name__, ContextWindowExceededError.__name__)
# ModelError ContextWindowExceededErrorBoth import cleanly from langchain_core.exceptions.
Common flags
- ModelError
- Base class for all model/API failures (rate limits, timeouts, 5xx).
- ContextWindowExceededError
- Raised when input exceeds the model's context window.
- StructuredTool._injected_args_keys
- Fixed to resolve postponed annotations (serialization fix in 1.6.0).
- RunnablePick
- Now deserializable (fix in 1.6.0 for pickling runnables).
History
From a zoo of provider errors to one family
Every model provider had its own way of saying "your input is too long" or "the API failed." OpenAI raised BadRequestError, Anthropic raised its own BadRequestError, and your catch blocks had to know which provider you were talking to. That coupling made provider-agnostic code fragile — swap a model and your error handling silently stopped matching. The 1.6.0 change standardizes the hierarchy so the abstraction layer you already use for models extends to their failures too.
Fun facts
Pros & cons
pros
- + One exception class across every provider
- + Enables clean retry and graceful-degradation logic
- + No more importing provider-specific errors
- + Foundation for robust production LLM apps
cons
- − Requires langchain-core>=1.6.0
- − Provider integrations must be updated to raise the standard types
Takeaways
- 1Upgrade to langchain-core>=1.6.0 for standard exception types.
- 2Catch ContextWindowExceededError to auto-truncate and retry.
- 3Use ModelError as the catch-all for rate limits, timeouts, and 5xx.
- 4Swap providers freely — your error handling no longer couples to a vendor.