LangChain Agent Tool Error — How to Fix and Prevent This Common Issue
In this tutorial, you'll learn about LangChain Agent Tool Error. We cover key concepts, practical examples, and best practices.
Your LangChain agent selects the wrong tool or fails to execute a tool call. The tool definition has missing input parameters or the agent cannot parse the tool description. Learn to define robust tools and debug agent routing.
The Problem
You define a tool for your agent but the agent cannot use it:
from langchain.agents import tool
@tool
def search(query: str) -> str:
"""Search the web for information."""
return f"Results for: {query}"
agent.run("Search for AI news")
The agent either ignores the tool or raises:
AgentExecutionError: Tool search received unexpected input
Step-by-Step Fix
Step 1: Use StructuredTool for typed parameters
from langchain.tools import StructuredTool
from langchain.pydantic_v1 import BaseModel, Field
class SearchInput(BaseModel):
query: str = Field(description="The search query")
max_results: int = Field(default=5, description="Number of results")
def search(query: str, max_results: int = 5) -> str:
return f"Found {max_results} results for: {query}"
tool = StructuredTool(
name="search",
description="Search the web for information",
args_schema=SearchInput,
func=search
)
Step 2: Create the agent with proper tools
from langchain.agents import create_openai_functions_agent, AgentExecutor
agent = create_openai_functions_agent(llm, [tool], prompt)
agent_executor = AgentExecutor(agent=agent, tools=[tool], verbose=True)
result = agent_executor.invoke({"input": "Search for AI news"})
Step 3: Debug tool selection
Set verbose=True to see which tools the agent selects and why.
Prevention Tips
- Use StructuredTool for tools with multiple parameters
- Write clear, detailed tool descriptions the agent can parse
- Set return_direct for tools that return final answers
- Test each tool in isolation before adding to an agent
- Limit the number of tools per agent to avoid confusion
Advanced Troubleshooting
Check the Logs
Most LangChain errors are logged to stdout or a dedicated log file. Check your logs first:
# Check system logs
journalctl -u langchain --since "1 hour ago"
# Or check the application log
tail -50 ~/.langchain/logs/error.log
Test with a Minimal Example
Create the simplest possible langchain configuration to verify the base setup works:
langchain --version
langchain --help
If the minimal test passes, add configuration options one at a time until you find the breaking change.
Common Configuration Mistakes
- Using the wrong file path or URL in configuration
- Forgetting to restart LangChain after changing config files
- Mixing tabs and spaces in YAML configuration files
- Setting incorrect permissions on configuration directories
When to Reinstall
If none of the above resolves the issue, consider a clean reinstall:
# Backup your configuration
cp -r ~/.langchain ~/.langchain.bak
# Remove and reinstall
# Follow the official LangChain installation guide
This ensures you start from a known good state and can isolate the issue.
Common Mistakes with agent tool
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
These mistakes appear frequently in real-world LANGCHAIN code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro