Critical severity intermediate · Fix: 5-10 min

ToolFunctionException

langchain.tools.base.ToolFunctionException

What this error means
This error occurs when a tool function called by the LLM raises an exception that is not caught or handled, causing the application to crash.

Stack trace

traceback
Traceback (most recent call last):
  File "app.py", line 42, in run_tool
    result = tool_function(*args)
  File "tools.py", line 10, in tool_function
    raise ValueError("Invalid input")
langchain.tools.base.ToolFunctionException: Exception in tool function: Invalid input
QUICK FIX
Wrap your tool function calls in try/except blocks to catch exceptions and prevent uncaught crashes.

Why it happens

When the LLM triggers a tool function during function-calling, any exception raised inside that function must be caught and handled properly. If the tool function raises an error without a try/except block, the exception propagates uncaught, causing this error and crashing the app.

Detection

Monitor logs for uncaught exceptions in tool functions and wrap tool calls in try/except blocks to catch and log errors before they crash the application.

Causes & fixes

1

Tool function raises an exception without internal error handling

✓ Fix

Add try/except blocks inside the tool function to catch and handle expected errors gracefully.

2

Calling code invoking the tool function does not catch exceptions

✓ Fix

Wrap calls to tool functions in try/except blocks to catch exceptions and handle or log them appropriately.

3

Unexpected input or edge cases cause the tool function to fail

✓ Fix

Validate inputs before processing and add defensive programming to handle edge cases inside the tool function.

Code: broken vs fixed

Broken - triggers the error
python
def tool_function(data):
    # This will raise an exception if data is invalid
    return 10 / data  # triggers ZeroDivisionError if data=0

result = tool_function(0)  # uncaught exception here
print(result)
Fixed - works correctly
python
import os

def tool_function(data):
    try:
        return 10 / data  # may raise ZeroDivisionError
    except ZeroDivisionError:
        return "Error: division by zero"

result = tool_function(0)  # handled safely
print(result)  # prints error message

# API keys or environment variables can be set here if needed
os.environ["API_KEY"] = os.getenv("API_KEY")
Added try/except inside the tool function to catch ZeroDivisionError and return a safe error message, preventing uncaught exceptions.

Workaround

Temporarily wrap all tool function calls in try/except blocks at the call site to catch exceptions and log errors, avoiding crashes until the tool functions are fixed.

Prevention

Design tool functions with robust input validation and internal error handling, and always wrap calls in try/except to ensure exceptions are caught and managed gracefully.

Python 3.9+ · langchain-core >=0.1.0 · tested on 0.2.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.