Replies: 1 comment
-
LangChain swallows errors in chat message histories to provide a smoother user experience by handling tool invocation errors gracefully. This behavior can be managed by implementing custom error handling strategies, such as using try/except blocks, fallbacks, or retry mechanisms. To opt out of this behavior, you can implement a try/except tool call as shown below: from typing import Any
from langchain_core.runnables import Runnable, RunnableConfig
def try_except_tool(tool_args: dict, config: RunnableConfig) -> Runnable:
try:
complex_tool.invoke(tool_args, config=config)
except Exception as e:
return f"Calling tool with arguments:\n\n{tool_args}\n\nraised the following error:\n\n{type(e)}: {e}"
chain = llm_with_tools | (lambda msg: msg.tool_calls[0]["args"]) | try_except_tool
print(
chain.invoke(
"use complex tool. the args are 5, 2.1, empty dictionary. don't forget dict_arg"
)
) This code snippet demonstrates how to catch and handle errors explicitly, thus opting out of the default error-swallowing behavior [1]. Regarding the specific error handling logic around line 181 in the try:
from botocore.exceptions import ClientError
except ImportError as e:
raise ImportError(
"Unable to import botocore, please install with `pip install botocore`."
) from e This logic ensures that the necessary module is available and provides a clear error message if it is not [2]. If you need more granular control over error handling, LangChain offers various strategies, such as using the For more detailed control, you can also use the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Description
I noticed that in multiple places, specifically in chat message histories, LangChain swallows any errors and sends them to a generic logger. For example: https://github.com/langchain-ai/langchain/blob/master/libs/community/langchain_community/chat_message_histories/dynamodb.py#L181
In a project we want to save chat history for DynamoDB and catch the error and handle it ourselves. Specifically we don't want to set a message count limit but we're handling when DynamoDB size limit is hit and we manually truncate some messages as needed. Because LangChain swallows these errors we have to override the methods and write our own implementation.
My questions are:
EDIT: A message history runnable is out of the question (unless there's a different pattern) because we are using LangGraph and have specific nodes for handling message history and using consolidation patterns before some steps as well.
System Info
System Information
Package Information
Optional packages not installed
Other Dependencies
Beta Was this translation helpful? Give feedback.
All reactions