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 3 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.

docs/tools.md Outdated
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.

Copy link
Member

Choose a reason for hiding this comment

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

please delete this main function part

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
docs/tools.md Outdated
from agents import Agent, Runner, function_tool
from typing import Any, List

def my_custom_error_function(error: Exception, *args: Any, **kwargs: Any) -> 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 think this is still incorrect

Copy link
Contributor

Choose a reason for hiding this comment

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

@seratch hello
I hope you are well

It's my request you should merge it because it's soo clear and user-friendly and I run this it's work correct so this kind of code need in document

docs/tools.md Outdated
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.

please delete this main function part

@MurtazaAliCode
Copy link
Author

fix it

@muhammadhamidrazasidtechno
Copy link
Contributor

I actually created an issue proposing a documentation update and included this exact example myself, so it's great to see that we're aligned on the best approach! I also tested it by passing user_id 'user_456', and it worked flawlessly, correctly demonstrating the custom error handling.

@MuhammadHamidRaza
Copy link
Contributor

Merge it @seratch

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.

4 participants