feat(core): add ContextOverflowError, raise in anthropic and openai (#35099)

This commit is contained in:
ccurme
2026-02-09 15:15:34 -05:00
committed by GitHub
parent 4ca586b322
commit 7c41298355
5 changed files with 445 additions and 137 deletions

View File

@@ -17,7 +17,7 @@ from langchain_core.callbacks import (
AsyncCallbackManagerForLLMRun,
CallbackManagerForLLMRun,
)
from langchain_core.exceptions import OutputParserException
from langchain_core.exceptions import ContextOverflowError, OutputParserException
from langchain_core.language_models import (
LanguageModelInput,
ModelProfile,
@@ -721,8 +721,16 @@ def _is_code_execution_related_block(
return False
class AnthropicContextOverflowError(anthropic.BadRequestError, ContextOverflowError):
"""BadRequestError raised when input exceeds Anthropic's context limit."""
def _handle_anthropic_bad_request(e: anthropic.BadRequestError) -> None:
"""Handle Anthropic BadRequestError."""
if "prompt is too long" in e.message:
raise AnthropicContextOverflowError(
message=e.message, response=e.response, body=e.body
) from e
if ("messages: at least one message is required") in e.message:
message = "Received only system message(s). "
warnings.warn(message, stacklevel=2)