-
Notifications
You must be signed in to change notification settings - Fork 15.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DOC: Custom Tool Error Handling is not Clear #10710
Comments
🤖 Hello, Thank you for bringing this to our attention. I understand that you're having trouble handling errors for your custom tool that subclasses Here's an example of how you can handle errors in your custom tool: from langchain.tools.base import BaseTool, ToolException
class GetCurrentWeatherTool(BaseTool):
name = "get_current_weather"
description = "Get the current weather for a specified location"
args_schema: Type[WeatherInput] = WeatherInput
handle_tool_error = True # or a string, or a callable
def _run(
self,
location: str,
run_manager: Optional[CallbackManagerForToolRun] = None
) -> str:
"""Use the tool."""
try:
return get_current_weather(location)
except Exception as e:
# If an error occurs, raise a ToolException
raise ToolException("An error occurred in GetCurrentWeatherTool") from e
async def _arun(
self,
location: str,
run_manager: Optional[AsyncCallbackManagerForToolRun] = None
) -> str:
"""Use the tool asynchronously."""
# You can implement the async version of the method here or raise a NotImplementedError if async is not supported
raise NotImplementedError("get_current_weather does not support async") In the If class GetCurrentWeatherTool(BaseTool):
handle_tool_error = lambda e: f"An error occurred: {e}"
# ... other methods and properties ... In this example, if a I hope this helps! If you have any other questions, feel free to ask. Best, Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
@Hsgngr How did you implemented this in pydantic based tool? I have similar question and till curious how to do that. |
I have added |
@Hsgngr Did you find any solution. If so, can you please post it here! |
Issue with current documentation:
I cannot handle my custom_tool's raised errors.
I have this weatherTool:
And I raise ToolException when the API call gets an error:
raise ToolException("location has been found.")
inside ofget_current_weather
Now I am adding this tool to my tools when I'm creating my agent:
In the documentation Custom Tools it explains how to add it and gives an example:
However I couldnt find the equal of that for Subclassing the BaseTool.
Idea or request for content:
It would be great to give an example for Subclassed CustomTool(BaseTool) when defining the tools.
The text was updated successfully, but these errors were encountered: