langchain · difficulty ◆◆
Standard Model Exceptions — retry & handle any provider
One portable except block for rate limits, timeouts, bad keys, and missing models — no matter which provider backs your call.
Swap OpenAI for Anthropic tomorrow, and your retry logic still works — because the error is the same class.
$ pip install -U langchain==1.3.16What it does
LangChain 1.3.16 introduces standard model exception types in langchain-core (PR #39538). Instead of catching a generic Exception or branching on each vendor's error classes, you now get a small, consistent hierarchy that every model integration — OpenAI, Anthropic, Fireworks, Perplexity, and the rest — raises for the same class of failure: ModelError (base), ModelRateLimitError, ModelTimeoutError, ModelAuthenticationError, and ModelNotFoundError. Because it ships in langchain-core, every partner package inherits it automatically — which is why it shows up in the changelogs of langchain, langchain-openai, and langchain-fireworks on the same day.
Why it matters
In production you almost always wrap model calls in retry and error-handling logic. Before this, you had to branch on each vendor's error classes (openai.RateLimitError, anthropic.APIStatusError, ...) or catch a broad Exception and guess. Standard exception types give you one portable except clause that works no matter which model provider you swap in. That is invaluable in multi-provider apps (fallback chains, model routing) or a library other teams consume — your error handling stops leaking provider-specific types. Paired with the ModelRetryMiddleware fix in the same release (re-raising non-retryable exceptions), you get clean, provider-agnostic resilience logic.
Example
$ safe_generate — translate any failure into a standard exceptionfrom langchain_core.exceptions import (
ModelError,
ModelRateLimitError,
ModelTimeoutError,
ModelAuthenticationError,
ModelNotFoundError,
)
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
def safe_generate(prompt: str) -> str:
try:
return llm.invoke(prompt).content
except ModelRateLimitError as e:
print(f"[retryable] rate limited: {e}")
raise
except ModelTimeoutError as e:
print(f"[retryable] timed out: {e}")
raise
except ModelAuthenticationError as e:
print(f"[fatal] bad API key: {e}")
raise
except ModelNotFoundError as e:
print(f"[fatal] model does not exist: {e}")
raise
except ModelError as e:
print(f"[unknown] {e}")
raise
# Works identically no matter which provider backs llm
try:
print(safe_generate("Say hello in one word."))
except ModelError:
print("Handled a standard model exception.")Expected output: Hello. With an invalid key you get [fatal] bad API key and ModelAuthenticationError propagates; on a rate limit, [retryable] rate limited prints and ModelRateLimitError propagates.
Common flags
- ModelError
- Base class for all standard model exceptions
- ModelRateLimitError
- Provider throttles requests — retryable
- ModelTimeoutError
- Call exceeds the timeout — retryable
- ModelAuthenticationError
- Invalid/missing credentials — not retryable
- ModelNotFoundError
- The requested model does not exist — not retryable
History
The standard-error project
The feature ships in langchain-core via PR #39538 and lands in langchain==1.3.16 (2026-08-20; no brand-new release on 2026-08-24, so this is the most recent substantive release). Because the exception types live in the core package, all partner packages inherit them automatically — that is why the same feature appears in the changelogs of langchain, langchain-openai, and langchain-fireworks on the same day. The companion ModelRetryMiddleware fix (PR #38960) ensures non-retryable exceptions re-raise immediately instead of being swallowed.
Fun facts
Pros & cons
pros
- + One portable except clause across every provider
- + Clean retry vs. fatal distinction built in
- + No provider-specific error types leaking into your code
- + Inherited automatically by all partner packages
cons
- − Requires upgrading to langchain==1.3.16+
- − Existing code that catches vendor-specific errors needs a migration
Takeaways
- 1Import the standard types from langchain_core.exceptions and catch them directly
- 2Retry on ModelRateLimitError and ModelTimeoutError; fix and re-raise on auth/not-found
- 3Verify by swapping ChatOpenAI for ChatAnthropic or ChatFireworks — your except blocks stay the same
- 4Test with an invalid API key to confirm you catch ModelAuthenticationError, not a vendor class
- 5Wrap safe_generate in a ModelRetryMiddleware-style loop that only retries the retryable types