Skip to content

Add error function example in tools #1371

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

Closed

Conversation

kankute-sameer
Copy link
Contributor

Fixes #1367 - Add code example for custom error handling in function tools

Adds a practical code example demonstrating how to use the failure_error_function parameter with @function_tool decorator. The example shows:

  • How to create a custom error handler function
  • How to apply it to a function tool using the decorator
  • Expected behavior when the tool fails

This addresses the need for better documentation around error handling in function tools, making it easier for developers to implement custom error responses for their tools.

Comment on lines +323 to +337

```python
def sync_error_handler(ctx: RunContextWrapper[Any], error: Exception) -> str:
"""Custom error handler that provides a formatted error message."""
return f"An error occurred: {error.__class__.__name__} - {str(error)}"

@function_tool(failure_error_function=sync_error_handler)
def get_latest_news():
"""Get the latest news from the web."""
# This function might fail due to network issues, API limits, etc.
raise ConnectionError("Failed to connect to news service")

# The error handler will be called if the tool fails
# and will return: "An error occurred: ConnectionError - Failed to connect to news service"
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put it before If you are manually creating a FunctionToolobject, then you must handle errors inside theon_invoke_tool function. line?

With this example, it will be even harder to notice that line for class FunctionTool

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its duplicate #1354 Add a comprehensive example for handling function tool errors and you made more comlicated it's hard for beginner

@muhammadhamidrazasidtechno
Copy link
Contributor

Hello @kankute-sameer

I appreciate your contribution. I've reviewed the example, and while it's accurate, I'm concerned it might be a bit complex for a beginner to fully grasp.

I've been working with PR #1354, which has a very clear and simple example for handling function tool errors. I tested it by passing user_id: 'user_456' and it successfully produced an error, which the error function then handled gracefully. This type of straightforward example is perfect for beginners to understand the core concept of a failure_error_function.

Perhaps we could consider using a more beginner-friendly approach like the one in #1354 to make the documentation more accessible to a wider audience.

@kankute-sameer
Copy link
Contributor Author

Yup. That looks great. Closing this as it is a duplicate.

@seratch seratch added documentation Improvements or additions to documentation duplicate This issue or pull request already exists labels Aug 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation duplicate This issue or pull request already exists
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Configure failure_error_function in FunctionTool class
4 participants