langchain 1.0.1__py3-none-any.whl → 1.0.3__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.

Potentially problematic release.


This version of langchain might be problematic. Click here for more details.

@@ -1,1702 +1,20 @@
1
- """Tool execution node for LangGraph workflows.
1
+ """Utils file included for backwards compat imports."""
2
2
 
3
- This module provides prebuilt functionality for executing tools in LangGraph.
4
-
5
- Tools are functions that models can call to interact with external systems,
6
- APIs, databases, or perform computations.
7
-
8
- The module implements design patterns for:
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 collections.abc import Awaitable, Callable
42
- from copy import copy, deepcopy
43
- from dataclasses import dataclass, replace
44
- from types import UnionType
45
- from typing import (
46
- TYPE_CHECKING,
47
- Annotated,
48
- Any,
49
- Generic,
50
- Literal,
51
- TypedDict,
52
- Union,
53
- cast,
54
- get_args,
55
- get_origin,
56
- get_type_hints,
57
- )
58
-
59
- from langchain_core.messages import (
60
- AIMessage,
61
- AnyMessage,
62
- RemoveMessage,
63
- ToolCall,
64
- ToolMessage,
65
- convert_to_messages,
66
- )
67
- from langchain_core.runnables.config import (
68
- RunnableConfig,
69
- get_config_list,
70
- get_executor_for_config,
71
- )
72
- from langchain_core.tools import BaseTool, InjectedToolArg
73
- from langchain_core.tools import tool as create_tool
74
- from langchain_core.tools.base import (
75
- TOOL_MESSAGE_BLOCK_TYPES,
76
- ToolException,
77
- _DirectlyInjectedToolArg,
78
- get_all_basemodel_annotations,
79
- )
80
- from langgraph._internal._runnable import RunnableCallable
81
- from langgraph.errors import GraphBubbleUp
82
- from langgraph.graph.message import REMOVE_ALL_MESSAGES
83
- from langgraph.store.base import BaseStore # noqa: TC002
84
- from langgraph.types import Command, Send, StreamWriter
85
- from pydantic import BaseModel, ValidationError
86
- from typing_extensions import TypeVar, Unpack
87
-
88
- if TYPE_CHECKING:
89
- from collections.abc import Sequence
90
-
91
- from langgraph.runtime import Runtime
92
-
93
- # right now we use a dict as the default, can change this to AgentState, but depends
94
- # on if this lives in LangChain or LangGraph... ideally would have some typed
95
- # messages key
96
- StateT = TypeVar("StateT", default=dict)
97
- ContextT = TypeVar("ContextT", default=None)
98
-
99
- INVALID_TOOL_NAME_ERROR_TEMPLATE = (
100
- "Error: {requested_tool} is not a valid tool, try one of [{available_tools}]."
3
+ from langgraph.prebuilt import InjectedState, InjectedStore, ToolRuntime
4
+ from langgraph.prebuilt.tool_node import (
5
+ ToolCallRequest,
6
+ ToolCallWithContext,
7
+ ToolCallWrapper,
101
8
  )
102
- TOOL_CALL_ERROR_TEMPLATE = "Error: {error}\n Please fix your mistakes."
103
- TOOL_EXECUTION_ERROR_TEMPLATE = (
104
- "Error executing tool '{tool_name}' with kwargs {tool_kwargs} with error:\n"
105
- " {error}\n"
106
- " Please fix the error and try again."
9
+ from langgraph.prebuilt.tool_node import (
10
+ ToolNode as _ToolNode, # noqa: F401
107
11
  )
108
- TOOL_INVOCATION_ERROR_TEMPLATE = (
109
- "Error invoking tool '{tool_name}' with kwargs {tool_kwargs} with error:\n"
110
- " {error}\n"
111
- " Please fix the error and try again."
112
- )
113
-
114
-
115
- class _ToolCallRequestOverrides(TypedDict, total=False):
116
- """Possible overrides for ToolCallRequest.override() method."""
117
-
118
- tool_call: ToolCall
119
-
120
-
121
- @dataclass()
122
- class ToolCallRequest:
123
- """Tool execution request passed to tool call interceptors.
124
-
125
- Attributes:
126
- tool_call: Tool call dict with name, args, and id from model output.
127
- tool: BaseTool instance to be invoked, or None if tool is not
128
- registered with the `ToolNode`. When tool is `None`, interceptors can
129
- handle the request without validation. If the interceptor calls `execute()`,
130
- validation will occur and raise an error for unregistered tools.
131
- state: Agent state (`dict`, `list`, or `BaseModel`).
132
- runtime: LangGraph runtime context (optional, `None` if outside graph).
133
- """
134
-
135
- tool_call: ToolCall
136
- tool: BaseTool | None
137
- state: Any
138
- runtime: ToolRuntime
139
-
140
- def override(self, **overrides: Unpack[_ToolCallRequestOverrides]) -> ToolCallRequest:
141
- """Replace the request with a new request with the given overrides.
142
-
143
- Returns a new `ToolCallRequest` instance with the specified attributes replaced.
144
- This follows an immutable pattern, leaving the original request unchanged.
145
-
146
- Args:
147
- **overrides: Keyword arguments for attributes to override. Supported keys:
148
- - tool_call: Tool call dict with name, args, and id
149
-
150
- Returns:
151
- New ToolCallRequest instance with specified overrides applied.
152
-
153
- Examples:
154
- ```python
155
- # Modify tool call arguments without mutating original
156
- modified_call = {**request.tool_call, "args": {"value": 10}}
157
- new_request = request.override(tool_call=modified_call)
158
-
159
- # Override multiple attributes
160
- new_request = request.override(tool_call=modified_call, state=new_state)
161
- ```
162
- """
163
- return replace(self, **overrides)
164
-
165
-
166
- ToolCallWrapper = Callable[
167
- [ToolCallRequest, Callable[[ToolCallRequest], ToolMessage | Command]],
168
- ToolMessage | Command,
169
- ]
170
- """Wrapper for tool call execution with multi-call support.
171
-
172
- Wrapper receives:
173
- request: ToolCallRequest with tool_call, tool, state, and runtime.
174
- execute: Callable to execute the tool (CAN BE CALLED MULTIPLE TIMES).
175
12
 
