Implementation:Langchain ai Langchain BaseChatModel Invoke With Tools
| Knowledge Sources | |
|---|---|
| Domains | NLP, Tool_Use |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
Concrete tool for invoking chat models with tool schemas and receiving structured tool call responses provided by langchain-core.
Description
The BaseChatModel.invoke() method follows the same invocation pipeline (input preparation, caching, rate limiting, _generate) but when tools are bound, the _generate() method includes tool schemas in the API request. The provider's response is parsed into an AIMessage with a tool_calls attribute containing a list of ToolCall objects (each with name, args, and id fields).
Usage
Call invoke() on a model that has had bind_tools() applied. Check response.tool_calls to determine if the model wants to call tools.
Code Reference
Source Location
- Repository: langchain
- File: libs/core/langchain_core/language_models/chat_models.py (invoke); libs/partners/openai/langchain_openai/chat_models/base.py (_generate with tool processing)
- Lines: chat_models.py L389-437 (invoke); base.py L1379-1478 (_generate)
Signature
def invoke(
self,
input: LanguageModelInput,
config: RunnableConfig | None = None,
*,
stop: list[str] | None = None,
**kwargs: Any,
) -> AIMessage:
Import
from langchain_openai import ChatOpenAI
from langchain_core.messages import AIMessage
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| input | LanguageModelInput | Yes | User messages (tools are already bound via bind_tools) |
| stop | list[str] or None | No | Stop sequences |
Outputs
| Name | Type | Description |
|---|---|---|
| return | AIMessage | Message with tool_calls list containing ToolCall objects with name, args, and id |
Usage Examples
Checking for Tool Calls
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
@tool
def get_weather(city: str) -> str:
"""Get weather for a city."""
return f"Sunny in {city}"
llm = ChatOpenAI(model="gpt-4o-mini")
llm_with_tools = llm.bind_tools([get_weather])
response = llm_with_tools.invoke("What's the weather in London?")
if response.tool_calls:
for tc in response.tool_calls:
print(f"Tool: {tc['name']}, Args: {tc['args']}, ID: {tc['id']}")
# Tool: get_weather, Args: {'city': 'London'}, ID: call_abc123
else:
print(response.content)