langchain 1.0.0a12__py3-none-any.whl → 1.0.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langchain/__init__.py +1 -1
- langchain/agents/__init__.py +7 -1
- langchain/agents/factory.py +722 -226
- langchain/agents/middleware/__init__.py +36 -9
- langchain/agents/middleware/_execution.py +388 -0
- langchain/agents/middleware/_redaction.py +350 -0
- langchain/agents/middleware/context_editing.py +46 -17
- langchain/agents/middleware/file_search.py +382 -0
- langchain/agents/middleware/human_in_the_loop.py +220 -173
- langchain/agents/middleware/model_call_limit.py +43 -10
- langchain/agents/middleware/model_fallback.py +79 -36
- langchain/agents/middleware/pii.py +68 -504
- langchain/agents/middleware/shell_tool.py +718 -0
- langchain/agents/middleware/summarization.py +2 -2
- langchain/agents/middleware/{planning.py → todo.py} +35 -16
- langchain/agents/middleware/tool_call_limit.py +308 -114
- langchain/agents/middleware/tool_emulator.py +200 -0
- langchain/agents/middleware/tool_retry.py +384 -0
- langchain/agents/middleware/tool_selection.py +25 -21
- langchain/agents/middleware/types.py +714 -257
- langchain/agents/structured_output.py +37 -27
- langchain/chat_models/__init__.py +7 -1
- langchain/chat_models/base.py +192 -190
- langchain/embeddings/__init__.py +13 -3
- langchain/embeddings/base.py +49 -29
- langchain/messages/__init__.py +50 -1
- langchain/tools/__init__.py +9 -7
- langchain/tools/tool_node.py +16 -1174
- langchain-1.0.4.dist-info/METADATA +92 -0
- langchain-1.0.4.dist-info/RECORD +34 -0
- langchain/_internal/__init__.py +0 -0
- langchain/_internal/_documents.py +0 -35
- langchain/_internal/_lazy_import.py +0 -35
- langchain/_internal/_prompts.py +0 -158
- langchain/_internal/_typing.py +0 -70
- langchain/_internal/_utils.py +0 -7
- langchain/agents/_internal/__init__.py +0 -1
- langchain/agents/_internal/_typing.py +0 -13
- langchain/agents/middleware/prompt_caching.py +0 -86
- langchain/documents/__init__.py +0 -7
- langchain/embeddings/cache.py +0 -361
- langchain/storage/__init__.py +0 -22
- langchain/storage/encoder_backed.py +0 -123
- langchain/storage/exceptions.py +0 -5
- langchain/storage/in_memory.py +0 -13
- langchain-1.0.0a12.dist-info/METADATA +0 -122
- langchain-1.0.0a12.dist-info/RECORD +0 -43
- {langchain-1.0.0a12.dist-info → langchain-1.0.4.dist-info}/WHEEL +0 -0
- {langchain-1.0.0a12.dist-info → langchain-1.0.4.dist-info}/licenses/LICENSE +0 -0
langchain/tools/tool_node.py
CHANGED
|
@@ -1,1178 +1,20 @@
|
|
|
1
|
-
"""
|
|
1
|
+
"""Utils file included for backwards compat imports."""
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
The module implements several key design patterns:
|
|
9
|
-
- Parallel execution of multiple tool calls for efficiency
|
|
10
|
-
- Robust error handling with customizable error messages
|
|
11
|
-
- State injection for tools that need access to graph state
|
|
12
|
-
- Store injection for tools that need persistent storage
|
|
13
|
-
- Command-based state updates for advanced control flow
|
|
14
|
-
|
|
15
|
-
Key Components:
|
|
16
|
-
ToolNode: Main class for executing tools in LangGraph workflows
|
|
17
|
-
InjectedState: Annotation for injecting graph state into tools
|
|
18
|
-
InjectedStore: Annotation for injecting persistent store into tools
|
|
19
|
-
tools_condition: Utility function for conditional routing based on tool calls
|
|
20
|
-
|
|
21
|
-
Typical Usage:
|
|
22
|
-
```python
|
|
23
|
-
from langchain_core.tools import tool
|
|
24
|
-
from langchain.tools import ToolNode
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
@tool
|
|
28
|
-
def my_tool(x: int) -> str:
|
|
29
|
-
return f"Result: {x}"
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
tool_node = ToolNode([my_tool])
|
|
33
|
-
```
|
|
34
|
-
"""
|
|
35
|
-
|
|
36
|
-
from __future__ import annotations
|
|
37
|
-
|
|
38
|
-
import asyncio
|
|
39
|
-
import inspect
|
|
40
|
-
import json
|
|
41
|
-
from copy import copy, deepcopy
|
|
42
|
-
from dataclasses import replace
|
|
43
|
-
from types import UnionType
|
|
44
|
-
from typing import (
|
|
45
|
-
TYPE_CHECKING,
|
|
46
|
-
Annotated,
|
|
47
|
-
Any,
|
|
48
|
-
Literal,
|
|
49
|
-
Optional,
|
|
50
|
-
Union,
|
|
51
|
-
cast,
|
|
52
|
-
get_args,
|
|
53
|
-
get_origin,
|
|
54
|
-
get_type_hints,
|
|
55
|
-
)
|
|
56
|
-
|
|
57
|
-
from langchain_core.messages import (
|
|
58
|
-
AIMessage,
|
|
59
|
-
AnyMessage,
|
|
60
|
-
RemoveMessage,
|
|
61
|
-
ToolCall,
|
|
62
|
-
ToolMessage,
|
|
63
|
-
convert_to_messages,
|
|
3
|
+
from langgraph.prebuilt import InjectedState, InjectedStore, ToolRuntime
|
|
4
|
+
from langgraph.prebuilt.tool_node import (
|
|
5
|
+
ToolCallRequest,
|
|
6
|
+
ToolCallWithContext,
|
|
7
|
+
ToolCallWrapper,
|
|
64
8
|
)
|
|
65
|
-
from
|
|
66
|
-
|
|
67
|
-
get_executor_for_config,
|
|
9
|
+
from langgraph.prebuilt.tool_node import (
|
|
10
|
+
ToolNode as _ToolNode, # noqa: F401
|
|
68
11
|
)
|
|
69
|
-
from langchain_core.tools import BaseTool, InjectedToolArg
|
|
70
|
-
from langchain_core.tools import tool as create_tool
|
|
71
|
-
from langchain_core.tools.base import (
|
|
72
|
-
TOOL_MESSAGE_BLOCK_TYPES,
|
|
73
|
-
get_all_basemodel_annotations,
|
|
74
|
-
)
|
|
75
|
-
from langgraph._internal._runnable import RunnableCallable
|
|
76
|
-
from langgraph.errors import GraphBubbleUp
|
|
77
|
-
from langgraph.graph.message import REMOVE_ALL_MESSAGES
|
|
78
|
-
from langgraph.types import Command, Send
|
|
79
|
-
from pydantic import BaseModel, ValidationError
|
|
80
|
-
|
|
81
|
-
if TYPE_CHECKING:
|
|
82
|
-
from collections.abc import Callable, Sequence
|
|
83
|
-
|
|
84
|
-
from langchain_core.runnables import RunnableConfig
|
|
85
|
-
from langgraph.store.base import BaseStore
|
|
86
|
-
|
|
87
|
-
INVALID_TOOL_NAME_ERROR_TEMPLATE = (
|
|
88
|
-
"Error: {requested_tool} is not a valid tool, try one of [{available_tools}]."
|
|
89
|
-
)
|
|
90
|
-
TOOL_CALL_ERROR_TEMPLATE = "Error: {error}\n Please fix your mistakes."
|
|
91
|
-
TOOL_EXECUTION_ERROR_TEMPLATE = (
|
|
92
|
-
"Error executing tool '{tool_name}' with kwargs {tool_kwargs} with error:\n"
|
|
93
|
-
" {error}\n"
|
|
94
|
-
" Please fix the error and try again."
|
|
95
|
-
)
|
|
96
|
-
TOOL_INVOCATION_ERROR_TEMPLATE = (
|
|
97
|
-
"Error invoking tool '{tool_name}' with kwargs {tool_kwargs} with error:\n"
|
|
98
|
-
" {error}\n"
|
|
99
|
-
" Please fix the error and try again."
|
|
100
|
-
)
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
def msg_content_output(output: Any) -> str | list[dict]:
|
|
104
|
-
"""Convert tool output to valid message content format.
|
|
105
|
-
|
|
106
|
-
LangChain ToolMessages accept either string content or a list of content blocks.
|
|
107
|
-
This function ensures tool outputs are properly formatted for message consumption
|
|
108
|
-
by attempting to preserve structured data when possible, falling back to JSON
|
|
109
|
-
serialization or string conversion.
|
|
110
|
-
|
|
111
|
-
Args:
|
|
112
|
-
output: The raw output from a tool execution. Can be any type.
|
|
113
|
-
|
|
114
|
-
Returns:
|
|
115
|
-
Either a string representation of the output or a list of content blocks
|
|
116
|
-
if the output is already in the correct format for structured content.
|
|
117
|
-
|
|
118
|
-
Note:
|
|
119
|
-
This function prioritizes backward compatibility by defaulting to JSON
|
|
120
|
-
serialization rather than supporting all possible message content formats.
|
|
121
|
-
"""
|
|
122
|
-
if isinstance(output, str) or (
|
|
123
|
-
isinstance(output, list)
|
|
124
|
-
and all(isinstance(x, dict) and x.get("type") in TOOL_MESSAGE_BLOCK_TYPES for x in output)
|
|
125
|
-
):
|
|
126
|
-
return output
|
|
127
|
-
# Technically a list of strings is also valid message content, but it's
|
|
128
|
-
# not currently well tested that all chat models support this.
|
|
129
|
-
# And for backwards compatibility we want to make sure we don't break
|
|
130
|
-
# any existing ToolNode usage.
|
|
131
|
-
try:
|
|
132
|
-
return json.dumps(output, ensure_ascii=False)
|
|
133
|
-
except Exception: # noqa: BLE001
|
|
134
|
-
return str(output)
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
class ToolInvocationError(Exception):
|
|
138
|
-
"""Exception raised when a tool invocation fails due to invalid arguments."""
|
|
139
|
-
|
|
140
|
-
def __init__(
|
|
141
|
-
self, tool_name: str, source: ValidationError, tool_kwargs: dict[str, Any]
|
|
142
|
-
) -> None:
|
|
143
|
-
"""Initialize the ToolInvocationError.
|
|
144
|
-
|
|
145
|
-
Args:
|
|
146
|
-
tool_name: The name of the tool that failed.
|
|
147
|
-
source: The exception that occurred.
|
|
148
|
-
tool_kwargs: The keyword arguments that were passed to the tool.
|
|
149
|
-
"""
|
|
150
|
-
self.message = TOOL_INVOCATION_ERROR_TEMPLATE.format(
|
|
151
|
-
tool_name=tool_name, tool_kwargs=tool_kwargs, error=source
|
|
152
|
-
)
|
|
153
|
-
self.tool_name = tool_name
|
|
154
|
-
self.tool_kwargs = tool_kwargs
|
|
155
|
-
self.source = source
|
|
156
|
-
super().__init__(self.message)
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
def _default_handle_tool_errors(e: Exception) -> str:
|
|
160
|
-
"""Default error handler for tool errors.
|
|
161
|
-
|
|
162
|
-
If the tool is a tool invocation error, return its message.
|
|
163
|
-
Otherwise, raise the error.
|
|
164
|
-
"""
|
|
165
|
-
if isinstance(e, ToolInvocationError):
|
|
166
|
-
return e.message
|
|
167
|
-
raise e
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
def _handle_tool_error(
|
|
171
|
-
e: Exception,
|
|
172
|
-
*,
|
|
173
|
-
flag: bool | str | Callable[..., str] | type[Exception] | tuple[type[Exception], ...],
|
|
174
|
-
) -> str:
|
|
175
|
-
"""Generate error message content based on exception handling configuration.
|
|
176
|
-
|
|
177
|
-
This function centralizes error message generation logic, supporting different
|
|
178
|
-
error handling strategies configured via the ToolNode's handle_tool_errors
|
|
179
|
-
parameter.
|
|
180
|
-
|
|
181
|
-
Args:
|
|
182
|
-
e: The exception that occurred during tool execution.
|
|
183
|
-
flag: Configuration for how to handle the error. Can be:
|
|
184
|
-
- bool: If True, use default error template
|
|
185
|
-
- str: Use this string as the error message
|
|
186
|
-
- Callable: Call this function with the exception to get error message
|
|
187
|
-
- tuple: Not used in this context (handled by caller)
|
|
188
|
-
|
|
189
|
-
Returns:
|
|
190
|
-
A string containing the error message to include in the ToolMessage.
|
|
191
|
-
|
|
192
|
-
Raises:
|
|
193
|
-
ValueError: If flag is not one of the supported types.
|
|
194
|
-
|
|
195
|
-
Note:
|
|
196
|
-
The tuple case is handled by the caller through exception type checking,
|
|
197
|
-
not by this function directly.
|
|
198
|
-
"""
|
|
199
|
-
if isinstance(flag, (bool, tuple)) or (isinstance(flag, type) and issubclass(flag, Exception)):
|
|
200
|
-
content = TOOL_CALL_ERROR_TEMPLATE.format(error=repr(e))
|
|
201
|
-
elif isinstance(flag, str):
|
|
202
|
-
content = flag
|
|
203
|
-
elif callable(flag):
|
|
204
|
-
content = flag(e) # type: ignore [assignment, call-arg]
|
|
205
|
-
else:
|
|
206
|
-
msg = (
|
|
207
|
-
f"Got unexpected type of `handle_tool_error`. Expected bool, str "
|
|
208
|
-
f"or callable. Received: {flag}"
|
|
209
|
-
)
|
|
210
|
-
raise ValueError(msg)
|
|
211
|
-
return content
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
def _infer_handled_types(handler: Callable[..., str]) -> tuple[type[Exception], ...]:
|
|
215
|
-
"""Infer exception types handled by a custom error handler function.
|
|
216
|
-
|
|
217
|
-
This function analyzes the type annotations of a custom error handler to determine
|
|
218
|
-
which exception types it's designed to handle. This enables type-safe error handling
|
|
219
|
-
where only specific exceptions are caught and processed by the handler.
|
|
220
|
-
|
|
221
|
-
Args:
|
|
222
|
-
handler: A callable that takes an exception and returns an error message string.
|
|
223
|
-
The first parameter (after self/cls if present) should be type-annotated
|
|
224
|
-
with the exception type(s) to handle.
|
|
225
|
-
|
|
226
|
-
Returns:
|
|
227
|
-
A tuple of exception types that the handler can process. Returns (Exception,)
|
|
228
|
-
if no specific type information is available for backward compatibility.
|
|
229
|
-
|
|
230
|
-
Raises:
|
|
231
|
-
ValueError: If the handler's annotation contains non-Exception types or
|
|
232
|
-
if Union types contain non-Exception types.
|
|
233
|
-
|
|
234
|
-
Note:
|
|
235
|
-
This function supports both single exception types and Union types for
|
|
236
|
-
handlers that need to handle multiple exception types differently.
|
|
237
|
-
"""
|
|
238
|
-
sig = inspect.signature(handler)
|
|
239
|
-
params = list(sig.parameters.values())
|
|
240
|
-
if params:
|
|
241
|
-
# If it's a method, the first argument is typically 'self' or 'cls'
|
|
242
|
-
if params[0].name in ["self", "cls"] and len(params) == 2:
|
|
243
|
-
first_param = params[1]
|
|
244
|
-
else:
|
|
245
|
-
first_param = params[0]
|
|
246
|
-
|
|
247
|
-
type_hints = get_type_hints(handler)
|
|
248
|
-
if first_param.name in type_hints:
|
|
249
|
-
origin = get_origin(first_param.annotation)
|
|
250
|
-
if origin in [Union, UnionType]:
|
|
251
|
-
args = get_args(first_param.annotation)
|
|
252
|
-
if all(issubclass(arg, Exception) for arg in args):
|
|
253
|
-
return tuple(args)
|
|
254
|
-
msg = (
|
|
255
|
-
"All types in the error handler error annotation must be "
|
|
256
|
-
"Exception types. For example, "
|
|
257
|
-
"`def custom_handler(e: Union[ValueError, TypeError])`. "
|
|
258
|
-
f"Got '{first_param.annotation}' instead."
|
|
259
|
-
)
|
|
260
|
-
raise ValueError(msg)
|
|
261
|
-
|
|
262
|
-
exception_type = type_hints[first_param.name]
|
|
263
|
-
if Exception in exception_type.__mro__:
|
|
264
|
-
return (exception_type,)
|
|
265
|
-
msg = (
|
|
266
|
-
f"Arbitrary types are not supported in the error handler "
|
|
267
|
-
f"signature. Please annotate the error with either a "
|
|
268
|
-
f"specific Exception type or a union of Exception types. "
|
|
269
|
-
"For example, `def custom_handler(e: ValueError)` or "
|
|
270
|
-
"`def custom_handler(e: Union[ValueError, TypeError])`. "
|
|
271
|
-
f"Got '{exception_type}' instead."
|
|
272
|
-
)
|
|
273
|
-
raise ValueError(msg)
|
|
274
|
-
|
|
275
|
-
# If no type information is available, return (Exception,)
|
|
276
|
-
# for backwards compatibility.
|
|
277
|
-
return (Exception,)
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
class ToolNode(RunnableCallable):
|
|
281
|
-
"""A node for executing tools in LangGraph workflows.
|
|
282
|
-
|
|
283
|
-
Handles tool execution patterns including function calls, state injection,
|
|
284
|
-
persistent storage, and control flow. Manages parallel execution,
|
|
285
|
-
error handling.
|
|
286
|
-
|
|
287
|
-
Input Formats:
|
|
288
|
-
1. Graph state with `messages` key that has a list of messages:
|
|
289
|
-
- Common representation for agentic workflows
|
|
290
|
-
- Supports custom messages key via ``messages_key`` parameter
|
|
291
|
-
|
|
292
|
-
2. **Message List**: ``[AIMessage(..., tool_calls=[...])]``
|
|
293
|
-
- List of messages with tool calls in the last AIMessage
|
|
294
|
-
|
|
295
|
-
3. **Direct Tool Calls**: ``[{"name": "tool", "args": {...}, "id": "1", "type": "tool_call"}]``
|
|
296
|
-
- Bypasses message parsing for direct tool execution
|
|
297
|
-
- For programmatic tool invocation and testing
|
|
298
|
-
|
|
299
|
-
Output Formats:
|
|
300
|
-
Output format depends on input type and tool behavior:
|
|
301
|
-
|
|
302
|
-
**For Regular tools**:
|
|
303
|
-
- Dict input → ``{"messages": [ToolMessage(...)]}``
|
|
304
|
-
- List input → ``[ToolMessage(...)]``
|
|
305
|
-
|
|
306
|
-
**For Command tools**:
|
|
307
|
-
- Returns ``[Command(...)]`` or mixed list with regular tool outputs
|
|
308
|
-
- Commands can update state, trigger navigation, or send messages
|
|
309
|
-
|
|
310
|
-
Args:
|
|
311
|
-
tools: A sequence of tools that can be invoked by this node. Supports:
|
|
312
|
-
- **BaseTool instances**: Tools with schemas and metadata
|
|
313
|
-
- **Plain functions**: Automatically converted to tools with inferred schemas
|
|
314
|
-
name: The name identifier for this node in the graph. Used for debugging
|
|
315
|
-
and visualization. Defaults to "tools".
|
|
316
|
-
tags: Optional metadata tags to associate with the node for filtering
|
|
317
|
-
and organization. Defaults to None.
|
|
318
|
-
handle_tool_errors: Configuration for error handling during tool execution.
|
|
319
|
-
Supports multiple strategies:
|
|
320
|
-
|
|
321
|
-
- **True**: Catch all errors and return a ToolMessage with the default
|
|
322
|
-
error template containing the exception details.
|
|
323
|
-
- **str**: Catch all errors and return a ToolMessage with this custom
|
|
324
|
-
error message string.
|
|
325
|
-
- **type[Exception]**: Only catch exceptions with the specified type and
|
|
326
|
-
return the default error message for it.
|
|
327
|
-
- **tuple[type[Exception], ...]**: Only catch exceptions with the specified
|
|
328
|
-
types and return default error messages for them.
|
|
329
|
-
- **Callable[..., str]**: Catch exceptions matching the callable's signature
|
|
330
|
-
and return the string result of calling it with the exception.
|
|
331
|
-
- **False**: Disable error handling entirely, allowing exceptions to
|
|
332
|
-
propagate.
|
|
333
|
-
|
|
334
|
-
Defaults to a callable that:
|
|
335
|
-
- catches tool invocation errors (due to invalid arguments provided by the model) and returns a descriptive error message
|
|
336
|
-
- ignores tool execution errors (they will be re-raised)
|
|
337
|
-
|
|
338
|
-
messages_key: The key in the state dictionary that contains the message list.
|
|
339
|
-
This same key will be used for the output ToolMessages.
|
|
340
|
-
Defaults to "messages".
|
|
341
|
-
Allows custom state schemas with different message field names.
|
|
342
|
-
|
|
343
|
-
Examples:
|
|
344
|
-
Basic usage:
|
|
345
|
-
|
|
346
|
-
```python
|
|
347
|
-
from langchain.tools import ToolNode
|
|
348
|
-
from langchain_core.tools import tool
|
|
349
|
-
|
|
350
|
-
@tool
|
|
351
|
-
def calculator(a: int, b: int) -> int:
|
|
352
|
-
\"\"\"Add two numbers.\"\"\"
|
|
353
|
-
return a + b
|
|
354
|
-
|
|
355
|
-
tool_node = ToolNode([calculator])
|
|
356
|
-
```
|
|
357
|
-
|
|
358
|
-
State injection:
|
|
359
|
-
|
|
360
|
-
```python
|
|
361
|
-
from typing_extensions import Annotated
|
|
362
|
-
from langchain.tools import InjectedState
|
|
363
|
-
|
|
364
|
-
@tool
|
|
365
|
-
def context_tool(query: str, state: Annotated[dict, InjectedState]) -> str:
|
|
366
|
-
\"\"\"Some tool that uses state.\"\"\"
|
|
367
|
-
return f"Query: {query}, Messages: {len(state['messages'])}"
|
|
368
|
-
|
|
369
|
-
tool_node = ToolNode([context_tool])
|
|
370
|
-
```
|
|
371
|
-
|
|
372
|
-
Error handling:
|
|
373
|
-
|
|
374
|
-
```python
|
|
375
|
-
def handle_errors(e: ValueError) -> str:
|
|
376
|
-
return "Invalid input provided"
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
tool_node = ToolNode([my_tool], handle_tool_errors=handle_errors)
|
|
380
|
-
```
|
|
381
|
-
""" # noqa: E501
|
|
382
|
-
|
|
383
|
-
name: str = "tools"
|
|
384
|
-
|
|
385
|
-
def __init__(
|
|
386
|
-
self,
|
|
387
|
-
tools: Sequence[BaseTool | Callable],
|
|
388
|
-
*,
|
|
389
|
-
name: str = "tools",
|
|
390
|
-
tags: list[str] | None = None,
|
|
391
|
-
handle_tool_errors: bool
|
|
392
|
-
| str
|
|
393
|
-
| Callable[..., str]
|
|
394
|
-
| type[Exception]
|
|
395
|
-
| tuple[type[Exception], ...] = _default_handle_tool_errors,
|
|
396
|
-
messages_key: str = "messages",
|
|
397
|
-
) -> None:
|
|
398
|
-
"""Initialize the ToolNode with the provided tools and configuration.
|
|
399
|
-
|
|
400
|
-
Args:
|
|
401
|
-
tools: Sequence of tools to make available for execution.
|
|
402
|
-
name: Node name for graph identification.
|
|
403
|
-
tags: Optional metadata tags.
|
|
404
|
-
handle_tool_errors: Error handling configuration.
|
|
405
|
-
messages_key: State key containing messages.
|
|
406
|
-
"""
|
|
407
|
-
super().__init__(self._func, self._afunc, name=name, tags=tags, trace=False)
|
|
408
|
-
self._tools_by_name: dict[str, BaseTool] = {}
|
|
409
|
-
self._tool_to_state_args: dict[str, dict[str, str | None]] = {}
|
|
410
|
-
self._tool_to_store_arg: dict[str, str | None] = {}
|
|
411
|
-
self._handle_tool_errors = handle_tool_errors
|
|
412
|
-
self._messages_key = messages_key
|
|
413
|
-
for tool in tools:
|
|
414
|
-
if not isinstance(tool, BaseTool):
|
|
415
|
-
tool_ = create_tool(cast("type[BaseTool]", tool))
|
|
416
|
-
else:
|
|
417
|
-
tool_ = tool
|
|
418
|
-
self._tools_by_name[tool_.name] = tool_
|
|
419
|
-
self._tool_to_state_args[tool_.name] = _get_state_args(tool_)
|
|
420
|
-
self._tool_to_store_arg[tool_.name] = _get_store_arg(tool_)
|
|
421
|
-
|
|
422
|
-
@property
|
|
423
|
-
def tools_by_name(self) -> dict[str, BaseTool]:
|
|
424
|
-
"""Mapping from tool name to BaseTool instance."""
|
|
425
|
-
return self._tools_by_name
|
|
426
|
-
|
|
427
|
-
def _func(
|
|
428
|
-
self,
|
|
429
|
-
input: list[AnyMessage] | dict[str, Any] | BaseModel,
|
|
430
|
-
config: RunnableConfig,
|
|
431
|
-
*,
|
|
432
|
-
store: Optional[BaseStore], # noqa: UP045
|
|
433
|
-
) -> Any:
|
|
434
|
-
tool_calls, input_type = self._parse_input(input, store)
|
|
435
|
-
config_list = get_config_list(config, len(tool_calls))
|
|
436
|
-
input_types = [input_type] * len(tool_calls)
|
|
437
|
-
with get_executor_for_config(config) as executor:
|
|
438
|
-
outputs = [*executor.map(self._run_one, tool_calls, input_types, config_list)]
|
|
439
|
-
|
|
440
|
-
return self._combine_tool_outputs(outputs, input_type)
|
|
441
|
-
|
|
442
|
-
async def _afunc(
|
|
443
|
-
self,
|
|
444
|
-
input: list[AnyMessage] | dict[str, Any] | BaseModel,
|
|
445
|
-
config: RunnableConfig,
|
|
446
|
-
*,
|
|
447
|
-
store: Optional[BaseStore], # noqa: UP045
|
|
448
|
-
) -> Any:
|
|
449
|
-
tool_calls, input_type = self._parse_input(input, store)
|
|
450
|
-
outputs = await asyncio.gather(
|
|
451
|
-
*(self._arun_one(call, input_type, config) for call in tool_calls)
|
|
452
|
-
)
|
|
453
|
-
|
|
454
|
-
return self._combine_tool_outputs(outputs, input_type)
|
|
455
|
-
|
|
456
|
-
def _combine_tool_outputs(
|
|
457
|
-
self,
|
|
458
|
-
outputs: list[ToolMessage | Command],
|
|
459
|
-
input_type: Literal["list", "dict", "tool_calls"],
|
|
460
|
-
) -> list[Command | list[ToolMessage] | dict[str, list[ToolMessage]]]:
|
|
461
|
-
# preserve existing behavior for non-command tool outputs for backwards
|
|
462
|
-
# compatibility
|
|
463
|
-
if not any(isinstance(output, Command) for output in outputs):
|
|
464
|
-
# TypedDict, pydantic, dataclass, etc. should all be able to load from dict
|
|
465
|
-
return outputs if input_type == "list" else {self._messages_key: outputs} # type: ignore[return-value, return-value]
|
|
466
|
-
|
|
467
|
-
# LangGraph will automatically handle list of Command and non-command node
|
|
468
|
-
# updates
|
|
469
|
-
combined_outputs: list[Command | list[ToolMessage] | dict[str, list[ToolMessage]]] = []
|
|
470
|
-
|
|
471
|
-
# combine all parent commands with goto into a single parent command
|
|
472
|
-
parent_command: Command | None = None
|
|
473
|
-
for output in outputs:
|
|
474
|
-
if isinstance(output, Command):
|
|
475
|
-
if (
|
|
476
|
-
output.graph is Command.PARENT
|
|
477
|
-
and isinstance(output.goto, list)
|
|
478
|
-
and all(isinstance(send, Send) for send in output.goto)
|
|
479
|
-
):
|
|
480
|
-
if parent_command:
|
|
481
|
-
parent_command = replace(
|
|
482
|
-
parent_command,
|
|
483
|
-
goto=cast("list[Send]", parent_command.goto) + output.goto,
|
|
484
|
-
)
|
|
485
|
-
else:
|
|
486
|
-
parent_command = Command(graph=Command.PARENT, goto=output.goto)
|
|
487
|
-
else:
|
|
488
|
-
combined_outputs.append(output)
|
|
489
|
-
else:
|
|
490
|
-
combined_outputs.append(
|
|
491
|
-
[output] if input_type == "list" else {self._messages_key: [output]}
|
|
492
|
-
)
|
|
493
|
-
|
|
494
|
-
if parent_command:
|
|
495
|
-
combined_outputs.append(parent_command)
|
|
496
|
-
return combined_outputs
|
|
497
|
-
|
|
498
|
-
def _run_one(
|
|
499
|
-
self,
|
|
500
|
-
call: ToolCall,
|
|
501
|
-
input_type: Literal["list", "dict", "tool_calls"],
|
|
502
|
-
config: RunnableConfig,
|
|
503
|
-
) -> ToolMessage | Command:
|
|
504
|
-
"""Run a single tool call synchronously."""
|
|
505
|
-
if invalid_tool_message := self._validate_tool_call(call):
|
|
506
|
-
return invalid_tool_message
|
|
507
|
-
|
|
508
|
-
try:
|
|
509
|
-
call_args = {**call, "type": "tool_call"}
|
|
510
|
-
tool = self.tools_by_name[call["name"]]
|
|
511
|
-
|
|
512
|
-
try:
|
|
513
|
-
response = tool.invoke(call_args, config)
|
|
514
|
-
except ValidationError as exc:
|
|
515
|
-
raise ToolInvocationError(call["name"], exc, call["args"]) from exc
|
|
516
|
-
|
|
517
|
-
# GraphInterrupt is a special exception that will always be raised.
|
|
518
|
-
# It can be triggered in the following scenarios,
|
|
519
|
-
# Where GraphInterrupt(GraphBubbleUp) is raised from an `interrupt` invocation
|
|
520
|
-
# most commonly:
|
|
521
|
-
# (1) a GraphInterrupt is raised inside a tool
|
|
522
|
-
# (2) a GraphInterrupt is raised inside a graph node for a graph called as a tool
|
|
523
|
-
# (3) a GraphInterrupt is raised when a subgraph is interrupted inside a graph
|
|
524
|
-
# called as a tool
|
|
525
|
-
# (2 and 3 can happen in a "supervisor w/ tools" multi-agent architecture)
|
|
526
|
-
except GraphBubbleUp:
|
|
527
|
-
raise
|
|
528
|
-
except Exception as e:
|
|
529
|
-
handled_types: tuple[type[Exception], ...]
|
|
530
|
-
if isinstance(self._handle_tool_errors, type) and issubclass(
|
|
531
|
-
self._handle_tool_errors, Exception
|
|
532
|
-
):
|
|
533
|
-
handled_types = (self._handle_tool_errors,)
|
|
534
|
-
elif isinstance(self._handle_tool_errors, tuple):
|
|
535
|
-
handled_types = self._handle_tool_errors
|
|
536
|
-
elif callable(self._handle_tool_errors) and not isinstance(
|
|
537
|
-
self._handle_tool_errors, type
|
|
538
|
-
):
|
|
539
|
-
handled_types = _infer_handled_types(self._handle_tool_errors)
|
|
540
|
-
else:
|
|
541
|
-
# default behavior is catching all exceptions
|
|
542
|
-
handled_types = (Exception,)
|
|
543
|
-
|
|
544
|
-
# Unhandled
|
|
545
|
-
if not self._handle_tool_errors or not isinstance(e, handled_types):
|
|
546
|
-
raise
|
|
547
|
-
# Handled
|
|
548
|
-
content = _handle_tool_error(e, flag=self._handle_tool_errors)
|
|
549
|
-
return ToolMessage(
|
|
550
|
-
content=content,
|
|
551
|
-
name=call["name"],
|
|
552
|
-
tool_call_id=call["id"],
|
|
553
|
-
status="error",
|
|
554
|
-
)
|
|
555
|
-
|
|
556
|
-
if isinstance(response, Command):
|
|
557
|
-
return self._validate_tool_command(response, call, input_type)
|
|
558
|
-
if isinstance(response, ToolMessage):
|
|
559
|
-
response.content = cast("str | list", msg_content_output(response.content))
|
|
560
|
-
return response
|
|
561
|
-
msg = f"Tool {call['name']} returned unexpected type: {type(response)}"
|
|
562
|
-
raise TypeError(msg)
|
|
563
|
-
|
|
564
|
-
async def _arun_one(
|
|
565
|
-
self,
|
|
566
|
-
call: ToolCall,
|
|
567
|
-
input_type: Literal["list", "dict", "tool_calls"],
|
|
568
|
-
config: RunnableConfig,
|
|
569
|
-
) -> ToolMessage | Command:
|
|
570
|
-
"""Run a single tool call asynchronously."""
|
|
571
|
-
if invalid_tool_message := self._validate_tool_call(call):
|
|
572
|
-
return invalid_tool_message
|
|
573
|
-
|
|
574
|
-
try:
|
|
575
|
-
call_args = {**call, "type": "tool_call"}
|
|
576
|
-
tool = self.tools_by_name[call["name"]]
|
|
577
|
-
|
|
578
|
-
try:
|
|
579
|
-
response = await tool.ainvoke(call_args, config)
|
|
580
|
-
except ValidationError as exc:
|
|
581
|
-
raise ToolInvocationError(call["name"], exc, call["args"]) from exc
|
|
582
|
-
|
|
583
|
-
# GraphInterrupt is a special exception that will always be raised.
|
|
584
|
-
# It can be triggered in the following scenarios,
|
|
585
|
-
# Where GraphInterrupt(GraphBubbleUp) is raised from an `interrupt` invocation
|
|
586
|
-
# most commonly:
|
|
587
|
-
# (1) a GraphInterrupt is raised inside a tool
|
|
588
|
-
# (2) a GraphInterrupt is raised inside a graph node for a graph called as a tool
|
|
589
|
-
# (3) a GraphInterrupt is raised when a subgraph is interrupted inside a graph
|
|
590
|
-
# called as a tool
|
|
591
|
-
# (2 and 3 can happen in a "supervisor w/ tools" multi-agent architecture)
|
|
592
|
-
except GraphBubbleUp:
|
|
593
|
-
raise
|
|
594
|
-
except Exception as e:
|
|
595
|
-
handled_types: tuple[type[Exception], ...]
|
|
596
|
-
if isinstance(self._handle_tool_errors, type) and issubclass(
|
|
597
|
-
self._handle_tool_errors, Exception
|
|
598
|
-
):
|
|
599
|
-
handled_types = (self._handle_tool_errors,)
|
|
600
|
-
elif isinstance(self._handle_tool_errors, tuple):
|
|
601
|
-
handled_types = self._handle_tool_errors
|
|
602
|
-
elif callable(self._handle_tool_errors) and not isinstance(
|
|
603
|
-
self._handle_tool_errors, type
|
|
604
|
-
):
|
|
605
|
-
handled_types = _infer_handled_types(self._handle_tool_errors)
|
|
606
|
-
else:
|
|
607
|
-
# default behavior is catching all exceptions
|
|
608
|
-
handled_types = (Exception,)
|
|
609
|
-
|
|
610
|
-
# Unhandled
|
|
611
|
-
if not self._handle_tool_errors or not isinstance(e, handled_types):
|
|
612
|
-
raise
|
|
613
|
-
# Handled
|
|
614
|
-
content = _handle_tool_error(e, flag=self._handle_tool_errors)
|
|
615
|
-
|
|
616
|
-
return ToolMessage(
|
|
617
|
-
content=content,
|
|
618
|
-
name=call["name"],
|
|
619
|
-
tool_call_id=call["id"],
|
|
620
|
-
status="error",
|
|
621
|
-
)
|
|
622
|
-
|
|
623
|
-
if isinstance(response, Command):
|
|
624
|
-
return self._validate_tool_command(response, call, input_type)
|
|
625
|
-
if isinstance(response, ToolMessage):
|
|
626
|
-
response.content = cast("str | list", msg_content_output(response.content))
|
|
627
|
-
return response
|
|
628
|
-
msg = f"Tool {call['name']} returned unexpected type: {type(response)}"
|
|
629
|
-
raise TypeError(msg)
|
|
630
|
-
|
|
631
|
-
def _parse_input(
|
|
632
|
-
self,
|
|
633
|
-
input: list[AnyMessage] | dict[str, Any] | BaseModel,
|
|
634
|
-
store: BaseStore | None,
|
|
635
|
-
) -> tuple[list[ToolCall], Literal["list", "dict", "tool_calls"]]:
|
|
636
|
-
input_type: Literal["list", "dict", "tool_calls"]
|
|
637
|
-
if isinstance(input, list):
|
|
638
|
-
if isinstance(input[-1], dict) and input[-1].get("type") == "tool_call":
|
|
639
|
-
input_type = "tool_calls"
|
|
640
|
-
tool_calls = cast("list[ToolCall]", input)
|
|
641
|
-
return tool_calls, input_type
|
|
642
|
-
input_type = "list"
|
|
643
|
-
messages = input
|
|
644
|
-
elif isinstance(input, dict) and (messages := input.get(self._messages_key, [])):
|
|
645
|
-
input_type = "dict"
|
|
646
|
-
elif messages := getattr(input, self._messages_key, []):
|
|
647
|
-
# Assume dataclass-like state that can coerce from dict
|
|
648
|
-
input_type = "dict"
|
|
649
|
-
else:
|
|
650
|
-
msg = "No message found in input"
|
|
651
|
-
raise ValueError(msg)
|
|
652
|
-
|
|
653
|
-
try:
|
|
654
|
-
latest_ai_message = next(m for m in reversed(messages) if isinstance(m, AIMessage))
|
|
655
|
-
except StopIteration:
|
|
656
|
-
msg = "No AIMessage found in input"
|
|
657
|
-
raise ValueError(msg)
|
|
658
|
-
|
|
659
|
-
tool_calls = [
|
|
660
|
-
self.inject_tool_args(call, input, store) for call in latest_ai_message.tool_calls
|
|
661
|
-
]
|
|
662
|
-
return tool_calls, input_type
|
|
663
|
-
|
|
664
|
-
def _validate_tool_call(self, call: ToolCall) -> ToolMessage | None:
|
|
665
|
-
requested_tool = call["name"]
|
|
666
|
-
if requested_tool not in self.tools_by_name:
|
|
667
|
-
all_tool_names = list(self.tools_by_name.keys())
|
|
668
|
-
content = INVALID_TOOL_NAME_ERROR_TEMPLATE.format(
|
|
669
|
-
requested_tool=requested_tool,
|
|
670
|
-
available_tools=", ".join(all_tool_names),
|
|
671
|
-
)
|
|
672
|
-
return ToolMessage(
|
|
673
|
-
content, name=requested_tool, tool_call_id=call["id"], status="error"
|
|
674
|
-
)
|
|
675
|
-
return None
|
|
676
|
-
|
|
677
|
-
def _inject_state(
|
|
678
|
-
self,
|
|
679
|
-
tool_call: ToolCall,
|
|
680
|
-
input: list[AnyMessage] | dict[str, Any] | BaseModel,
|
|
681
|
-
) -> ToolCall:
|
|
682
|
-
state_args = self._tool_to_state_args[tool_call["name"]]
|
|
683
|
-
if state_args and isinstance(input, list):
|
|
684
|
-
required_fields = list(state_args.values())
|
|
685
|
-
if (
|
|
686
|
-
len(required_fields) == 1 and required_fields[0] == self._messages_key
|
|
687
|
-
) or required_fields[0] is None:
|
|
688
|
-
input = {self._messages_key: input}
|
|
689
|
-
else:
|
|
690
|
-
err_msg = (
|
|
691
|
-
f"Invalid input to ToolNode. Tool {tool_call['name']} requires "
|
|
692
|
-
f"graph state dict as input."
|
|
693
|
-
)
|
|
694
|
-
if any(state_field for state_field in state_args.values()):
|
|
695
|
-
required_fields_str = ", ".join(f for f in required_fields if f)
|
|
696
|
-
err_msg += f" State should contain fields {required_fields_str}."
|
|
697
|
-
raise ValueError(err_msg)
|
|
698
|
-
|
|
699
|
-
if isinstance(input, dict):
|
|
700
|
-
tool_state_args = {
|
|
701
|
-
tool_arg: input[state_field] if state_field else input
|
|
702
|
-
for tool_arg, state_field in state_args.items()
|
|
703
|
-
}
|
|
704
|
-
else:
|
|
705
|
-
tool_state_args = {
|
|
706
|
-
tool_arg: getattr(input, state_field) if state_field else input
|
|
707
|
-
for tool_arg, state_field in state_args.items()
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
tool_call["args"] = {
|
|
711
|
-
**tool_call["args"],
|
|
712
|
-
**tool_state_args,
|
|
713
|
-
}
|
|
714
|
-
return tool_call
|
|
715
|
-
|
|
716
|
-
def _inject_store(self, tool_call: ToolCall, store: BaseStore | None) -> ToolCall:
|
|
717
|
-
store_arg = self._tool_to_store_arg[tool_call["name"]]
|
|
718
|
-
if not store_arg:
|
|
719
|
-
return tool_call
|
|
720
|
-
|
|
721
|
-
if store is None:
|
|
722
|
-
msg = (
|
|
723
|
-
"Cannot inject store into tools with InjectedStore annotations - "
|
|
724
|
-
"please compile your graph with a store."
|
|
725
|
-
)
|
|
726
|
-
raise ValueError(msg)
|
|
727
|
-
|
|
728
|
-
tool_call["args"] = {
|
|
729
|
-
**tool_call["args"],
|
|
730
|
-
store_arg: store,
|
|
731
|
-
}
|
|
732
|
-
return tool_call
|
|
733
|
-
|
|
734
|
-
def inject_tool_args(
|
|
735
|
-
self,
|
|
736
|
-
tool_call: ToolCall,
|
|
737
|
-
input: list[AnyMessage] | dict[str, Any] | BaseModel,
|
|
738
|
-
store: BaseStore | None,
|
|
739
|
-
) -> ToolCall:
|
|
740
|
-
"""Inject graph state and store into tool call arguments.
|
|
741
|
-
|
|
742
|
-
This method enables tools to access graph context that should not be controlled
|
|
743
|
-
by the model. Tools can declare dependencies on graph state or persistent storage
|
|
744
|
-
using InjectedState and InjectedStore annotations. This method automatically
|
|
745
|
-
identifies these dependencies and injects the appropriate values.
|
|
746
|
-
|
|
747
|
-
The injection process preserves the original tool call structure while adding
|
|
748
|
-
the necessary context arguments. This allows tools to be both model-callable
|
|
749
|
-
and context-aware without exposing internal state management to the model.
|
|
750
|
-
|
|
751
|
-
Args:
|
|
752
|
-
tool_call: The tool call dictionary to augment with injected arguments.
|
|
753
|
-
Must contain 'name', 'args', 'id', and 'type' fields.
|
|
754
|
-
input: The current graph state to inject into tools requiring state access.
|
|
755
|
-
Can be a message list, state dictionary, or BaseModel instance.
|
|
756
|
-
store: The persistent store instance to inject into tools requiring storage.
|
|
757
|
-
Will be None if no store is configured for the graph.
|
|
758
|
-
|
|
759
|
-
Returns:
|
|
760
|
-
A new ToolCall dictionary with the same structure as the input but with
|
|
761
|
-
additional arguments injected based on the tool's annotation requirements.
|
|
762
|
-
|
|
763
|
-
Raises:
|
|
764
|
-
ValueError: If a tool requires store injection but no store is provided,
|
|
765
|
-
or if state injection requirements cannot be satisfied.
|
|
766
|
-
|
|
767
|
-
Note:
|
|
768
|
-
This method is automatically called during tool execution but can also
|
|
769
|
-
be used manually when working with the Send API or custom routing logic.
|
|
770
|
-
The injection is performed on a copy of the tool call to avoid mutating
|
|
771
|
-
the original.
|
|
772
|
-
"""
|
|
773
|
-
if tool_call["name"] not in self.tools_by_name:
|
|
774
|
-
return tool_call
|
|
775
|
-
|
|
776
|
-
tool_call_copy: ToolCall = copy(tool_call)
|
|
777
|
-
tool_call_with_state = self._inject_state(tool_call_copy, input)
|
|
778
|
-
return self._inject_store(tool_call_with_state, store)
|
|
779
|
-
|
|
780
|
-
def _validate_tool_command(
|
|
781
|
-
self,
|
|
782
|
-
command: Command,
|
|
783
|
-
call: ToolCall,
|
|
784
|
-
input_type: Literal["list", "dict", "tool_calls"],
|
|
785
|
-
) -> Command:
|
|
786
|
-
if isinstance(command.update, dict):
|
|
787
|
-
# input type is dict when ToolNode is invoked with a dict input
|
|
788
|
-
# (e.g. {"messages": [AIMessage(..., tool_calls=[...])]})
|
|
789
|
-
if input_type not in ("dict", "tool_calls"):
|
|
790
|
-
msg = (
|
|
791
|
-
"Tools can provide a dict in Command.update only when using dict "
|
|
792
|
-
f"with '{self._messages_key}' key as ToolNode input, "
|
|
793
|
-
f"got: {command.update} for tool '{call['name']}'"
|
|
794
|
-
)
|
|
795
|
-
raise ValueError(msg)
|
|
796
|
-
|
|
797
|
-
updated_command = deepcopy(command)
|
|
798
|
-
state_update = cast("dict[str, Any]", updated_command.update) or {}
|
|
799
|
-
messages_update = state_update.get(self._messages_key, [])
|
|
800
|
-
elif isinstance(command.update, list):
|
|
801
|
-
# Input type is list when ToolNode is invoked with a list input
|
|
802
|
-
# (e.g. [AIMessage(..., tool_calls=[...])])
|
|
803
|
-
if input_type != "list":
|
|
804
|
-
msg = (
|
|
805
|
-
"Tools can provide a list of messages in Command.update "
|
|
806
|
-
"only when using list of messages as ToolNode input, "
|
|
807
|
-
f"got: {command.update} for tool '{call['name']}'"
|
|
808
|
-
)
|
|
809
|
-
raise ValueError(msg)
|
|
810
|
-
|
|
811
|
-
updated_command = deepcopy(command)
|
|
812
|
-
messages_update = updated_command.update
|
|
813
|
-
else:
|
|
814
|
-
return command
|
|
815
|
-
|
|
816
|
-
# convert to message objects if updates are in a dict format
|
|
817
|
-
messages_update = convert_to_messages(messages_update)
|
|
818
|
-
|
|
819
|
-
# no validation needed if all messages are being removed
|
|
820
|
-
if messages_update == [RemoveMessage(id=REMOVE_ALL_MESSAGES)]:
|
|
821
|
-
return updated_command
|
|
822
|
-
|
|
823
|
-
has_matching_tool_message = False
|
|
824
|
-
for message in messages_update:
|
|
825
|
-
if not isinstance(message, ToolMessage):
|
|
826
|
-
continue
|
|
827
|
-
|
|
828
|
-
if message.tool_call_id == call["id"]:
|
|
829
|
-
message.name = call["name"]
|
|
830
|
-
has_matching_tool_message = True
|
|
831
|
-
|
|
832
|
-
# validate that we always have a ToolMessage matching the tool call in
|
|
833
|
-
# Command.update if command is sent to the CURRENT graph
|
|
834
|
-
if updated_command.graph is None and not has_matching_tool_message:
|
|
835
|
-
example_update = (
|
|
836
|
-
'`Command(update={"messages": '
|
|
837
|
-
'[ToolMessage("Success", tool_call_id=tool_call_id), ...]}, ...)`'
|
|
838
|
-
if input_type == "dict"
|
|
839
|
-
else "`Command(update="
|
|
840
|
-
'[ToolMessage("Success", tool_call_id=tool_call_id), ...], ...)`'
|
|
841
|
-
)
|
|
842
|
-
msg = (
|
|
843
|
-
"Expected to have a matching ToolMessage in Command.update "
|
|
844
|
-
f"for tool '{call['name']}', got: {messages_update}. "
|
|
845
|
-
"Every tool call (LLM requesting to call a tool) "
|
|
846
|
-
"in the message history MUST have a corresponding ToolMessage. "
|
|
847
|
-
f"You can fix it by modifying the tool to return {example_update}."
|
|
848
|
-
)
|
|
849
|
-
raise ValueError(msg)
|
|
850
|
-
return updated_command
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
def tools_condition(
|
|
854
|
-
state: list[AnyMessage] | dict[str, Any] | BaseModel,
|
|
855
|
-
messages_key: str = "messages",
|
|
856
|
-
) -> Literal["tools", "__end__"]:
|
|
857
|
-
"""Conditional routing function for tool-calling workflows.
|
|
858
|
-
|
|
859
|
-
This utility function implements the standard conditional logic for ReAct-style
|
|
860
|
-
agents: if the last AI message contains tool calls, route to the tool execution
|
|
861
|
-
node; otherwise, end the workflow. This pattern is fundamental to most tool-calling
|
|
862
|
-
agent architectures.
|
|
863
|
-
|
|
864
|
-
The function handles multiple state formats commonly used in LangGraph applications,
|
|
865
|
-
making it flexible for different graph designs while maintaining consistent behavior.
|
|
866
|
-
|
|
867
|
-
Args:
|
|
868
|
-
state: The current graph state to examine for tool calls. Supported formats:
|
|
869
|
-
- Dictionary containing a messages key (for StateGraph)
|
|
870
|
-
- BaseModel instance with a messages attribute
|
|
871
|
-
messages_key: The key or attribute name containing the message list in the state.
|
|
872
|
-
This allows customization for graphs using different state schemas.
|
|
873
|
-
Defaults to "messages".
|
|
874
|
-
|
|
875
|
-
Returns:
|
|
876
|
-
Either "tools" if tool calls are present in the last AI message, or "__end__"
|
|
877
|
-
to terminate the workflow. These are the standard routing destinations for
|
|
878
|
-
tool-calling conditional edges.
|
|
879
|
-
|
|
880
|
-
Raises:
|
|
881
|
-
ValueError: If no messages can be found in the provided state format.
|
|
882
|
-
|
|
883
|
-
Example:
|
|
884
|
-
Basic usage in a ReAct agent:
|
|
885
|
-
|
|
886
|
-
```python
|
|
887
|
-
from langgraph.graph import StateGraph
|
|
888
|
-
from langchain.tools import ToolNode
|
|
889
|
-
from langchain.tools.tool_node import tools_condition
|
|
890
|
-
from typing_extensions import TypedDict
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
class State(TypedDict):
|
|
894
|
-
messages: list
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
graph = StateGraph(State)
|
|
898
|
-
graph.add_node("llm", call_model)
|
|
899
|
-
graph.add_node("tools", ToolNode([my_tool]))
|
|
900
|
-
graph.add_conditional_edges(
|
|
901
|
-
"llm",
|
|
902
|
-
tools_condition, # Routes to "tools" or "__end__"
|
|
903
|
-
{"tools": "tools", "__end__": "__end__"},
|
|
904
|
-
)
|
|
905
|
-
```
|
|
906
|
-
|
|
907
|
-
Custom messages key:
|
|
908
|
-
|
|
909
|
-
```python
|
|
910
|
-
def custom_condition(state):
|
|
911
|
-
return tools_condition(state, messages_key="chat_history")
|
|
912
|
-
```
|
|
913
|
-
|
|
914
|
-
Note:
|
|
915
|
-
This function is designed to work seamlessly with ToolNode and standard
|
|
916
|
-
LangGraph patterns. It expects the last message to be an AIMessage when
|
|
917
|
-
tool calls are present, which is the standard output format for tool-calling
|
|
918
|
-
language models.
|
|
919
|
-
"""
|
|
920
|
-
if isinstance(state, list):
|
|
921
|
-
ai_message = state[-1]
|
|
922
|
-
elif (isinstance(state, dict) and (messages := state.get(messages_key, []))) or (
|
|
923
|
-
messages := getattr(state, messages_key, [])
|
|
924
|
-
):
|
|
925
|
-
ai_message = messages[-1]
|
|
926
|
-
else:
|
|
927
|
-
msg = f"No messages found in input state to tool_edge: {state}"
|
|
928
|
-
raise ValueError(msg)
|
|
929
|
-
if hasattr(ai_message, "tool_calls") and len(ai_message.tool_calls) > 0:
|
|
930
|
-
return "tools"
|
|
931
|
-
return "__end__"
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
class InjectedState(InjectedToolArg):
|
|
935
|
-
"""Annotation for injecting graph state into tool arguments.
|
|
936
|
-
|
|
937
|
-
This annotation enables tools to access graph state without exposing state
|
|
938
|
-
management details to the language model. Tools annotated with InjectedState
|
|
939
|
-
receive state data automatically during execution while remaining invisible
|
|
940
|
-
to the model's tool-calling interface.
|
|
941
|
-
|
|
942
|
-
Args:
|
|
943
|
-
field: Optional key to extract from the state dictionary. If None, the entire
|
|
944
|
-
state is injected. If specified, only that field's value is injected.
|
|
945
|
-
This allows tools to request specific state components rather than
|
|
946
|
-
processing the full state structure.
|
|
947
|
-
|
|
948
|
-
Example:
|
|
949
|
-
```python
|
|
950
|
-
from typing import List
|
|
951
|
-
from typing_extensions import Annotated, TypedDict
|
|
952
|
-
|
|
953
|
-
from langchain_core.messages import BaseMessage, AIMessage
|
|
954
|
-
from langchain.tools import InjectedState, ToolNode, tool
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
class AgentState(TypedDict):
|
|
958
|
-
messages: List[BaseMessage]
|
|
959
|
-
foo: str
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
@tool
|
|
963
|
-
def state_tool(x: int, state: Annotated[dict, InjectedState]) -> str:
|
|
964
|
-
'''Do something with state.'''
|
|
965
|
-
if len(state["messages"]) > 2:
|
|
966
|
-
return state["foo"] + str(x)
|
|
967
|
-
else:
|
|
968
|
-
return "not enough messages"
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
@tool
|
|
972
|
-
def foo_tool(x: int, foo: Annotated[str, InjectedState("foo")]) -> str:
|
|
973
|
-
'''Do something else with state.'''
|
|
974
|
-
return foo + str(x + 1)
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
node = ToolNode([state_tool, foo_tool])
|
|
978
|
-
|
|
979
|
-
tool_call1 = {"name": "state_tool", "args": {"x": 1}, "id": "1", "type": "tool_call"}
|
|
980
|
-
tool_call2 = {"name": "foo_tool", "args": {"x": 1}, "id": "2", "type": "tool_call"}
|
|
981
|
-
state = {
|
|
982
|
-
"messages": [AIMessage("", tool_calls=[tool_call1, tool_call2])],
|
|
983
|
-
"foo": "bar",
|
|
984
|
-
}
|
|
985
|
-
node.invoke(state)
|
|
986
|
-
```
|
|
987
|
-
|
|
988
|
-
```pycon
|
|
989
|
-
[
|
|
990
|
-
ToolMessage(content="not enough messages", name="state_tool", tool_call_id="1"),
|
|
991
|
-
ToolMessage(content="bar2", name="foo_tool", tool_call_id="2"),
|
|
992
|
-
]
|
|
993
|
-
```
|
|
994
|
-
|
|
995
|
-
Note:
|
|
996
|
-
- InjectedState arguments are automatically excluded from tool schemas
|
|
997
|
-
presented to language models
|
|
998
|
-
- ToolNode handles the injection process during execution
|
|
999
|
-
- Tools can mix regular arguments (controlled by the model) with injected
|
|
1000
|
-
arguments (controlled by the system)
|
|
1001
|
-
- State injection occurs after the model generates tool calls but before
|
|
1002
|
-
tool execution
|
|
1003
|
-
"""
|
|
1004
|
-
|
|
1005
|
-
def __init__(self, field: str | None = None) -> None:
|
|
1006
|
-
"""Initialize the InjectedState annotation."""
|
|
1007
|
-
self.field = field
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
class InjectedStore(InjectedToolArg):
|
|
1011
|
-
"""Annotation for injecting persistent store into tool arguments.
|
|
1012
|
-
|
|
1013
|
-
This annotation enables tools to access LangGraph's persistent storage system
|
|
1014
|
-
without exposing storage details to the language model. Tools annotated with
|
|
1015
|
-
InjectedStore receive the store instance automatically during execution while
|
|
1016
|
-
remaining invisible to the model's tool-calling interface.
|
|
1017
|
-
|
|
1018
|
-
The store provides persistent, cross-session data storage that tools can use
|
|
1019
|
-
for maintaining context, user preferences, or any other data that needs to
|
|
1020
|
-
persist beyond individual workflow executions.
|
|
1021
|
-
|
|
1022
|
-
!!! warning
|
|
1023
|
-
`InjectedStore` annotation requires `langchain-core >= 0.3.8`
|
|
1024
|
-
|
|
1025
|
-
Example:
|
|
1026
|
-
```python
|
|
1027
|
-
from typing_extensions import Annotated
|
|
1028
|
-
from langgraph.store.memory import InMemoryStore
|
|
1029
|
-
from langchain.tools import InjectedStore, ToolNode, tool
|
|
1030
|
-
|
|
1031
|
-
@tool
|
|
1032
|
-
def save_preference(
|
|
1033
|
-
key: str,
|
|
1034
|
-
value: str,
|
|
1035
|
-
store: Annotated[Any, InjectedStore()]
|
|
1036
|
-
) -> str:
|
|
1037
|
-
\"\"\"Save user preference to persistent storage.\"\"\"
|
|
1038
|
-
store.put(("preferences",), key, value)
|
|
1039
|
-
return f"Saved {key} = {value}"
|
|
1040
|
-
|
|
1041
|
-
@tool
|
|
1042
|
-
def get_preference(
|
|
1043
|
-
key: str,
|
|
1044
|
-
store: Annotated[Any, InjectedStore()]
|
|
1045
|
-
) -> str:
|
|
1046
|
-
\"\"\"Retrieve user preference from persistent storage.\"\"\"
|
|
1047
|
-
result = store.get(("preferences",), key)
|
|
1048
|
-
return result.value if result else "Not found"
|
|
1049
|
-
```
|
|
1050
|
-
|
|
1051
|
-
Usage with ToolNode and graph compilation:
|
|
1052
|
-
|
|
1053
|
-
```python
|
|
1054
|
-
from langgraph.graph import StateGraph
|
|
1055
|
-
from langgraph.store.memory import InMemoryStore
|
|
1056
|
-
|
|
1057
|
-
store = InMemoryStore()
|
|
1058
|
-
tool_node = ToolNode([save_preference, get_preference])
|
|
1059
|
-
|
|
1060
|
-
graph = StateGraph(State)
|
|
1061
|
-
graph.add_node("tools", tool_node)
|
|
1062
|
-
compiled_graph = graph.compile(store=store) # Store is injected automatically
|
|
1063
|
-
```
|
|
1064
|
-
|
|
1065
|
-
Cross-session persistence:
|
|
1066
|
-
|
|
1067
|
-
```python
|
|
1068
|
-
# First session
|
|
1069
|
-
result1 = graph.invoke({"messages": [HumanMessage("Save my favorite color as blue")]})
|
|
1070
|
-
|
|
1071
|
-
# Later session - data persists
|
|
1072
|
-
result2 = graph.invoke({"messages": [HumanMessage("What's my favorite color?")]})
|
|
1073
|
-
```
|
|
1074
|
-
|
|
1075
|
-
Note:
|
|
1076
|
-
- InjectedStore arguments are automatically excluded from tool schemas
|
|
1077
|
-
presented to language models
|
|
1078
|
-
- The store instance is automatically injected by ToolNode during execution
|
|
1079
|
-
- Tools can access namespaced storage using the store's get/put methods
|
|
1080
|
-
- Store injection requires the graph to be compiled with a store instance
|
|
1081
|
-
- Multiple tools can share the same store instance for data consistency
|
|
1082
|
-
"""
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
def _is_injection(type_arg: Any, injection_type: type[InjectedState | InjectedStore]) -> bool:
|
|
1086
|
-
"""Check if a type argument represents an injection annotation.
|
|
1087
|
-
|
|
1088
|
-
This utility function determines whether a type annotation indicates that
|
|
1089
|
-
an argument should be injected with state or store data. It handles both
|
|
1090
|
-
direct annotations and nested annotations within Union or Annotated types.
|
|
1091
|
-
|
|
1092
|
-
Args:
|
|
1093
|
-
type_arg: The type argument to check for injection annotations.
|
|
1094
|
-
injection_type: The injection type to look for (InjectedState or InjectedStore).
|
|
1095
|
-
|
|
1096
|
-
Returns:
|
|
1097
|
-
True if the type argument contains the specified injection annotation.
|
|
1098
|
-
"""
|
|
1099
|
-
if isinstance(type_arg, injection_type) or (
|
|
1100
|
-
isinstance(type_arg, type) and issubclass(type_arg, injection_type)
|
|
1101
|
-
):
|
|
1102
|
-
return True
|
|
1103
|
-
origin_ = get_origin(type_arg)
|
|
1104
|
-
if origin_ is Union or origin_ is Annotated:
|
|
1105
|
-
return any(_is_injection(ta, injection_type) for ta in get_args(type_arg))
|
|
1106
|
-
return False
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
def _get_state_args(tool: BaseTool) -> dict[str, str | None]:
|
|
1110
|
-
"""Extract state injection mappings from tool annotations.
|
|
1111
|
-
|
|
1112
|
-
This function analyzes a tool's input schema to identify arguments that should
|
|
1113
|
-
be injected with graph state. It processes InjectedState annotations to build
|
|
1114
|
-
a mapping of tool argument names to state field names.
|
|
1115
|
-
|
|
1116
|
-
Args:
|
|
1117
|
-
tool: The tool to analyze for state injection requirements.
|
|
1118
|
-
|
|
1119
|
-
Returns:
|
|
1120
|
-
A dictionary mapping tool argument names to state field names. If a field
|
|
1121
|
-
name is None, the entire state should be injected for that argument.
|
|
1122
|
-
"""
|
|
1123
|
-
full_schema = tool.get_input_schema()
|
|
1124
|
-
tool_args_to_state_fields: dict = {}
|
|
1125
|
-
|
|
1126
|
-
for name, type_ in get_all_basemodel_annotations(full_schema).items():
|
|
1127
|
-
injections = [
|
|
1128
|
-
type_arg for type_arg in get_args(type_) if _is_injection(type_arg, InjectedState)
|
|
1129
|
-
]
|
|
1130
|
-
if len(injections) > 1:
|
|
1131
|
-
msg = (
|
|
1132
|
-
"A tool argument should not be annotated with InjectedState more than "
|
|
1133
|
-
f"once. Received arg {name} with annotations {injections}."
|
|
1134
|
-
)
|
|
1135
|
-
raise ValueError(msg)
|
|
1136
|
-
if len(injections) == 1:
|
|
1137
|
-
injection = injections[0]
|
|
1138
|
-
if isinstance(injection, InjectedState) and injection.field:
|
|
1139
|
-
tool_args_to_state_fields[name] = injection.field
|
|
1140
|
-
else:
|
|
1141
|
-
tool_args_to_state_fields[name] = None
|
|
1142
|
-
else:
|
|
1143
|
-
pass
|
|
1144
|
-
return tool_args_to_state_fields
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
def _get_store_arg(tool: BaseTool) -> str | None:
|
|
1148
|
-
"""Extract store injection argument from tool annotations.
|
|
1149
|
-
|
|
1150
|
-
This function analyzes a tool's input schema to identify the argument that
|
|
1151
|
-
should be injected with the graph store. Only one store argument is supported
|
|
1152
|
-
per tool.
|
|
1153
|
-
|
|
1154
|
-
Args:
|
|
1155
|
-
tool: The tool to analyze for store injection requirements.
|
|
1156
|
-
|
|
1157
|
-
Returns:
|
|
1158
|
-
The name of the argument that should receive the store injection, or None
|
|
1159
|
-
if no store injection is required.
|
|
1160
|
-
|
|
1161
|
-
Raises:
|
|
1162
|
-
ValueError: If a tool argument has multiple InjectedStore annotations.
|
|
1163
|
-
"""
|
|
1164
|
-
full_schema = tool.get_input_schema()
|
|
1165
|
-
for name, type_ in get_all_basemodel_annotations(full_schema).items():
|
|
1166
|
-
injections = [
|
|
1167
|
-
type_arg for type_arg in get_args(type_) if _is_injection(type_arg, InjectedStore)
|
|
1168
|
-
]
|
|
1169
|
-
if len(injections) > 1:
|
|
1170
|
-
msg = (
|
|
1171
|
-
"A tool argument should not be annotated with InjectedStore more than "
|
|
1172
|
-
f"once. Received arg {name} with annotations {injections}."
|
|
1173
|
-
)
|
|
1174
|
-
raise ValueError(msg)
|
|
1175
|
-
if len(injections) == 1:
|
|
1176
|
-
return name
|
|
1177
12
|
|
|
1178
|
-
|
|
13
|
+
__all__ = [
|
|
14
|
+
"InjectedState",
|
|
15
|
+
"InjectedStore",
|
|
16
|
+
"ToolCallRequest",
|
|
17
|
+
"ToolCallWithContext",
|
|
18
|
+
"ToolCallWrapper",
|
|
19
|
+
"ToolRuntime",
|
|
20
|
+
]
|