Skip to content

docs: Add a comprehensive example for handling function tool errors. #1354

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

MurtazaAliCode
Copy link

This Pull Request adds a comprehensive and runnable code example to the "Handling errors in function tools" documentation section. The goal is to make a complex topic more accessible to new users.

The current documentation accurately describes the theory behind using failure_error_function, but it lacks a practical, complete example that shows the concept in action. This can make it challenging for developers to correctly implement custom error handling.

This PR addresses that gap by providing a clean, working example that demonstrates the following:

  • Custom Error Handling in Practice: It shows how to define and use a my_custom_error_function to gracefully handle exceptions within a function tool.
  • Correct Implementation: It fixes an issue where the model variable was undefined, ensuring the code is fully runnable out-of-the-box.
  • Clear Output: The example correctly shows how the custom error message is returned as the final output, proving that the error was handled as intended without crashing the program.

This contribution makes the documentation more robust and user-friendly, which will significantly improve the developer experience for the community.

docs/tools.md Outdated
import asyncio
from agents import Agent, Runner, function_tool, FunctionTool, Model

def my_custom_error_function(error, *args) -> str:
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this method signature is correct.

raise ValueError("Data list cannot be empty.")
return "Operation successful."

async def main():
Copy link
Member

Choose a reason for hiding this comment

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

To keep the document page as simple as possible, let's have only the tool and its error function in this code snippet.

docs/tools.md Outdated
return "An internal server error occurred. Please try again later."

@function_tool(failure_error_function=my_custom_error_function)
def risky_operation(data) -> str:
Copy link
Member

Choose a reason for hiding this comment

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

I feel "risky" is not the case here. It should be more of something that can fail. How about using an example to perform a backend API call (no need to use real backend; just pseudo code)?

@seratch seratch added the documentation Improvements or additions to documentation label Aug 3, 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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants