Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions docs/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,46 @@ When you create a function tool via `@function_tool`, you can pass a `failure_er
- If you explicitly pass `None`, then any tool call errors will be re-raised for you to handle. This could be a `ModelBehaviorError` if the model produced invalid JSON, or a `UserError` if your code crashed, etc.

If you are manually creating a `FunctionTool` object, then you must handle errors inside the `on_invoke_tool` function.

## Example: Custom Error Handling in Function Tools
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, can you delete this headline, plus move the whole change above L322? I noticed placing here does not make sense.

If you are manually creating a FunctionTool object, then you must handle errors inside the on_invoke_tool function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix it


```python
iimport asyncio
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

This comment was marked as off-topic.

Copy link
Member

Choose a reason for hiding this comment

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

@MuhammadHamidRaza As you know, I review everything in this repo. So, please avoid pinging me like this. I’d appreciate your understanding.

"""A custom function to provide a user-friendly error message."""
print(f"A tool call failed with the following error: {error}")
return "An internal server error occurred. Please try again later."

@function_tool(failure_error_function=my_custom_error_function)
def get_user_profile(user_id: str) -> str:
"""Fetches a user profile from a mock API.

This function demonstrates a "flaky" or failing API call.
"""
if user_id == "user_123":
return "User profile for user_123 successfully retrieved."
else:
raise ValueError(f"Could not retrieve profile for user_id: {user_id}. API returned an error.")

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


agent_with_error_tools = Agent(
name="Assistant",
instructions="Use the provided tools to answer.",
model=model,
tools=[get_user_profile]
)

try:
result = await Runner.run(agent_with_error_tools, "Get the profile for user_id 'user_456'.")
print("Final Output:", result.final_output)
except Exception as e:
print(f"An unexpected exception occurred: {e}")


if _name_ == "_main_":
asyncio.run(main())
```