AttributeError / TypeError
langchain_core.tools.tool: AttributeError: 'function' object has no attribute 'name' OR TypeError: tool() missing required argument 'name'
Stack trace
Traceback (most recent call last):
File "agent.py", line 45, in <module>
agent = create_react_agent(llm, tools, prompt)
File "langchain/agents/agent.py", line 120, in create_react_agent
agent = _create_agent_from_tools(tools, llm, prompt)
File "langchain/agents/tool_calling_agent.py", line 78, in _create_agent_from_tools
tool_names = [t.name for t in tools]
AttributeError: 'function' object has no attribute 'name'
OR
TypeError: tool() missing 1 required positional argument: 'func' Why it happens
The @tool decorator from langchain_core.tools must wrap a function with a proper docstring (first line is the tool name, remaining lines are the description). If you use the old Tool(name=..., func=..., description=...) pattern or forget the docstring, the decorator cannot extract tool metadata. Additionally, agent code expects tool objects with .name, .description, and .invoke() attributes: plain functions lack these.
Detection
Before running the agent, verify each tool with: `print(tool.name, tool.description)` and ensure the tool is callable via `tool.invoke({'arg': value})`. Use isinstance(tool, BaseTool) to confirm it's a proper LangChain tool object.
Causes & fixes
Using the deprecated Tool(name=..., func=..., description=...) pattern instead of @tool decorator
Replace Tool(...) with @tool decorator. Move function to use 'func' parameter name and add a docstring. Example: @tool\ndef my_tool(x: int) -> str:\n """Tool name. Tool description here."""\n return str(x)
Function has no docstring or docstring is empty: @tool cannot extract name/description
Add a proper docstring to the decorated function. First line is the tool name (e.g., 'Calculator'), remaining lines are the description. Use triple quotes: def add(a: int, b: int) -> int:\n """Add two numbers.\n\n Args:\n a: first number\n b: second number\n """\n return a + b
Passing a plain function to create_react_agent instead of a @tool-decorated function
Decorate the function with @tool before passing to create_react_agent. Import: from langchain_core.tools import tool. Then: @tool\ndef my_func(x): ... and pass my_func to tools list.
Tool function has incorrect parameter type hints or missing return type hint
Add explicit type hints to all parameters and the return value. @tool requires Python type hints to infer argument schema. Example: def search(query: str) -> str: not def search(query):
Attempting to set tool.name or tool.description after decoration: these are read-only
Do not modify tool attributes after decoration. Instead, re-decorate with @tool and adjust the docstring. The decorator generates these from the function signature and docstring.
Code: broken vs fixed
import os
from langchain_openai import ChatOpenAI
from langchain.tools import Tool # ❌ OLD PATTERN
from langchain.agents import create_react_agent, AgentExecutor
from langchain import hub
# ❌ BROKEN: Using old Tool() constructor pattern
def add_numbers(a, b): # ❌ No type hints, no docstring
return a + b
add_tool = Tool(
name="add", # ❌ Manual name specification
func=add_numbers,
description="Add two numbers"
)
# ❌ BROKEN: Plain function passed to agent (not a tool object)
def multiply(x, y):
return x * y
tools = [add_tool, multiply] # ❌ multiply is not a tool — missing decorator and metadata
llm = ChatOpenAI(model="gpt-4o-mini", api_key=os.environ.get("OPENAI_API_KEY"))
prompt = hub.pull("hwchase17/react")
try:
agent = create_react_agent(llm, tools, prompt) # ❌ CRASHES: multiply has no .name attribute
executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools)
result = executor.invoke({"input": "Add 5 and 3"})
print(result)
except AttributeError as e:
print(f"Error: {e}") import os
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool # ✅ CORRECT: Use @tool decorator
from langchain.agents import create_react_agent, AgentExecutor
from langchain import hub
# ✅ FIXED: Using @tool decorator with proper docstring and type hints
@tool
def add(a: int, b: int) -> int:
"""Add two numbers together.
Args:
a: first number
b: second number
Returns:
sum of a and b
"""
return a + b
# ✅ FIXED: Using @tool decorator for multiply as well
@tool
def multiply(x: int, y: int) -> int:
"""Multiply two numbers.
Args:
x: first number
y: second number
Returns:
product of x and y
"""
return x * y
tools = [add, multiply] # ✅ Now both are proper tool objects with .name, .description, .invoke()
llm = ChatOpenAI(model="gpt-4o-mini", api_key=os.environ.get("OPENAI_API_KEY"))
prompt = hub.pull("hwchase17/react")
# ✅ WORKS: All tools have required attributes
agent = create_react_agent(llm, tools, prompt)
executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools)
result = executor.invoke({"input": "Add 5 and 3, then multiply by 2"})
print(f"Result: {result['output']}") Workaround
If you cannot immediately migrate to @tool decorator, wrap your old Tool objects in a custom class that implements __getattr__ to provide missing attributes, or use a compatibility layer: `tool.name = tool.name or 'default'; tool.description = tool.description or ''` before passing to create_react_agent. However, this is fragile: migrate to @tool as soon as possible.
Prevention
Always use @tool decorator from langchain_core.tools for new agents (2026+). Add type hints to all function parameters and return values. Validate tool setup before agent creation: `for t in tools: assert hasattr(t, 'name'), assert hasattr(t, 'invoke')`. Use LangGraph StateGraph for complex multi-step agents where tool management is more explicit and testable.