Session 6· 05· 15 min

Tool Calling with @tool and bind_tools

What you'll learn
  • Declare a tool with the @tool decorator
  • Bind a tool to a model with bind_tools()
  • Inspect AIMessage.tool_calls to see the model's request

The shortest tool definition in Python

In Session 5 you defined tools as hand-written JSON schemas (15+ lines per tool). LangChain generates the schema from your Python function — type hints + docstring = full tool definition. Three lines instead of fifteen.

Raw SDK (Session 5)
lesson 11
  • Write a JSON schema dict by hand
  • Manually list properties, types, required
  • Repeat for every new tool
  • ~15 lines per tool
LangChain (here)
@tool decorator
  • Type-hint your function args
  • Write a clear docstring
  • @tool does the rest
  • ~3 lines per tool
scripts/05_tool_calling_basic.py
from langchain.tools import tool                        ①

@tool                                                        ②
def get_weather(location: str) -> str:                       ③
    """Get fake weather for a location."""                   ④
    return f"It is sunny in {location}."

model_with_tools = model.bind_tools([get_weather])           ⑤

response = model_with_tools.invoke("What's the weather in Boston?")
print(response.tool_calls)                                    ⑥
Import the @tool decorator.
@tool transforms the Python function into a LangChain tool.
Type hints define the argument schema — location: str becomes a required string field.
The docstring becomes the tool description — the model uses it to decide WHEN to call the tool.
bind_tools returns a NEW model instance that knows about the tool. You invoke THIS one.
tool_calls is a list of proposed function calls. The model did not execute anything.
$ python scripts/05_tool_calling_basic.py
The docstring IS the description
Your function's docstring becomes the tool description the model sees. Write it clearly. "Get fake weather for a location." is fine. "gw" is not.
Knowledge Check
How does LangChain build the tool schema from your function?
Code Check
What does bind_tools return?
Recap — what you just learned
  • @tool turns a Python function into a LangChain tool
  • Type hints + docstring become the schema + description
  • bind_tools returns a new tool-aware model
  • tool_calls on the response holds the model's function-call request
Next up: 06 — Tool Execution Loop