These error codes represent various scenarios where errors might occur, such as invalid prompts, tool execution issues, or model-related problems.
Error Messages
Error messages are constructed using these error codes. A helper function, create_message, is provided in the same file to generate detailed error messages:
The create_message function formats an error message that includes:
A custom message (message).
A URL for troubleshooting based on the provided error_code.
Example Error Message:
For an error like OUTPUT_PARSING_FAILURE, the generated message might look like:
code
"An error occurred while parsing the output.
For troubleshooting, visit: https://python.langchain.com/docs/troubleshooting/errors/OUTPUT_PARSING_FAILURE"
Exception Classes
Custom exceptions are defined to handle specific errors. Examples include:
LangChainException: A generic base exception for LangChain.
OutputParserException: A specific exception for parsing errors, which includes:
error: The specific error message.
observation: Additional context about the error.
llm_output: The model's output that caused the error.
send_to_llm: A flag indicating whether the error context should be sent back to the model for remediation.
The repository includes utility functions for error handling:
raise_for_status_with_text in ./repos/langchain/libs/core/langchain_core/utils/utils.py:
Used to raise an error with the HTTP response text if a request fails.
py
def raise_for_status_with_text(response: Response) -> None:
try:
response.raise_for_status()
except HTTPError as e:
raise ValueError(response.text) from e
Warnings for Deprecation:
Deprecated methods and imports are accompanied by warnings to guide users on updated usage.
Summary
Error Codes: Defined as an Enum in langchain_core/exceptions.py.
Error Messages: Constructed using the create_message function, with troubleshooting links based on error codes.
Custom Exceptions: Classes like OutputParserException provide structured error handling.
Utilities: Functions like raise_for_status_with_text help manage HTTP errors.
These mechanisms ensure that LangChain provides meaningful and actionable error messages to developers.