176
- Returns:
177
- ToolMessage or Command (the final result).
178
-
179
- The execute callable can be invoked multiple times for retry logic,
180
- with potentially modified requests each time. Each call to execute
181
- is independent and stateless.
182
-
183
- !!! note
184
- When implementing middleware for `create_agent`, use
185
- `AgentMiddleware.wrap_tool_call` which provides properly typed
186
- state parameter for better type safety.
187
-
188
- Examples:
189
- Passthrough (execute once):
190
-
191
- def handler(request, execute):
192
- return execute(request)
193
-
194
- Modify request before execution:
195
-
196
- ```python
197
- def handler(request, execute):
198
- request.tool_call["args"]["value"] *= 2
199
- return execute(request)
200
- ```
201
-
202
- Retry on error (execute multiple times):
203
-
204
- ```python
205
- def handler(request, execute):
206
- for attempt in range(3):
207
- try:
208
- result = execute(request)
209
- if is_valid(result):
210
- return result
211
- except Exception:
212
- if attempt == 2:
213
- raise
214
- return result
215
- ```
216
-
217
- Conditional retry based on response:
218
-
219
- ```python
220
- def handler(request, execute):
221
- for attempt in range(3):
222
- result = execute(request)
223
- if isinstance(result, ToolMessage) and result.status != "error":
224
- return result
225
- if attempt < 2:
226
- continue
227
- return result
228
- ```
229
-
230
- Cache/short-circuit without calling execute:
231
-
232
- ```python
233
- def handler(request, execute):
234
- if cached := get_cache(request):
235
- return ToolMessage(content=cached, tool_call_id=request.tool_call["id"])
236
- result = execute(request)
237
- save_cache(request, result)
238
- return result
239
- ```
240
- """
241
-
242
- AsyncToolCallWrapper = Callable[
243
- [ToolCallRequest, Callable[[ToolCallRequest], Awaitable[ToolMessage | Command]]],
244
- Awaitable[ToolMessage | Command],
13
+ __all__ = [
14
+ "InjectedState",
15
+ "InjectedStore",
16
+ "ToolCallRequest",
17
+ "ToolCallWithContext",
18
+ "ToolCallWrapper",
19
+ "ToolRuntime",
245
20
  ]
246
- """Async wrapper for tool call execution with multi-call support."""
247
-
248
-
249
- class ToolCallWithContext(TypedDict):
250
- """ToolCall with additional context for graph state.
251
-
252
- This is an internal data structure meant to help the `ToolNode` accept
253
- tool calls with additional context (e.g. state) when dispatched using the
254
- Send API.
255
-
256
- The Send API is used in create_agent to distribute tool calls in parallel
257
- and support human-in-the-loop workflows where graph execution may be paused
258
- for an indefinite time.
259
- """
260
-
261
- tool_call: ToolCall
262
- __type: Literal["tool_call_with_context"]
263
- """Type to parameterize the payload.
264
-
265
- Using "__" as a prefix to be defensive against potential name collisions with
266
- regular user state.
267
- """
268
- state: Any
269
- """The state is provided as additional context."""
270
-
271
-
272
- def msg_content_output(output: Any) -> str | list[dict]:
273
- """Convert tool output to `ToolMessage` content format.
274
-
275
- Handles `str`, `list[dict]` (content blocks), and arbitrary objects by attempting
276
- JSON serialization with fallback to str().
277
-
278
- Args:
279
- output: Tool execution output of any type.
280
-
281
- Returns:
282
- String or list of content blocks suitable for `ToolMessage.content`.
283
- """
284
- if isinstance(output, str) or (
285
- isinstance(output, list)
286
- and all(isinstance(x, dict) and x.get("type") in TOOL_MESSAGE_BLOCK_TYPES for x in output)
287
- ):
288
- return output
289
- # Technically a list of strings is also valid message content, but it's
290
- # not currently well tested that all chat models support this.
291
- # And for backwards compatibility we want to make sure we don't break
292
- # any existing ToolNode usage.
293
- try:
294
- return json.dumps(output, ensure_ascii=False)
295
- except Exception: # noqa: BLE001
296
- return str(output)
297
-
298
-
299
- class ToolInvocationError(ToolException):
300
- """An error occurred while invoking a tool due to invalid arguments.
301
-
302
- This exception is only raised when invoking a tool using the `ToolNode`!
303
- """
304
-
305
- def __init__(
306
- self, tool_name: str, source: ValidationError, tool_kwargs: dict[str, Any]
307
- ) -> None:
308
- """Initialize the ToolInvocationError.
309
-
310
- Args:
311
- tool_name: The name of the tool that failed.
312
- source: The exception that occurred.
313
- tool_kwargs: The keyword arguments that were passed to the tool.
314
- """
315
- self.message = TOOL_INVOCATION_ERROR_TEMPLATE.format(
316
- tool_name=tool_name, tool_kwargs=tool_kwargs, error=source
317
- )
318
- self.tool_name = tool_name
319
- self.tool_kwargs = tool_kwargs
320
- self.source = source
321
- super().__init__(self.message)
322
-
323
-
324
- def _default_handle_tool_errors(e: Exception) -> str:
325
- """Default error handler for tool errors.
326
-
327
- If the tool is a tool invocation error, return its message.
328
- Otherwise, raise the error.
329
- """
330
- if isinstance(e, ToolInvocationError):
331
- return e.message
332
- raise e
333
-
334
-
335
- def _handle_tool_error(
336
- e: Exception,
337
- *,
338
- flag: bool | str | Callable[..., str] | type[Exception] | tuple[type[Exception], ...],
339
- ) -> str:
340
- """Generate error message content based on exception handling configuration.
341
-
342
- This function centralizes error message generation logic, supporting different
343
- error handling strategies configured via the `ToolNode`'s `handle_tool_errors`
344
- parameter.
345
-
346
- Args:
347
- e: The exception that occurred during tool execution.
348
- flag: Configuration for how to handle the error. Can be:
349
- - bool: If `True`, use default error template
350
- - str: Use this string as the error message
351
- - Callable: Call this function with the exception to get error message
352
- - tuple: Not used in this context (handled by caller)
353
-
354
- Returns:
355
- A string containing the error message to include in the `ToolMessage`.
356
-
357
- Raises:
358
- ValueError: If flag is not one of the supported types.
359
-
360
- !!! note
361
- The tuple case is handled by the caller through exception type checking,
362
- not by this function directly.
363
- """
364
- if isinstance(flag, (bool, tuple)) or (isinstance(flag, type) and issubclass(flag, Exception)):
365
- content = TOOL_CALL_ERROR_TEMPLATE.format(error=repr(e))
366
- elif isinstance(flag, str):
367
- content = flag
368
- elif callable(flag):
369
- content = flag(e) # type: ignore [assignment, call-arg]
370
- else:
371
- msg = (
372
- f"Got unexpected type of `handle_tool_error`. Expected bool, str "
373
- f"or callable. Received: {flag}"
374
- )
375
- raise ValueError(msg)
376
- return content
377
-
378
-
379
- def _infer_handled_types(handler: Callable[..., str]) -> tuple[type[Exception], ...]:
380
- """Infer exception types handled by a custom error handler function.
381
-
382
- This function analyzes the type annotations of a custom error handler to determine
383
- which exception types it's designed to handle. This enables type-safe error handling
384
- where only specific exceptions are caught and processed by the handler.
385
-
386
- Args:
387
- handler: A callable that takes an exception and returns an error message string.
388
- The first parameter (after self/cls if present) should be type-annotated
389
- with the exception type(s) to handle.
390
-
391
- Returns:
392
- A tuple of exception types that the handler can process. Returns (Exception,)
393
- if no specific type information is available for backward compatibility.
394
-
395
- Raises:
396
- ValueError: If the handler's annotation contains non-Exception types or
397
- if Union types contain non-Exception types.
398
-
399
- !!! note
400
- This function supports both single exception types and Union types for
401
- handlers that need to handle multiple exception types differently.
402
- """
403
- sig = inspect.signature(handler)
404
- params = list(sig.parameters.values())
405
- if params:
406
- # If it's a method, the first argument is typically 'self' or 'cls'
407
- if params[0].name in ["self", "cls"] and len(params) == 2:
408
- first_param = params[1]
409
- else:
410
- first_param = params[0]
411
-
412
- type_hints = get_type_hints(handler)
413
- if first_param.name in type_hints:
414
- origin = get_origin(first_param.annotation)
415
- if origin in [Union, UnionType]:
416
- args = get_args(first_param.annotation)
417
- if all(issubclass(arg, Exception) for arg in args):
418
- return tuple(args)
419
- msg = (
420
- "All types in the error handler error annotation must be "
421
- "Exception types. For example, "
422
- "`def custom_handler(e: Union[ValueError, TypeError])`. "
423
- f"Got '{first_param.annotation}' instead."
424
- )
425
- raise ValueError(msg)
426
-
427
- exception_type = type_hints[first_param.name]
428
- if Exception in exception_type.__mro__:
429
- return (exception_type,)
430
- msg = (
431
- f"Arbitrary types are not supported in the error handler "
432
- f"signature. Please annotate the error with either a "
433
- f"specific Exception type or a union of Exception types. "
434
- "For example, `def custom_handler(e: ValueError)` or "
435
- "`def custom_handler(e: Union[ValueError, TypeError])`. "
436
- f"Got '{exception_type}' instead."
437
- )
438
- raise ValueError(msg)
439
-
440
- # If no type information is available, return (Exception,)
441
- # for backwards compatibility.
442
- return (Exception,)
443
-
444
-
445
- class _ToolNode(RunnableCallable):
446
- """A node for executing tools in LangGraph workflows.
447
-
448
- Handles tool execution patterns including function calls, state injection,
449
- persistent storage, and control flow. Manages parallel execution,
450
- error handling.
451
-
452
- Input Formats:
453
- 1. Graph state with `messages` key that has a list of messages:
454
- - Common representation for agentic workflows
455
- - Supports custom messages key via `messages_key` parameter
456
-
457
- 2. **Message List**: `[AIMessage(..., tool_calls=[...])]`
458
- - List of messages with tool calls in the last AIMessage
459
-
460
- 3. **Direct Tool Calls**: `[{"name": "tool", "args": {...}, "id": "1", "type": "tool_call"}]`
461
- - Bypasses message parsing for direct tool execution
462
- - For programmatic tool invocation and testing
463
-
464
- Output Formats:
465
- Output format depends on input type and tool behavior:
466
-
467
- **For Regular tools**:
468
- - Dict input → `{"messages": [ToolMessage(...)]}`
469
- - List input → `[ToolMessage(...)]`
470
-
471
- **For Command tools**:
472
- - Returns `[Command(...)]` or mixed list with regular tool outputs
473
- - Commands can update state, trigger navigation, or send messages
474
-
475
- Args:
476
- tools: A sequence of tools that can be invoked by this node. Supports:
477
- - **BaseTool instances**: Tools with schemas and metadata
478
- - **Plain functions**: Automatically converted to tools with inferred schemas
479
- name: The name identifier for this node in the graph. Used for debugging
480
- and visualization. Defaults to "tools".
481
- tags: Optional metadata tags to associate with the node for filtering
482
- and organization. Defaults to `None`.
483
- handle_tool_errors: Configuration for error handling during tool execution.
484
- Supports multiple strategies:
485
-
486
- - **True**: Catch all errors and return a ToolMessage with the default
487
- error template containing the exception details.
488
- - **str**: Catch all errors and return a ToolMessage with this custom
489
- error message string.
490
- - **type[Exception]**: Only catch exceptions with the specified type and
491
- return the default error message for it.
492
- - **tuple[type[Exception], ...]**: Only catch exceptions with the specified
493
- types and return default error messages for them.
494
- - **Callable[..., str]**: Catch exceptions matching the callable's signature
495
- and return the string result of calling it with the exception.
496
- - **False**: Disable error handling entirely, allowing exceptions to
497
- propagate.
498
-
499
- Defaults to a callable that:
500
- - catches tool invocation errors (due to invalid arguments provided by the model) and returns a descriptive error message
501
- - ignores tool execution errors (they will be re-raised)
502
-
503
- messages_key: The key in the state dictionary that contains the message list.
504
- This same key will be used for the output `ToolMessage` objects.
505
- Defaults to "messages".
506
- Allows custom state schemas with different message field names.
507
-
508
- Examples:
509
- Basic usage:
510
-
511
- ```python
512
- from langchain.tools import ToolNode
513
- from langchain_core.tools import tool
514
-
515
- @tool
516
- def calculator(a: int, b: int) -> int:
517
- \"\"\"Add two numbers.\"\"\"
518
- return a + b
519
-
520
- tool_node = ToolNode([calculator])
521
- ```
522
-
523
- State injection:
524
-
525
- ```python
526
- from typing_extensions import Annotated
527
- from langchain.tools import InjectedState
528
-
529
- @tool
530
- def context_tool(query: str, state: Annotated[dict, InjectedState]) -> str:
531
- \"\"\"Some tool that uses state.\"\"\"
532
- return f"Query: {query}, Messages: {len(state['messages'])}"
533
-
534
- tool_node = ToolNode([context_tool])
535
- ```
536
-
537
- Error handling:
538
-
539
- ```python
540
- def handle_errors(e: ValueError) -> str:
541
- return "Invalid input provided"
542
-
543
-
544
- tool_node = ToolNode([my_tool], handle_tool_errors=handle_errors)
545
- ```
546
- """ # noqa: E501
547
-
548
- name: str = "tools"
549
-
550
- def __init__(
551
- self,
552
- tools: Sequence[BaseTool | Callable],
553
- *,
554
- name: str = "tools",
555
- tags: list[str] | None = None,
556
- handle_tool_errors: bool
557
- | str
558
- | Callable[..., str]
559
- | type[Exception]
560
- | tuple[type[Exception], ...] = _default_handle_tool_errors,
561
- messages_key: str = "messages",
562
- wrap_tool_call: ToolCallWrapper | None = None,
563
- awrap_tool_call: AsyncToolCallWrapper | None = None,
564
- ) -> None:
565
- """Initialize `ToolNode` with tools and configuration.
566
-
567
- Args:
568
- tools: Sequence of tools to make available for execution.
569
- name: Node name for graph identification.
570
- tags: Optional metadata tags.
571
- handle_tool_errors: Error handling configuration.
572
- messages_key: State key containing messages.
573
- wrap_tool_call: Sync wrapper function to intercept tool execution. Receives
574
- ToolCallRequest and execute callable, returns ToolMessage or Command.
575
- Enables retries, caching, request modification, and control flow.
576
- awrap_tool_call: Async wrapper function to intercept tool execution.
577
- If not provided, falls back to wrap_tool_call for async execution.
578
- """
579
- super().__init__(self._func, self._afunc, name=name, tags=tags, trace=False)
580
- self._tools_by_name: dict[str, BaseTool] = {}
581
- self._tool_to_state_args: dict[str, dict[str, str | None]] = {}
582
- self._tool_to_store_arg: dict[str, str | None] = {}
583
- self._tool_to_runtime_arg: dict[str, str | None] = {}
584
- self._handle_tool_errors = handle_tool_errors
585
- self._messages_key = messages_key
586
- self._wrap_tool_call = wrap_tool_call
587
- self._awrap_tool_call = awrap_tool_call
588
- for tool in tools:
589
- if not isinstance(tool, BaseTool):
590
- tool_ = create_tool(cast("type[BaseTool]", tool))
591
- else:
592
- tool_ = tool
593
- self._tools_by_name[tool_.name] = tool_
594
- self._tool_to_state_args[tool_.name] = _get_state_args(tool_)
595
- self._tool_to_store_arg[tool_.name] = _get_store_arg(tool_)
596
- self._tool_to_runtime_arg[tool_.name] = _get_runtime_arg(tool_)
597
-
598
- @property
599
- def tools_by_name(self) -> dict[str, BaseTool]:
600
- """Mapping from tool name to BaseTool instance."""
601
- return self._tools_by_name
602
-
603
- def _func(
604
- self,
605
- input: list[AnyMessage] | dict[str, Any] | BaseModel,
606
- config: RunnableConfig,
607
- runtime: Runtime,
608
- ) -> Any:
609
- tool_calls, input_type = self._parse_input(input)
610
- config_list = get_config_list(config, len(tool_calls))
611
-
612
- # Construct ToolRuntime instances at the top level for each tool call
613
- tool_runtimes = []
614
- for call, cfg in zip(tool_calls, config_list, strict=False):
615
- state = self._extract_state(input)
616
- tool_runtime = ToolRuntime(
617
- state=state,
618
- tool_call_id=call["id"],
619
- config=cfg,
620
- context=runtime.context,
621
- store=runtime.store,
622
- stream_writer=runtime.stream_writer,
623
- )
624
- tool_runtimes.append(tool_runtime)
625
-
626
- # Inject tool arguments (including runtime)
627
-
628
- injected_tool_calls = []
629
- input_types = [input_type] * len(tool_calls)
630
- for call, tool_runtime in zip(tool_calls, tool_runtimes, strict=False):
631
- injected_call = self._inject_tool_args(call, tool_runtime) # type: ignore[arg-type]
632
- injected_tool_calls.append(injected_call)
633
- with get_executor_for_config(config) as executor:
634
- outputs = list(
635
- executor.map(self._run_one, injected_tool_calls, input_types, tool_runtimes)
636
- )
637
-
638
- return self._combine_tool_outputs(outputs, input_type)
639
-
640
- async def _afunc(
641
- self,
642
- input: list[AnyMessage] | dict[str, Any] | BaseModel,
643
- config: RunnableConfig,
644
- runtime: Runtime,
645
- ) -> Any:
646
- tool_calls, input_type = self._parse_input(input)
647
- config_list = get_config_list(config, len(tool_calls))
648
-
649
- # Construct ToolRuntime instances at the top level for each tool call
650
- tool_runtimes = []
651
- for call, cfg in zip(tool_calls, config_list, strict=False):
652
- state = self._extract_state(input)
653
- tool_runtime = ToolRuntime(
654
- state=state,
655
- tool_call_id=call["id"],
656
- config=cfg,
657
- context=runtime.context,
658
- store=runtime.store,
659
- stream_writer=runtime.stream_writer,
660
- )
661
- tool_runtimes.append(tool_runtime)
662
-
663
- injected_tool_calls = []
664
- coros = []
665
- for call, tool_runtime in zip(tool_calls, tool_runtimes, strict=False):
666
- injected_call = self._inject_tool_args(call, tool_runtime) # type: ignore[arg-type]
667
- injected_tool_calls.append(injected_call)
668
- coros.append(self._arun_one(injected_call, input_type, tool_runtime)) # type: ignore[arg-type]
669
- outputs = await asyncio.gather(*coros)
670
-
671
- return self._combine_tool_outputs(outputs, input_type)
672
-
673
- def _combine_tool_outputs(
674
- self,
675
- outputs: list[ToolMessage | Command],
676
- input_type: Literal["list", "dict", "tool_calls"],
677
- ) -> list[Command | list[ToolMessage] | dict[str, list[ToolMessage]]]:
678
- # preserve existing behavior for non-command tool outputs for backwards
679
- # compatibility
680
- if not any(isinstance(output, Command) for output in outputs):
681
- # TypedDict, pydantic, dataclass, etc. should all be able to load from dict
682
- return outputs if input_type == "list" else {self._messages_key: outputs} # type: ignore[return-value, return-value]
683
-
684
- # LangGraph will automatically handle list of Command and non-command node
685
- # updates
686
- combined_outputs: list[Command | list[ToolMessage] | dict[str, list[ToolMessage]]] = []
687
-
688
- # combine all parent commands with goto into a single parent command
689
- parent_command: Command | None = None
690
- for output in outputs:
691
- if isinstance(output, Command):
692
- if (
693
- output.graph is Command.PARENT
694
- and isinstance(output.goto, list)
695
- and all(isinstance(send, Send) for send in output.goto)
696
- ):
697
- if parent_command:
698
- parent_command = replace(
699
- parent_command,
700
- goto=cast("list[Send]", parent_command.goto) + output.goto,
701
- )
702
- else:
703
- parent_command = Command(graph=Command.PARENT, goto=output.goto)
704
- else:
705
- combined_outputs.append(output)
706
- else:
707
- combined_outputs.append(
708
- [output] if input_type == "list" else {self._messages_key: [output]}
709
- )
710
-
711
- if parent_command:
712
- combined_outputs.append(parent_command)
713
- return combined_outputs
714
-
715
- def _execute_tool_sync(
716
- self,
717
- request: ToolCallRequest,
718
- input_type: Literal["list", "dict", "tool_calls"],
719
- config: RunnableConfig,
720
- ) -> ToolMessage | Command:
721
- """Execute tool call with configured error handling.
722
-
723
- Args:
724
- request: Tool execution request.
725
- input_type: Input format.
726
- config: Runnable configuration.
727
-
728
- Returns:
729
- ToolMessage or Command.
730
-
731
- Raises:
732
- Exception: If tool fails and handle_tool_errors is False.
733
- """
734
- call = request.tool_call
735
- tool = request.tool
736
-
737
- # Validate tool exists when we actually need to execute it
738
- if tool is None:
739
- if invalid_tool_message := self._validate_tool_call(call):
740
- return invalid_tool_message
741
- # This should never happen if validation works correctly
742
- msg = f"Tool {call['name']} is not registered with ToolNode"
743
- raise TypeError(msg)
744
-
745
- call_args = {**call, "type": "tool_call"}
746
-
747
- try:
748
- try:
749
- response = tool.invoke(call_args, config)
750
- except ValidationError as exc:
751
- raise ToolInvocationError(call["name"], exc, call["args"]) from exc
752
-
753
- # GraphInterrupt is a special exception that will always be raised.
754
- # It can be triggered in the following scenarios,
755
- # Where GraphInterrupt(GraphBubbleUp) is raised from an `interrupt` invocation
756
- # most commonly:
757
- # (1) a GraphInterrupt is raised inside a tool
758
- # (2) a GraphInterrupt is raised inside a graph node for a graph called as a tool
759
- # (3) a GraphInterrupt is raised when a subgraph is interrupted inside a graph
760
- # called as a tool
761
- # (2 and 3 can happen in a "supervisor w/ tools" multi-agent architecture)
762
- except GraphBubbleUp:
763
- raise
764
- except Exception as e:
765
- # Determine which exception types are handled
766
- handled_types: tuple[type[Exception], ...]
767
- if isinstance(self._handle_tool_errors, type) and issubclass(
768
- self._handle_tool_errors, Exception
769
- ):
770
- handled_types = (self._handle_tool_errors,)
771
- elif isinstance(self._handle_tool_errors, tuple):
772
- handled_types = self._handle_tool_errors
773
- elif callable(self._handle_tool_errors) and not isinstance(
774
- self._handle_tool_errors, type
775
- ):
776
- handled_types = _infer_handled_types(self._handle_tool_errors)
777
- else:
778
- # default behavior is catching all exceptions
779
- handled_types = (Exception,)
780
-
781
- # Check if this error should be handled
782
- if not self._handle_tool_errors or not isinstance(e, handled_types):
783
- raise
784
-
785
- # Error is handled - create error ToolMessage
786
- content = _handle_tool_error(e, flag=self._handle_tool_errors)
787
- return ToolMessage(
788
- content=content,
789
- name=call["name"],
790
- tool_call_id=call["id"],
791
- status="error",
792
- )
793
-
794
- # Process successful response
795
- if isinstance(response, Command):
796
- # Validate Command before returning to handler
797
- return self._validate_tool_command(response, request.tool_call, input_type)
798
- if isinstance(response, ToolMessage):
799
- response.content = cast("str | list", msg_content_output(response.content))
800
- return response
801
-
802
- msg = f"Tool {call['name']} returned unexpected type: {type(response)}"
803
- raise TypeError(msg)
804
-
805
- def _run_one(
806
- self,
807
- call: ToolCall,
808
- input_type: Literal["list", "dict", "tool_calls"],
809
- tool_runtime: ToolRuntime,
810
- ) -> ToolMessage | Command:
811
- """Execute single tool call with wrap_tool_call wrapper if configured.
812
-
813
- Args:
814
- call: Tool call dict.
815
- input_type: Input format.
816
- tool_runtime: Tool runtime.
817
-
818
- Returns:
819
- ToolMessage or Command.
820
- """
821
- # Validation is deferred to _execute_tool_sync to allow interceptors
822
- # to short-circuit requests for unregistered tools
823
- tool = self.tools_by_name.get(call["name"])
824
-
825
- # Create the tool request with state and runtime
826
- tool_request = ToolCallRequest(
827
- tool_call=call,
828
- tool=tool,
829
- state=tool_runtime.state,
830
- runtime=tool_runtime,
831
- )
832
-
833
- config = tool_runtime.config
834
-
835
- if self._wrap_tool_call is None:
836
- # No wrapper - execute directly
837
- return self._execute_tool_sync(tool_request, input_type, config)
838
-
839
- # Define execute callable that can be called multiple times
840
- def execute(req: ToolCallRequest) -> ToolMessage | Command:
841
- """Execute tool with given request. Can be called multiple times."""
842
- return self._execute_tool_sync(req, input_type, config)
843
-
844
- # Call wrapper with request and execute callable
845
- try:
846
- return self._wrap_tool_call(tool_request, execute)
847
- except Exception as e:
848
- # Wrapper threw an exception
849
- if not self._handle_tool_errors:
850
- raise
851
- # Convert to error message
852
- content = _handle_tool_error(e, flag=self._handle_tool_errors)
853
- return ToolMessage(
854
- content=content,
855
- name=tool_request.tool_call["name"],
856
- tool_call_id=tool_request.tool_call["id"],
857
- status="error",
858
- )
859
-
860
- async def _execute_tool_async(
861
- self,
862
- request: ToolCallRequest,
863
- input_type: Literal["list", "dict", "tool_calls"],
864
- config: RunnableConfig,
865
- ) -> ToolMessage | Command:
866
- """Execute tool call asynchronously with configured error handling.
867
-
868
- Args:
869
- request: Tool execution request.
870
- input_type: Input format.
871
- config: Runnable configuration.
872
-
873
- Returns:
874
- ToolMessage or Command.
875
-
876
- Raises:
877
- Exception: If tool fails and handle_tool_errors is False.
878
- """
879
- call = request.tool_call
880
- tool = request.tool
881
-
882
- # Validate tool exists when we actually need to execute it
883
- if tool is None:
884
- if invalid_tool_message := self._validate_tool_call(call):
885
- return invalid_tool_message
886
- # This should never happen if validation works correctly
887
- msg = f"Tool {call['name']} is not registered with ToolNode"
888
- raise TypeError(msg)
889
-
890
- call_args = {**call, "type": "tool_call"}
891
-
892
- try:
893
- try:
894
- response = await tool.ainvoke(call_args, config)
895
- except ValidationError as exc:
896
- raise ToolInvocationError(call["name"], exc, call["args"]) from exc
897
-
898
- # GraphInterrupt is a special exception that will always be raised.
899
- # It can be triggered in the following scenarios,
900
- # Where GraphInterrupt(GraphBubbleUp) is raised from an `interrupt` invocation
901
- # most commonly:
902
- # (1) a GraphInterrupt is raised inside a tool
903
- # (2) a GraphInterrupt is raised inside a graph node for a graph called as a tool
904
- # (3) a GraphInterrupt is raised when a subgraph is interrupted inside a graph
905
- # called as a tool
906
- # (2 and 3 can happen in a "supervisor w/ tools" multi-agent architecture)
907
- except GraphBubbleUp:
908
- raise
909
- except Exception as e:
910
- # Determine which exception types are handled
911
- handled_types: tuple[type[Exception], ...]
912
- if isinstance(self._handle_tool_errors, type) and issubclass(
913
- self._handle_tool_errors, Exception
914
- ):
915
- handled_types = (self._handle_tool_errors,)
916
- elif isinstance(self._handle_tool_errors, tuple):
917
- handled_types = self._handle_tool_errors
918
- elif callable(self._handle_tool_errors) and not isinstance(
919
- self._handle_tool_errors, type
920
- ):
921
- handled_types = _infer_handled_types(self._handle_tool_errors)
922
- else:
923
- # default behavior is catching all exceptions
924
- handled_types = (Exception,)
925
-
926
- # Check if this error should be handled
927
- if not self._handle_tool_errors or not isinstance(e, handled_types):
928
- raise
929
-
930
- # Error is handled - create error ToolMessage
931
- content = _handle_tool_error(e, flag=self._handle_tool_errors)
932
- return ToolMessage(
933
- content=content,
934
- name=call["name"],
935
- tool_call_id=call["id"],
936
- status="error",
937
- )
938
-
939
- # Process successful response
940
- if isinstance(response, Command):
941
- # Validate Command before returning to handler
942
- return self._validate_tool_command(response, request.tool_call, input_type)
943
- if isinstance(response, ToolMessage):
944
- response.content = cast("str | list", msg_content_output(response.content))
945
- return response
946
-
947
- msg = f"Tool {call['name']} returned unexpected type: {type(response)}"
948
- raise TypeError(msg)
949
-
950
- async def _arun_one(
951
- self,
952
- call: ToolCall,
953
- input_type: Literal["list", "dict", "tool_calls"],
954
- tool_runtime: ToolRuntime,
955
- ) -> ToolMessage | Command:
956
- """Execute single tool call asynchronously with awrap_tool_call wrapper if configured.
957
-
958
- Args:
959
- call: Tool call dict.
960
- input_type: Input format.
961
- tool_runtime: Tool runtime.
962
-
963
- Returns:
964
- ToolMessage or Command.
965
- """
966
- # Validation is deferred to _execute_tool_async to allow interceptors
967
- # to short-circuit requests for unregistered tools
968
- tool = self.tools_by_name.get(call["name"])
969
-
970
- # Create the tool request with state and runtime
971
- tool_request = ToolCallRequest(
972
- tool_call=call,
973
- tool=tool,
974
- state=tool_runtime.state,
975
- runtime=tool_runtime,
976
- )
977
-
978
- config = tool_runtime.config
979
-
980
- if self._awrap_tool_call is None and self._wrap_tool_call is None:
981
- # No wrapper - execute directly
982
- return await self._execute_tool_async(tool_request, input_type, config)
983
-
984
- # Define async execute callable that can be called multiple times
985
- async def execute(req: ToolCallRequest) -> ToolMessage | Command:
986
- """Execute tool with given request. Can be called multiple times."""
987
- return await self._execute_tool_async(req, input_type, config)
988
-
989
- def _sync_execute(req: ToolCallRequest) -> ToolMessage | Command:
990
- """Sync execute fallback for sync wrapper."""
991
- return self._execute_tool_sync(req, input_type, config)
992
-
993
- # Call wrapper with request and execute callable
994
- try:
995
- if self._awrap_tool_call is not None:
996
- return await self._awrap_tool_call(tool_request, execute)
997
- # None check was performed above already
998
- self._wrap_tool_call = cast("ToolCallWrapper", self._wrap_tool_call)
999
- return self._wrap_tool_call(tool_request, _sync_execute)
1000
- except Exception as e:
1001
- # Wrapper threw an exception
1002
- if not self._handle_tool_errors:
1003
- raise
1004
- # Convert to error message
1005
- content = _handle_tool_error(e, flag=self._handle_tool_errors)
1006
- return ToolMessage(
1007
- content=content,
1008
- name=tool_request.tool_call["name"],
1009
- tool_call_id=tool_request.tool_call["id"],
1010
- status="error",
1011
- )
1012
-
1013
- def _parse_input(
1014
- self,
1015
- input: list[AnyMessage] | dict[str, Any] | BaseModel,
1016
- ) -> tuple[list[ToolCall], Literal["list", "dict", "tool_calls"]]:
1017
- input_type: Literal["list", "dict", "tool_calls"]
1018
- if isinstance(input, list):
1019
- if isinstance(input[-1], dict) and input[-1].get("type") == "tool_call":
1020
- input_type = "tool_calls"
1021
- tool_calls = cast("list[ToolCall]", input)
1022
- return tool_calls, input_type
1023
- input_type = "list"
1024
- messages = input
1025
- elif isinstance(input, dict) and input.get("__type") == "tool_call_with_context":
1026
- # Handle ToolCallWithContext from Send API
1027
- # mypy will not be able to type narrow correctly since the signature
1028
- # for input contains dict[str, Any]. We'd need to narrow dict[str, Any]
1029
- # before we can apply correct typing.
1030
- input_with_ctx = cast("ToolCallWithContext", input)
1031
- input_type = "tool_calls"
1032
- return [input_with_ctx["tool_call"]], input_type
1033
- elif isinstance(input, dict) and (messages := input.get(self._messages_key, [])):
1034
- input_type = "dict"
1035
- elif messages := getattr(input, self._messages_key, []):
1036
- # Assume dataclass-like state that can coerce from dict
1037
- input_type = "dict"
1038
- else:
1039
- msg = "No message found in input"
1040
- raise ValueError(msg)
1041
-
1042
- try:
1043
- latest_ai_message = next(m for m in reversed(messages) if isinstance(m, AIMessage))
1044
- except StopIteration:
1045
- msg = "No AIMessage found in input"
1046
- raise ValueError(msg)
1047
-
1048
- tool_calls = list(latest_ai_message.tool_calls)
1049
- return tool_calls, input_type
1050
-
1051
- def _validate_tool_call(self, call: ToolCall) -> ToolMessage | None:
1052
- requested_tool = call["name"]
1053
- if requested_tool not in self.tools_by_name:
1054
- all_tool_names = list(self.tools_by_name.keys())
1055
- content = INVALID_TOOL_NAME_ERROR_TEMPLATE.format(
1056
- requested_tool=requested_tool,
1057
- available_tools=", ".join(all_tool_names),
1058
- )
1059
- return ToolMessage(
1060
- content, name=requested_tool, tool_call_id=call["id"], status="error"
1061
- )
1062
- return None
1063
-
1064
- def _extract_state(
1065
- self, input: list[AnyMessage] | dict[str, Any] | BaseModel
1066
- ) -> list[AnyMessage] | dict[str, Any] | BaseModel:
1067
- """Extract state from input, handling ToolCallWithContext if present.
1068
-
1069
- Args:
1070
- input: The input which may be raw state or ToolCallWithContext.
1071
-
1072
- Returns:
1073
- The actual state to pass to wrap_tool_call wrappers.
1074
- """
1075
- if isinstance(input, dict) and input.get("__type") == "tool_call_with_context":
1076
- return input["state"]
1077
- return input
1078
-
1079
- def _inject_state(
1080
- self,
1081
- tool_call: ToolCall,
1082
- state: list[AnyMessage] | dict[str, Any] | BaseModel,
1083
- ) -> ToolCall:
1084
- state_args = self._tool_to_state_args[tool_call["name"]]
1085
-
1086
- if state_args and isinstance(state, list):
1087
- required_fields = list(state_args.values())
1088
- if (
1089
- len(required_fields) == 1 and required_fields[0] == self._messages_key
1090
- ) or required_fields[0] is None:
1091
- state = {self._messages_key: state}
1092
- else:
1093
- err_msg = (
1094
- f"Invalid input to ToolNode. Tool {tool_call['name']} requires "
1095
- f"graph state dict as input."
1096
- )
1097
- if any(state_field for state_field in state_args.values()):
1098
- required_fields_str = ", ".join(f for f in required_fields if f)
1099
- err_msg += f" State should contain fields {required_fields_str}."
1100
- raise ValueError(err_msg)
1101
-
1102
- if isinstance(state, dict):
1103
- tool_state_args = {
1104
- tool_arg: state[state_field] if state_field else state
1105
- for tool_arg, state_field in state_args.items()
1106
- }
1107
- else:
1108
- tool_state_args = {
1109
- tool_arg: getattr(state, state_field) if state_field else state
1110
- for tool_arg, state_field in state_args.items()
1111
- }
1112
-
1113
- tool_call["args"] = {
1114
- **tool_call["args"],
1115
- **tool_state_args,
1116
- }
1117
- return tool_call
1118
-
1119
- def _inject_store(self, tool_call: ToolCall, store: BaseStore | None) -> ToolCall:
1120
- store_arg = self._tool_to_store_arg[tool_call["name"]]
1121
- if not store_arg:
1122
- return tool_call
1123
-
1124
- if store is None:
1125
- msg = (
1126
- "Cannot inject store into tools with InjectedStore annotations - "
1127
- "please compile your graph with a store."
1128
- )
1129
- raise ValueError(msg)
1130
-
1131
- tool_call["args"] = {
1132
- **tool_call["args"],
1133
- store_arg: store,
1134
- }
1135
- return tool_call
1136
-
1137
- def _inject_runtime(self, tool_call: ToolCall, tool_runtime: ToolRuntime) -> ToolCall:
1138
- """Inject ToolRuntime into tool call arguments.
1139
-
1140
- Args:
1141
- tool_call: The tool call to inject runtime into.
1142
- tool_runtime: The ToolRuntime instance to inject.
1143
-
1144
- Returns:
1145
- The tool call with runtime injected if needed.
1146
- """
1147
- runtime_arg = self._tool_to_runtime_arg.get(tool_call["name"])
1148
- if not runtime_arg:
1149
- return tool_call
1150
-
1151
- tool_call["args"] = {
1152
- **tool_call["args"],
1153
- runtime_arg: tool_runtime,
1154
- }
1155
- return tool_call
1156
-
1157
- def _inject_tool_args(
1158
- self,
1159
- tool_call: ToolCall,
1160
- tool_runtime: ToolRuntime,
1161
- ) -> ToolCall:
1162
- """Inject graph state, store, and runtime into tool call arguments.
1163
-
1164
- This is an internal method that enables tools to access graph context that
1165
- should not be controlled by the model. Tools can declare dependencies on graph
1166
- state, persistent storage, or runtime context using InjectedState, InjectedStore,
1167
- and ToolRuntime annotations. This method automatically identifies these
1168
- dependencies and injects the appropriate values.
1169
-
1170
- The injection process preserves the original tool call structure while adding
1171
- the necessary context arguments. This allows tools to be both model-callable
1172
- and context-aware without exposing internal state management to the model.
1173
-
1174
- Args:
1175
- tool_call: The tool call dictionary to augment with injected arguments.
1176
- Must contain 'name', 'args', 'id', and 'type' fields.
1177
- tool_runtime: The ToolRuntime instance containing all runtime context
1178
- (state, config, store, context, stream_writer) to inject into tools.
1179
-
1180
- Returns:
1181
- A new ToolCall dictionary with the same structure as the input but with
1182
- additional arguments injected based on the tool's annotation requirements.
1183
-
1184
- Raises:
1185
- ValueError: If a tool requires store injection but no store is provided,
1186
- or if state injection requirements cannot be satisfied.
1187
-
1188
- !!! note
1189
- This method is called automatically during tool execution. It should not
1190
- be called from outside the `ToolNode`.
1191
- """
1192
- if tool_call["name"] not in self.tools_by_name:
1193
- return tool_call
1194
-
1195
- tool_call_copy: ToolCall = copy(tool_call)
1196
- tool_call_with_state = self._inject_state(tool_call_copy, tool_runtime.state)
1197
- tool_call_with_store = self._inject_store(tool_call_with_state, tool_runtime.store)
1198
- return self._inject_runtime(tool_call_with_store, tool_runtime)
1199
-
1200
- def _validate_tool_command(
1201
- self,
1202
- command: Command,
1203
- call: ToolCall,
1204
- input_type: Literal["list", "dict", "tool_calls"],
1205
- ) -> Command:
1206
- if isinstance(command.update, dict):
1207
- # input type is dict when ToolNode is invoked with a dict input
1208
- # (e.g. {"messages": [AIMessage(..., tool_calls=[...])]})
1209
- if input_type not in ("dict", "tool_calls"):
1210
- msg = (
1211
- "Tools can provide a dict in Command.update only when using dict "
1212
- f"with '{self._messages_key}' key as ToolNode input, "
1213
- f"got: {command.update} for tool '{call['name']}'"
1214
- )
1215
- raise ValueError(msg)
1216
-
1217
- updated_command = deepcopy(command)
1218
- state_update = cast("dict[str, Any]", updated_command.update) or {}
1219
- messages_update = state_update.get(self._messages_key, [])
1220
- elif isinstance(command.update, list):
1221
- # Input type is list when ToolNode is invoked with a list input
1222
- # (e.g. [AIMessage(..., tool_calls=[...])])
1223
- if input_type != "list":
1224
- msg = (
1225
- "Tools can provide a list of messages in Command.update "
1226
- "only when using list of messages as ToolNode input, "
1227
- f"got: {command.update} for tool '{call['name']}'"
1228
- )
1229
- raise ValueError(msg)
1230
-
1231
- updated_command = deepcopy(command)
1232
- messages_update = updated_command.update
1233
- else:
1234
- return command
1235
-
1236
- # convert to message objects if updates are in a dict format
1237
- messages_update = convert_to_messages(messages_update)
1238
-
1239
- # no validation needed if all messages are being removed
1240
- if messages_update == [RemoveMessage(id=REMOVE_ALL_MESSAGES)]:
1241
- return updated_command
1242
-
1243
- has_matching_tool_message = False
1244
- for message in messages_update:
1245
- if not isinstance(message, ToolMessage):
1246
- continue
1247
-
1248
- if message.tool_call_id == call["id"]:
1249
- message.name = call["name"]
1250
- has_matching_tool_message = True
1251
-
1252
- # validate that we always have a ToolMessage matching the tool call in
1253
- # Command.update if command is sent to the CURRENT graph
1254
- if updated_command.graph is None and not has_matching_tool_message:
1255
- example_update = (
1256
- '`Command(update={"messages": '
1257
- '[ToolMessage("Success", tool_call_id=tool_call_id), ...]}, ...)`'
1258
- if input_type == "dict"
1259
- else "`Command(update="
1260
- '[ToolMessage("Success", tool_call_id=tool_call_id), ...], ...)`'
1261
- )
1262
- msg = (
1263
- "Expected to have a matching ToolMessage in Command.update "
1264
- f"for tool '{call['name']}', got: {messages_update}. "
1265
- "Every tool call (LLM requesting to call a tool) "
1266
- "in the message history MUST have a corresponding ToolMessage. "
1267
- f"You can fix it by modifying the tool to return {example_update}."
1268
- )
1269
- raise ValueError(msg)
1270
- return updated_command
1271
-
1272
-
1273
- def tools_condition(
1274
- state: list[AnyMessage] | dict[str, Any] | BaseModel,
1275
- messages_key: str = "messages",
1276
- ) -> Literal["tools", "__end__"]:
1277
- """Conditional routing function for tool-calling workflows.
1278
-
1279
- This utility function implements the standard conditional logic for ReAct-style
1280
- agents: if the last AI message contains tool calls, route to the tool execution
1281
- node; otherwise, end the workflow. This pattern is fundamental to most tool-calling
1282
- agent architectures.
1283
-
1284
- The function handles multiple state formats commonly used in LangGraph applications,
1285
- making it flexible for different graph designs while maintaining consistent behavior.
1286
-
1287
- Args:
1288
- state: The current graph state to examine for tool calls. Supported formats:
1289
- - Dictionary containing a messages key (for StateGraph)
1290
- - BaseModel instance with a messages attribute
1291
- messages_key: The key or attribute name containing the message list in the state.
1292
- This allows customization for graphs using different state schemas.
1293
- Defaults to "messages".
1294
-
1295
- Returns:
1296
- Either "tools" if tool calls are present in the last AI message, or "__end__"
1297
- to terminate the workflow. These are the standard routing destinations for
1298
- tool-calling conditional edges.
1299
-
1300
- Raises:
1301
- ValueError: If no messages can be found in the provided state format.
1302
-
1303
- Example:
1304
- Basic usage in a ReAct agent:
1305
-
1306
- ```python
1307
- from langgraph.graph import StateGraph
1308
- from langchain.tools import ToolNode
1309
- from langchain.tools.tool_node import tools_condition
1310
- from typing_extensions import TypedDict
1311
-
1312
-
1313
- class State(TypedDict):
1314
- messages: list
1315
-
1316
-
1317
- graph = StateGraph(State)
1318
- graph.add_node("llm", call_model)
1319
- graph.add_node("tools", ToolNode([my_tool]))
1320
- graph.add_conditional_edges(
1321
- "llm",
1322
- tools_condition, # Routes to "tools" or "__end__"
1323
- {"tools": "tools", "__end__": "__end__"},
1324
- )
1325
- ```
1326
-
1327
- Custom messages key:
1328
-
1329
- ```python
1330
- def custom_condition(state):
1331
- return tools_condition(state, messages_key="chat_history")
1332
- ```
1333
-
1334
- !!! note
1335
- This function is designed to work seamlessly with `ToolNode` and standard
1336
- LangGraph patterns. It expects the last message to be an `AIMessage` when
1337
- tool calls are present, which is the standard output format for tool-calling
1338
- language models.
1339
- """
1340
- if isinstance(state, list):
1341
- ai_message = state[-1]
1342
- elif (isinstance(state, dict) and (messages := state.get(messages_key, []))) or (
1343
- messages := getattr(state, messages_key, [])
1344
- ):
1345
- ai_message = messages[-1]
1346
- else:
1347
- msg = f"No messages found in input state to tool_edge: {state}"
1348
- raise ValueError(msg)
1349
- if hasattr(ai_message, "tool_calls") and len(ai_message.tool_calls) > 0:
1350
- return "tools"
1351
- return "__end__"
1352
-
1353
-
1354
- @dataclass
1355
- class ToolRuntime(_DirectlyInjectedToolArg, Generic[ContextT, StateT]):
1356
- """Runtime context automatically injected into tools.
1357
-
1358
- When a tool function has a parameter named `tool_runtime` with type hint
1359
- `ToolRuntime`, the tool execution system will automatically inject an instance
1360
- containing:
1361
-
1362
- - `state`: The current graph state
1363
- - `tool_call_id`: The ID of the current tool call
1364
- - `config`: `RunnableConfig` for the current execution
1365
- - `context`: Runtime context (from langgraph `Runtime`)
1366
- - `store`: `BaseStore` instance for persistent storage (from langgraph `Runtime`)
1367
- - `stream_writer`: `StreamWriter` for streaming output (from langgraph `Runtime`)
1368
-
1369
- No `Annotated` wrapper is needed - just use `runtime: ToolRuntime`
1370
- as a parameter.
1371
-
1372
- Example:
1373
- ```python
1374
- from langchain_core.tools import tool
1375
- from langchain.tools import ToolRuntime
1376
-
1377
- @tool
1378
- def my_tool(x: int, runtime: ToolRuntime) -> str:
1379
- \"\"\"Tool that accesses runtime context.\"\"\"
1380
- # Access state
1381
- messages = tool_runtime.state["messages"]
1382
-
1383
- # Access tool_call_id
1384
- print(f"Tool call ID: {tool_runtime.tool_call_id}")
1385
-
1386
- # Access config
1387
- print(f"Run ID: {tool_runtime.config.get('run_id')}")
1388
-
1389
- # Access runtime context
1390
- user_id = tool_runtime.context.get("user_id")
1391
-
1392
- # Access store
1393
- tool_runtime.store.put(("metrics",), "count", 1)
1394
-
1395
- # Stream output
1396
- tool_runtime.stream_writer.write("Processing...")
1397
-
1398
- return f"Processed {x}"
1399
- ```
1400
-
1401
- !!! note
1402
- This is a marker class used for type checking and detection.
1403
- The actual runtime object will be constructed during tool execution.
1404
- """
1405
-
1406
- state: StateT
1407
- context: ContextT
1408
- config: RunnableConfig
1409
- stream_writer: StreamWriter
1410
- tool_call_id: str | None
1411
- store: BaseStore | None
1412
-
1413
-
1414
- class InjectedState(InjectedToolArg):
1415
- """Annotation for injecting graph state into tool arguments.
1416
-
1417
- This annotation enables tools to access graph state without exposing state
1418
- management details to the language model. Tools annotated with `InjectedState`
1419
- receive state data automatically during execution while remaining invisible
1420
- to the model's tool-calling interface.
1421
-
1422
- Args:
1423
- field: Optional key to extract from the state dictionary. If `None`, the entire
1424
- state is injected. If specified, only that field's value is injected.
1425
- This allows tools to request specific state components rather than
1426
- processing the full state structure.
1427
-
1428
- Example:
1429
- ```python
1430
- from typing import List
1431
- from typing_extensions import Annotated, TypedDict
1432
-
1433
- from langchain_core.messages import BaseMessage, AIMessage
1434
- from langchain.tools import InjectedState, ToolNode, tool
1435
-
1436
-
1437
- class AgentState(TypedDict):
1438
- messages: List[BaseMessage]
1439
- foo: str
1440
-
1441
-
1442
- @tool
1443
- def state_tool(x: int, state: Annotated[dict, InjectedState]) -> str:
1444
- '''Do something with state.'''
1445
- if len(state["messages"]) > 2:
1446
- return state["foo"] + str(x)
1447
- else:
1448
- return "not enough messages"
1449
-
1450
-
1451
- @tool
1452
- def foo_tool(x: int, foo: Annotated[str, InjectedState("foo")]) -> str:
1453
- '''Do something else with state.'''
1454
- return foo + str(x + 1)
1455
-
1456
-
1457
- node = ToolNode([state_tool, foo_tool])
1458
-
1459
- tool_call1 = {"name": "state_tool", "args": {"x": 1}, "id": "1", "type": "tool_call"}
1460
- tool_call2 = {"name": "foo_tool", "args": {"x": 1}, "id": "2", "type": "tool_call"}
1461
- state = {
1462
- "messages": [AIMessage("", tool_calls=[tool_call1, tool_call2])],
1463
- "foo": "bar",
1464
- }
1465
- node.invoke(state)
1466
- ```
1467
-
1468
- ```python
1469
- [
1470
- ToolMessage(content="not enough messages", name="state_tool", tool_call_id="1"),
1471
- ToolMessage(content="bar2", name="foo_tool", tool_call_id="2"),
1472
- ]
1473
- ```
1474
-
1475
- !!! note
1476
- - `InjectedState` arguments are automatically excluded from tool schemas
1477
- presented to language models
1478
- - `ToolNode` handles the injection process during execution
1479
- - Tools can mix regular arguments (controlled by the model) with injected
1480
- arguments (controlled by the system)
1481
- - State injection occurs after the model generates tool calls but before
1482
- tool execution
1483
- """
1484
-
1485
- def __init__(self, field: str | None = None) -> None:
1486
- """Initialize the `InjectedState` annotation."""
1487
- self.field = field
1488
-
1489
-
1490
- class InjectedStore(InjectedToolArg):
1491
- """Annotation for injecting persistent store into tool arguments.
1492
-
1493
- This annotation enables tools to access LangGraph's persistent storage system
1494
- without exposing storage details to the language model. Tools annotated with
1495
- InjectedStore receive the store instance automatically during execution while
1496
- remaining invisible to the model's tool-calling interface.
1497
-
1498
- The store provides persistent, cross-session data storage that tools can use
1499
- for maintaining context, user preferences, or any other data that needs to
1500
- persist beyond individual workflow executions.
1501
-
1502
- !!! warning
1503
- `InjectedStore` annotation requires `langchain-core >= 0.3.8`
1504
-
1505
- Example:
1506
- ```python
1507
- from typing_extensions import Annotated
1508
- from langgraph.store.memory import InMemoryStore
1509
- from langchain.tools import InjectedStore, ToolNode, tool
1510
-
1511
- @tool
1512
- def save_preference(
1513
- key: str,
1514
- value: str,
1515
- store: Annotated[Any, InjectedStore()]
1516
- ) -> str:
1517
- \"\"\"Save user preference to persistent storage.\"\"\"
1518
- store.put(("preferences",), key, value)
1519
- return f"Saved {key} = {value}"
1520
-
1521
- @tool
1522
- def get_preference(
1523
- key: str,
1524
- store: Annotated[Any, InjectedStore()]
1525
- ) -> str:
1526
- \"\"\"Retrieve user preference from persistent storage.\"\"\"
1527
- result = store.get(("preferences",), key)
1528
- return result.value if result else "Not found"
1529
- ```
1530
-
1531
- Usage with `ToolNode` and graph compilation:
1532
-
1533
- ```python
1534
- from langgraph.graph import StateGraph
1535
- from langgraph.store.memory import InMemoryStore
1536
-
1537
- store = InMemoryStore()
1538
- tool_node = ToolNode([save_preference, get_preference])
1539
-
1540
- graph = StateGraph(State)
1541
- graph.add_node("tools", tool_node)
1542
- compiled_graph = graph.compile(store=store) # Store is injected automatically
1543
- ```
1544
-
1545
- Cross-session persistence:
1546
-
1547
- ```python
1548
- # First session
1549
- result1 = graph.invoke({"messages": [HumanMessage("Save my favorite color as blue")]})
1550
-
1551
- # Later session - data persists
1552
- result2 = graph.invoke({"messages": [HumanMessage("What's my favorite color?")]})
1553
- ```
1554
-
1555
- !!! note
1556
- - `InjectedStore` arguments are automatically excluded from tool schemas
1557
- presented to language models
1558
- - The store instance is automatically injected by `ToolNode` during execution
1559
- - Tools can access namespaced storage using the store's get/put methods
1560
- - Store injection requires the graph to be compiled with a store instance
1561
- - Multiple tools can share the same store instance for data consistency
1562
- """
1563
-
1564
-
1565
- def _is_injection(
1566
- type_arg: Any,
1567
- injection_type: type[InjectedState | InjectedStore | ToolRuntime],
1568
- ) -> bool:
1569
- """Check if a type argument represents an injection annotation.
1570
-
1571
- This utility function determines whether a type annotation indicates that
1572
- an argument should be injected with state or store data. It handles both
1573
- direct annotations and nested annotations within Union or Annotated types.
1574
-
1575
- Args:
1576
- type_arg: The type argument to check for injection annotations.
1577
- injection_type: The injection type to look for (InjectedState or InjectedStore).
1578
-
1579
- Returns:
1580
- True if the type argument contains the specified injection annotation.
1581
- """
1582
- if isinstance(type_arg, injection_type) or (
1583
- isinstance(type_arg, type) and issubclass(type_arg, injection_type)
1584
- ):
1585
- return True
1586
- origin_ = get_origin(type_arg)
1587
- if origin_ is Union or origin_ is Annotated:
1588
- return any(_is_injection(ta, injection_type) for ta in get_args(type_arg))
1589
- return False
1590
-
1591
-
1592
- def _get_state_args(tool: BaseTool) -> dict[str, str | None]:
1593
- """Extract state injection mappings from tool annotations.
1594
-
1595
- This function analyzes a tool's input schema to identify arguments that should
1596
- be injected with graph state. It processes InjectedState annotations to build
1597
- a mapping of tool argument names to state field names.
1598
-
1599
- Args:
1600
- tool: The tool to analyze for state injection requirements.
1601
-
1602
- Returns:
1603
- A dictionary mapping tool argument names to state field names. If a field
1604
- name is None, the entire state should be injected for that argument.
1605
- """
1606
- full_schema = tool.get_input_schema()
1607
- tool_args_to_state_fields: dict = {}
1608
-
1609
- for name, type_ in get_all_basemodel_annotations(full_schema).items():
1610
- injections = [
1611
- type_arg for type_arg in get_args(type_) if _is_injection(type_arg, InjectedState)
1612
- ]
1613
- if len(injections) > 1:
1614
- msg = (
1615
- "A tool argument should not be annotated with InjectedState more than "
1616
- f"once. Received arg {name} with annotations {injections}."
1617
- )
1618
- raise ValueError(msg)
1619
- if len(injections) == 1:
1620
- injection = injections[0]
1621
- if isinstance(injection, InjectedState) and injection.field:
1622
- tool_args_to_state_fields[name] = injection.field
1623
- else:
1624
- tool_args_to_state_fields[name] = None
1625
- else:
1626
- pass
1627
- return tool_args_to_state_fields
1628
-
1629
-
1630
- def _get_store_arg(tool: BaseTool) -> str | None:
1631
- """Extract store injection argument from tool annotations.
1632
-
1633
- This function analyzes a tool's input schema to identify the argument that
1634
- should be injected with the graph store. Only one store argument is supported
1635
- per tool.
1636
-
1637
- Args:
1638
- tool: The tool to analyze for store injection requirements.
1639
-
1640
- Returns:
1641
- The name of the argument that should receive the store injection, or None
1642
- if no store injection is required.
1643
-
1644
- Raises:
1645
- ValueError: If a tool argument has multiple InjectedStore annotations.
1646
- """
1647
- full_schema = tool.get_input_schema()
1648
- for name, type_ in get_all_basemodel_annotations(full_schema).items():
1649
- injections = [
1650
- type_arg for type_arg in get_args(type_) if _is_injection(type_arg, InjectedStore)
1651
- ]
1652
- if len(injections) > 1:
1653
- msg = (
1654
- "A tool argument should not be annotated with InjectedStore more than "
1655
- f"once. Received arg {name} with annotations {injections}."
1656
- )
1657
- raise ValueError(msg)
1658
- if len(injections) == 1:
1659
- return name
1660
-
1661
- return None
1662
-
1663
-
1664
- def _get_runtime_arg(tool: BaseTool) -> str | None:
1665
- """Extract runtime injection argument from tool annotations.
1666
-
1667
- This function analyzes a tool's input schema to identify the argument that
1668
- should be injected with the ToolRuntime instance. Only one runtime argument
1669
- is supported per tool.
1670
-
1671
- Args:
1672
- tool: The tool to analyze for runtime injection requirements.
1673
-
1674
- Returns:
1675
- The name of the argument that should receive the runtime injection, or None
1676
- if no runtime injection is required.
1677
-
1678
- Raises:
1679
- ValueError: If a tool argument has multiple ToolRuntime annotations.
1680
- """
1681
- full_schema = tool.get_input_schema()
1682
- for name, type_ in get_all_basemodel_annotations(full_schema).items():
1683
- # Check if the parameter name is "runtime" (regardless of type)
1684
- if name == "runtime":
1685
- return name
1686
- # Check if the type itself is ToolRuntime (direct usage)
1687
- if _is_injection(type_, ToolRuntime):
1688
- return name
1689
- # Check if ToolRuntime is in Annotated args
1690
- injections = [
1691
- type_arg for type_arg in get_args(type_) if _is_injection(type_arg, ToolRuntime)
1692
- ]
1693
- if len(injections) > 1:
1694
- msg = (
1695
- "A tool argument should not be annotated with ToolRuntime more than "
1696
- f"once. Received arg {name} with annotations {injections}."
1697
- )
1698
- raise ValueError(msg)
1699
- if len(injections) == 1:
1700
- return name
1701
-
1702
- return None