By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
The @tool decorator reads a function's name, type hints and docstring and turns them into a JSON schema. The model never sees your code — it sees the schema, and it asks for a call when it thinks the schema matches the request.
from langchain.chat_models import init_chat_model
from langchain.tools import tool
@tool
def get_weather(city: str) -> str:
"""Return the current weather for a city.
Args:
city: City name, for example "Mumbai".
"""
# Replace this with a real API call in production.
return f"{city}: 31°C, humid, light rain at 17:00"
model = init_chat_model("openai:gpt-4.1-mini", temperature=0)
model_with_tools = model.bind_tools([get_weather])
reply = model_with_tools.invoke("Will it rain in Mumbai this evening?")
call = reply.tool_calls[0]
print(reply.tool_calls)
print(get_weather.invoke(call["args"]))bind_tools only tells the model which functions exist and what arguments they take. It does not run anything. The model replies with a request; your code — or the agent runtime — executes it and appends a ToolMessage with the result so the next model call can read it. That distinction is the whole safety model of a tool-using agent.
The schema is built from the function's signature. The tool name tells the model what the button does, the docstring tells it when to press it, and the argument descriptions tell it what to type. If either is vague, the model guesses — and a confident guess is worse than a clarification question.
lookup_order, not orders.order_id without a format invites guesses.ValueError before touching data.Key takeaways
@tool turns a typed, documented function into a schema the model can read.
bind_tools exposes schemas; it executes nothing.
The model returns tool_calls; the runtime validates and executes them, then appends a ToolMessage.
Design tool names, docstrings and validation for a stranger — the model is that stranger.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.