mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
feat(core): add ContextOverflowError, raise in anthropic and openai (#35099)
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user