
    KKi             	         S r SSKJr  SSKrSSKrSSKrSSKJrJr  SSK	J	r	J
r
  SSKJrJr  SSKJr  SSKJrJrJrJrJrJrJrJrJrJrJr  SS	KJrJrJrJ r J!r!J"r"  SS
K#J$r$J%r%J&r&  SSK'J(r(J)r)  SSK'J*r+  SSK,J-r-J.r.J/r/J0r0  SSK1J2r2  SSK3J4r4  SSK5J6r6  SSK7J8r8  SSK9J:r:J;r;J<r<  SSK=J>r>J?r?  SSK@JArAJBrB  \(       a  SSKJCrC  SSKDJErE  SSKFJGrG  \A" S\HS9rI\A" SSS9rJSrKSrLSrMSrN " S S \S!S"9rO\ " S# S$5      5       rP\\P\\P/\!\:-  4   /\!\:-  4   rQ \\P\\P/\\!\:-     4   /\\!\:-     4   rR  " S% S&\5      rSS<S' jrT " S( S)\.5      rUS=S* jrV      S>S+ jrWS?S, jrX      S@S- jrY\ " S. S/5      5       rZ " S0 S1\25      r[ SA     SBS2 jjr\\ " S3 S4\/\\J\I4   5      5       r] " S5 S6\)5      r^ " S7 S8\)5      r_      SCS9 jr`      SDS: jraSES; jrbg)Fa  Tool execution node for LangGraph workflows.

This module provides prebuilt functionality for executing tools in LangGraph.

Tools are functions that models can call to interact with external systems,
APIs, databases, or perform computations.

The module implements design patterns for:

- Parallel execution of multiple tool calls for efficiency
- Robust error handling with customizable error messages
- State injection for tools that need access to graph state
- Store injection for tools that need persistent storage
- Command-based state updates for advanced control flow

Key Components:

- [`ToolNode`][langgraph.prebuilt.ToolNode]: Main class for executing tools in LangGraph workflows
- [`InjectedState`][langgraph.prebuilt.InjectedState]: Annotation for injecting graph state into tools
- [`InjectedStore`][langgraph.prebuilt.InjectedStore]: Annotation for injecting persistent store into tools
- [`ToolRuntime`][langgraph.prebuilt.ToolRuntime]: Runtime information for tools, bundling together `state`, `context`,
    `config`, `stream_writer`, `tool_call_id`, and `store`
- [`tools_condition`][langgraph.prebuilt.tools_condition]: Utility function for conditional routing based on tool calls

Typical Usage:
    ```python
    from langchain_core.tools import tool
    from langchain.tools import ToolNode


    @tool
    def my_tool(x: int) -> str:
        return f"Result: {x}"


    tool_node = ToolNode([my_tool])
    ```
    )annotationsN)	AwaitableCallable)copydeepcopy)	dataclassreplace)	UnionType)TYPE_CHECKING	AnnotatedAnyGenericLiteral	TypedDictUnioncastget_args
get_originget_type_hints)	AIMessage
AnyMessageRemoveMessageToolCallToolMessageconvert_to_messages)RunnableConfigget_config_listget_executor_for_config)BaseToolInjectedToolArg)tool)TOOL_MESSAGE_BLOCK_TYPESToolException_DirectlyInjectedToolArgget_all_basemodel_annotations)RunnableCallable)GraphBubbleUp)REMOVE_ALL_MESSAGES)	BaseStore)CommandSendStreamWriter)	BaseModelValidationError)TypeVarUnpack)Sequence)Runtime)ErrorDetailsStateT)defaultContextTzLError: {requested_tool} is not a valid tool, try one of [{available_tools}].z)Error: {error}
 Please fix your mistakes.zvError executing tool '{tool_name}' with kwargs {tool_kwargs} with error:
 {error}
 Please fix the error and try again.zuError invoking tool '{tool_name}' with kwargs {tool_kwargs} with error:
 {error}
 Please fix the error and try again.c                  8    \ rS rSr% SrS\S'   S\S'   S\S'   S	rg
)_ToolCallRequestOverridesx   z9Possible overrides for ToolCallRequest.override() method.r   	tool_callr   r!   r   state N__name__
__module____qualname____firstlineno____doc____annotations____static_attributes__r<       ^/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/langgraph/prebuilt/tool_node.pyr8   r8   x   s    C
NJrE   r8   F)totalc                  ^    \ rS rSr% SrS\S'   S\S'   S\S'   S	\S
'   SS jr    SS jrSrg)ToolCallRequest   aB  Tool execution request passed to tool call interceptors.

Attributes:
    tool_call: Tool call dict with name, args, and id from model output.
    tool: BaseTool instance to be invoked, or None if tool is not
        registered with the `ToolNode`. When tool is `None`, interceptors can
        handle the request without validation. If the interceptor calls `execute()`,
        validation will occur and raise an error for unregistered tools.
    state: Agent state (`dict`, `list`, or `BaseModel`).
    runtime: LangGraph runtime context (optional, `None` if outside graph).
r   r:   BaseTool | Noner!   r   r;   ToolRuntimeruntimec                    SSK n[        U S5      (       a  [        X5      (       d  [        R                  XU5        gUR	                  SU S3[
        SS9  [        R                  XU5        g)zRaise deprecation warning when setting attributes directly.

Direct attribute assignment is deprecated. Use the `override()` method instead.
r   N__dataclass_fields__zSetting attribute 'zt' on ToolCallRequest is deprecated. Use the override() method instead to create a new instance with modified values.   )
stacklevel)warningshasattrobject__setattr__warnDeprecationWarning)selfnamevaluerR   s       rF   rU   ToolCallRequest.__setattr__   sm    
 	 t344GD<O<Ot51MM%dV ,c c"	   t51rE   c                    [        U 40 UD6$ )aU  Replace the request with a new request with the given overrides.

Returns a new `ToolCallRequest` instance with the specified attributes replaced.
This follows an immutable pattern, leaving the original request unchanged.

Args:
    **overrides: Keyword arguments for attributes to override.

        Supported keys:

        - tool_call: Tool call dict with `name`, `args`, and `id`
        - state: Agent state (`dict`, `list`, or `BaseModel`)

Returns:
    New ToolCallRequest instance with specified overrides applied.

Examples:
    ```python
    # Modify tool call arguments without mutating original
    modified_call = {**request.tool_call, "args": {"value": 10}}
    new_request = request.override(tool_call=modified_call)

    # Override multiple attributes
    new_request = request.override(tool_call=modified_call, state=new_state)
    ```
)r	   )rX   	overridess     rF   overrideToolCallRequest.override   s    : t)y))rE   r<   N)rY   strrZ   r   returnNone)r]   z!Unpack[_ToolCallRequestOverrides]ra   rI   )	r>   r?   r@   rA   rB   rC   rU   r^   rD   r<   rE   rF   rI   rI      s:    
 
J2&*<*	*rE   rI   c                  :    \ rS rSr% SrS\S'   S\S'    S\S'   S	rg
)ToolCallWithContexti  a}  ToolCall with additional context for graph state.

This is an internal data structure meant to help the `ToolNode` accept
tool calls with additional context (e.g. state) when dispatched using the
Send API.

The Send API is used in create_agent to distribute tool calls in parallel
and support human-in-the-loop workflows where graph execution may be paused
for an indefinite time.
r   r:   z!Literal['tool_call_with_context']_ToolCallWithContext__typer   r;   r<   Nr=   r<   rE   rF   rd   rd     s$    	 --
 J6rE   rd   c                    [        U [        5      (       d,  [        U [        5      (       a  [        S U  5       5      (       a  U $  [        R
                  " U SS9$ ! [         a    [        U 5      s $ f = f)a<  Convert tool output to `ToolMessage` content format.

Handles `str`, `list[dict]` (content blocks), and arbitrary objects by attempting
JSON serialization with fallback to str().

Args:
    output: Tool execution output of any type.

Returns:
    String or list of content blocks suitable for `ToolMessage.content`.
c              3     #    U  H4  n[        U[        5      =(       a    UR                  S 5      [        ;   v   M6     g7f)typeN)
isinstancedictgetr"   ).0xs     rF   	<genexpr>%msg_content_output.<locals>.<genexpr>?  s4      
 q$MAEE&M5M$MMs   <>F)ensure_ascii)ri   r`   listalljsondumps	Exception)outputs    rF   msg_content_outputrw   1  sn     &#64   

 
 

 
zz&u55 6{s   A A21A2c                  H   ^  \ rS rSrSr S         SU 4S jjjrSrU =r$ )ToolInvocationErroriO  zAn error occurred while invoking a tool due to invalid arguments.

This exception is only raised when invoking a tool using the `ToolNode`!
c                  > Ubm  / nU HS  nSR                  S UR                  SS5       5       5      nUR                  SS5      nUR                  U SU 35        MU     S	R                  U5      n	O[        U5      n	[        R                  XU	S
9U l        Xl        X0l        X l	        X@l
        [        T
U ]1  U R                  5        g)a1  Initialize the ToolInvocationError.

Args:
    tool_name: The name of the tool that failed.
    source: The exception that occurred.
    tool_kwargs: The keyword arguments that were passed to the tool.
    filtered_errors: Optional list of filtered validation errors excluding
        injected arguments.
N.c              3  8   #    U  H  n[        U5      v   M     g 7fN)r`   )rl   locs     rF   rn   /ToolInvocationError.__init__.<locals>.<genexpr>j  s     "L7K3s887Ks   r~   r<   msgzUnknown errorz: 
)	tool_nametool_kwargserror)joinrk   appendr`   TOOL_INVOCATION_ERROR_TEMPLATEformatmessager   r   sourcefiltered_errorssuper__init__)rX   r   r   r   r   error_str_partsr   loc_strr   error_display_str	__class__s             rF   r   ToolInvocationError.__init__U  s    " & O((("Luyy7K"LLii7&&'"SE':; ) !%		/ : #F5<<@Q = 
 #&.&rE   )r   r   r   r   r   r}   )
r   r`   r   r.   r   dict[str, Any]r   zlist[ErrorDetails] | Nonera   rb   )r>   r?   r@   rA   rB   r   rD   __classcell__r   s   @rF   ry   ry   O  sG     6:#'#'  #' $	#'
 3#' 
#' #'rE   ry   c                H    [        U [        5      (       a  U R                  $ U e)zDefault error handler for tool errors.

If the tool is a tool invocation error, return its message.
Otherwise, raise the error.
)ri   ry   r   )es    rF   _default_handle_tool_errorsr   {  s!     !())yy
GrE   c               N   [        U[        [        45      (       d*  [        U[        5      (       a3  [	        U[
        5      (       a  [        R                  [        U 5      S9nU$ [        U[        5      (       a  UnU$ [        U5      (       a
  U" U 5      nU$ SU 3n[        U5      e)a|  Generate error message content based on exception handling configuration.

This function centralizes error message generation logic, supporting different
error handling strategies configured via the `ToolNode`'s `handle_tool_errors`
parameter.

Args:
    e: The exception that occurred during tool execution.
    flag: Configuration for how to handle the error. Can be:
        - bool: If `True`, use default error template
        - str: Use this string as the error message
        - Callable: Call this function with the exception to get error message
        - tuple: Not used in this context (handled by caller)

Returns:
    A string containing the error message to include in the `ToolMessage`.

Raises:
    ValueError: If flag is not one of the supported types.

!!! note
    The tuple case is handled by the caller through exception type checking,
    not by this function directly.
)r   zVGot unexpected type of `handle_tool_error`. Expected bool, str or callable. Received: )ri   booltuplerh   
issubclassru   TOOL_CALL_ERROR_TEMPLATEr   reprr`   callable
ValueError)r   flagcontentr   s       rF   _handle_tool_errorr     s    B $u&&4:dI#>#>*11Q1@ N 
D#		 N 
$q' N	&&*V- 	 orE   c                z   [         R                  " U 5      n[        UR                  R	                  5       5      nU(       a  US   R
                  S;   a  [        U5      S:X  a  US   nOUS   n[        U 5      nUR
                  U;   a  [        UR                  5      nU[        [        4;   aR  [        UR                  5      n[        S U 5       5      (       a  [        U5      $ SUR                   S3n[        U5      eXCR
                     n[         UR"                  ;   a  U4$ SU S3n[        U5      e[         4$ )	a  Infer exception types handled by a custom error handler function.

This function analyzes the type annotations of a custom error handler to determine
which exception types it's designed to handle. This enables type-safe error handling
where only specific exceptions are caught and processed by the handler.

Args:
    handler: A callable that takes an exception and returns an error message string.
            The first parameter (after self/cls if present) should be type-annotated
            with the exception type(s) to handle.

Returns:
    A tuple of exception types that the handler can process. Returns (Exception,)
    if no specific type information is available for backward compatibility.

Raises:
    ValueError: If the handler's annotation contains non-Exception types or
        if Union types contain non-Exception types.

!!! note
    This function supports both single exception types and Union types for
    handlers that need to handle multiple exception types differently.
r   )rX   clsrP      c              3  B   #    U  H  n[        U[        5      v   M     g 7fr}   )r   ru   )rl   args     rF   rn   '_infer_handled_types.<locals>.<genexpr>  s     BTcz#y11T   zAll types in the error handler error annotation must be Exception types. For example, `def custom_handler(e: Union[ValueError, TypeError])`. Got 'z
' instead.a  Arbitrary types are not supported in the error handler signature. Please annotate the error with either a specific Exception type or a union of Exception types. For example, `def custom_handler(e: ValueError)` or `def custom_handler(e: Union[ValueError, TypeError])`. Got ')inspect	signaturerq   
parametersvaluesrY   lenr   r   
annotationr   r
   r   rr   r   r   ru   __mro__)	handlersigparamsfirst_param
type_hintsoriginargsr   exception_types	            rF   _infer_handled_typesr     s:   0 

G
$C#..'')*F!9>>_,V1A )K )K#G,
z) 6 67F%++ 6 67BTBBB ;& (223:?  !o%'(8(89NN222&((
 ''z3  S/! <rE   c                   [        5       nU(       a  UR                  (       a)  UR                  UR                  R                  5       5        UR                  (       a  UR                  UR                  5        UR                  (       a  UR                  UR                  5        / nU R                  5        H  nUS   (       d  M  US   S   U;  d  M  0 UEn[        UR                  S5      [        5      (       a4  US   nUR                  5        VVs0 s H  u  pxXr;  d  M  Xx_M     n	nnXS'   UR                  U5        M     U$ s  snnf )a  Filter validation errors to only include LLM-controlled arguments.

When a tool invocation fails validation, only errors for arguments that the LLM
controls should be included in error messages. This ensures the LLM receives
focused, actionable feedback about parameters it can actually fix. System-injected
arguments (state, store, runtime) are filtered out since the LLM has no control
over them.

This function also removes injected argument values from the `input` field in error
details, ensuring that only LLM-provided arguments appear in error messages.

Args:
    validation_error: The Pydantic ValidationError raised during tool invocation.
    injected_args: The _InjectedArgs structure containing all injected arguments,
        or None if there are no injected arguments.

Returns:
    List of ErrorDetails containing only errors for LLM-controlled arguments,
    with system-injected argument values removed from the input field.
r~   r   input)setr;   updatekeysstoreaddrM   errorsri   rk   rj   itemsr   )
validation_errorinjected_argsinjected_arg_namesr   r   
error_copy
input_dictkv
input_copys
             rF   _filter_validation_errorsr     s%   2 $'5%%m&9&9&>&>&@A""=#6#67  ""=#8#89*,O!((* <<E%LO3EE)2EJ *..1488'0
%/%5%5%7%7TQ1;VDAD%7   '17# "":.! +$ s   E"Ec                  8    \ rS rSr% SrS\S'   S\S'   S\S'   Srg	)
_InjectedArgsi2  a  Internal structure for tracking injected arguments for a tool.

This data structure is built once during ToolNode initialization by analyzing
the tool's signature and args schema, then reused during execution for efficient
injection without repeated reflection.

The structure maps from tool parameter names to their injection sources, enabling
the ToolNode to know exactly which arguments need to be injected and where to
get their values from.

Attributes:
    state: Mapping from tool parameter names to state field names for injection.
        Keys are tool parameter names, values are either:
        - str: Name of the state field to extract and inject
        - None: Inject the entire state object
        Empty dict if no state injection is needed.
    store: Name of the tool parameter where the store should be injected,
        or None if no store injection is needed.
    runtime: Name of the tool parameter where the runtime should be injected,
        or None if no runtime injection is needed.

Example:
    For a tool with signature:
    ```python
    def my_tool(
        x: int,
        messages: Annotated[list, InjectedState("messages")],
        full_state: Annotated[dict, InjectedState()],
        store: Annotated[BaseStore, InjectedStore()],
        runtime: ToolRuntime,
    ) -> str:
        ...
    ```

    The resulting `_InjectedArgs` would be:
    ```python
    _InjectedArgs(
        state={
            "messages": "messages",  # Extract state["messages"]
            "full_state": None,      # Inject entire state
        },
        store="store",               # Inject into "store" parameter
        runtime="runtime",           # Inject into "runtime" parameter
    )
    ```
zdict[str, str | None]r;   
str | Noner   rM   r<   Nr=   r<   rE   rF   r   r   2  s    -^ ! rE   r   c                    ^  \ rS rSr% SrSrS\S'   SS\SSSS.               SU 4S	 jjjr\	SS
 j5       r
        SS jr        SS jr      SS jr        SS jr        SS jr        SS jr        SS jr    SS jrSS jr    S S jr S!       S"S jjr        S#S jrSrU =r$ )$ToolNodeih  u  A node for executing tools in LangGraph workflows.

Handles tool execution patterns including function calls, state injection,
persistent storage, and control flow. Manages parallel execution,
error handling.

Use `ToolNode` when building custom workflows that require fine-grained control over
tool execution—for example, custom routing logic, specialized error handling, or
non-standard agent architectures.

For standard ReAct-style agents, use [`create_agent`][langchain.agents.create_agent]
instead. It uses `ToolNode` internally with sensible defaults for the agent loop,
conditional routing, and error handling.

Input Formats:
    1. **Graph state** with `messages` key that has a list of messages:
        - Common representation for agentic workflows
        - Supports custom messages key via `messages_key` parameter

    2. **Message List**: `[AIMessage(..., tool_calls=[...])]`
        - List of messages with tool calls in the last AIMessage

    3. **Direct Tool Calls**: `[{"name": "tool", "args": {...}, "id": "1", "type": "tool_call"}]`
        - Bypasses message parsing for direct tool execution
        - For programmatic tool invocation and testing

Output Formats:
    Output format depends on input type and tool behavior:

    **For Regular tools**:

    - Dict input → `{"messages": [ToolMessage(...)]}`
    - List input → `[ToolMessage(...)]`

    **For Command tools**:

    - Returns `[Command(...)]` or mixed list with regular tool outputs
    - `Command` can update state, trigger navigation, or send messages

Args:
    tools: A sequence of tools that can be invoked by this node.

        Supports:

        - **BaseTool instances**: Tools with schemas and metadata
        - **Plain functions**: Automatically converted to tools with inferred schemas

    name: The name identifier for this node in the graph. Used for debugging
        and visualization.
    tags: Optional metadata tags to associate with the node for filtering
        and organization.
    handle_tool_errors: Configuration for error handling during tool execution.
        Supports multiple strategies:

        - `True`: Catch all errors and return a `ToolMessage` with the default
            error template containing the exception details.
        - `str`: Catch all errors and return a `ToolMessage` with this custom
            error message string.
        - `type[Exception]`: Only catch exceptions with the specified type and
            return the default error message for it.
        - `tuple[type[Exception], ...]`: Only catch exceptions with the specified
            types and return default error messages for them.
        - `Callable[..., str]`: Catch exceptions matching the callable's signature
            and return the string result of calling it with the exception.
        - `False`: Disable error handling entirely, allowing exceptions to
            propagate.

        Defaults to a callable that:

        - Catches tool invocation errors (due to invalid arguments provided by the
            model) and returns a descriptive error message
        - Ignores tool execution errors (they will be re-raised)

    messages_key: The key in the state dictionary that contains the message list.
        This same key will be used for the output `ToolMessage` objects.

        Allows custom state schemas with different message field names.

Examples:
    Basic usage:

    ```python
    from langchain.tools import ToolNode
    from langchain_core.tools import tool

    @tool
    def calculator(a: int, b: int) -> int:
        """Add two numbers."""
        return a + b

    tool_node = ToolNode([calculator])
    ```

    State injection:

    ```python
    from typing_extensions import Annotated
    from langchain.tools import InjectedState

    @tool
    def context_tool(query: str, state: Annotated[dict, InjectedState]) -> str:
        """Some tool that uses state."""
        return f"Query: {query}, Messages: {len(state['messages'])}"

    tool_node = ToolNode([context_tool])
    ```

    Error handling:

    ```python
    def handle_errors(e: ValueError) -> str:
        return "Invalid input provided"


    tool_node = ToolNode([my_tool], handle_tool_errors=handle_errors)
    ```
toolsr`   rY   Nmessages)rY   tagshandle_tool_errorsmessages_keywrap_tool_callawrap_tool_callc               z  > [         T
U ]  U R                  U R                  X#SS9  0 U l        0 U l        X@l        XPl        X`l        Xpl	        U Hj  n[        U[        5      (       d  [        [        SU5      5      n	OUn	XR                  U	R                  '   [        U	5      U R
                  U	R                  '   Ml     g)a  Initialize `ToolNode` with tools and configuration.

Args:
    tools: Sequence of tools to make available for execution.
    name: Node name for graph identification.
    tags: Optional metadata tags.
    handle_tool_errors: Error handling configuration.
    messages_key: State key containing messages.
    wrap_tool_call: Sync wrapper function to intercept tool execution. Receives
        ToolCallRequest and execute callable, returns ToolMessage or Command.
        Enables retries, caching, request modification, and control flow.
    awrap_tool_call: Async wrapper function to intercept tool execution.
        If not provided, falls back to wrap_tool_call for async execution.
F)rY   r   traceztype[BaseTool]N)r   r   _func_afunc_tools_by_name_injected_args_handle_tool_errors_messages_key_wrap_tool_call_awrap_tool_callri   r   create_toolr   rY   _get_all_injected_args)rX   r   rY   r   r   r   r   r   r!   tool_r   s             rF   r   ToolNode.__init__  s    : 	T[[teT358:#5 )- /DdH--#D)94$@A.3

+.DU.KD

+ rE   c                    U R                   $ )z,Mapping from tool name to BaseTool instance.)r   )rX   s    rF   tools_by_nameToolNode.tools_by_name  s     """rE   c           
        U R                  U5      u  pE[        U[        U5      5      n/ n[        XFSS9 HV  u  pU R	                  U5      n
[        U
US   U	UR                  UR                  UR                  S9nUR                  U5        MX     U/[        U5      -  n[        U5       n[        UR                  U R                  XLU5      5      nS S S 5        U R                  WU5      $ ! , (       d  f       N = fNF)strictid)r;   tool_call_idconfigcontextr   stream_writer)_parse_inputr   r   zip_extract_staterL   r   r   r   r   r   rq   map_run_one_combine_tool_outputs)rX   r   r   rM   
tool_calls
input_typeconfig_listtool_runtimescallcfgr;   tool_runtimeinput_typesexecutoroutputss                  rF   r   ToolNode._func  s     "&!2!25!9
%fc*o> ZUCID''.E&!$Zmm%33L   . D "lS_4$V,T]]J]SG -
 ))':>> -,s   )'C**
C8c           
       #    U R                  U5      u  pE[        U[        U5      5      n/ n[        XFSS9 HV  u  pU R	                  U5      n
[        U
US   U	UR                  UR                  UR                  S9nUR                  U5        MX     / n[        XGSS9 H&  u  pUR                  U R                  XU5      5        M(     [        R                  " U6 I S h  vN nU R                  X5      $  N7fr   )r   r   r   r   r   rL   r   r   r   r   	_arun_oneasynciogatherr   )rX   r   r   rM   r   r   r   r   r   r   r;   r   corosr  s                 rF   r   ToolNode._afunc3  s      "&!2!25!9
%fc*o> ZUCID''.E&!$Zmm%33L   . D "%j"NDLL,GH #O..))'>> /s   CC5C3C5c                   [        S U 5       5      (       d  US:X  a  U$ U R                  U0$ / nS nU GH  n[        U[        5      (       a  UR                  [        R
                  L a  [        UR                  [        5      (       ay  [        S UR                   5       5      (       aX  U(       a-  [        U[        SUR                  5      UR                  -   S9nM  [        [        R
                  UR                  S9nM  UR                  U5        M  UR                  US:X  a  U/OU R                  U/05        GM     U(       a  UR                  U5        U$ )Nc              3  B   #    U  H  n[        U[        5      v   M     g 7fr}   )ri   r*   )rl   rv   s     rF   rn   1ToolNode._combine_tool_outputs.<locals>.<genexpr>Y  s     EW6:fg..Wr   rq   c              3  B   #    U  H  n[        U[        5      v   M     g 7fr}   )ri   r+   )rl   sends     rF   rn   r  j  s     K{tJtT22{r   z
list[Send])goto)graphr  )anyr   ri   r*   r  PARENTr  rq   rr   r	   r   r   )rX   r  r   combined_outputsparent_commandrv   s         rF   r   ToolNode._combine_tool_outputsR  s     EWEEE(F27U9K9KW8UU  	
 *.F&'**LLGNN2"6;;55Kv{{KKK%)0*!%lN4G4G!H6;;!V*
 *1w~~FKK)X$++F3 '' *f 4VH4;M;MPVx:X# * ##N3rE   c                $   UR                   nUR                  nUc.  U R                  U5      =n(       a  U$ SUS    S3n[        U5      eU R	                  XAR
                  U5      n0 UESS0En	  UR                  X5      n
[        U
[.        5      (       a  U R1                  XR                   U5      $ [        U
[,        5      (       a&  [3        S[5        U
R6                  5      5      U
l        U
$ SUS    S[!        U
5       3n[        U5      e! [         aB  nU R                  R                  US   5      n[        X5      n[        US   XS   U5      UeSnAff = f! [         a    e [         Ga  n[        U R                  [         5      (       a-  [#        U R                  [        5      (       a  U R                  4nO[        U R                  [$        5      (       a  U R                  nOV['        U R                  5      (       a5  [        U R                  [         5      (       d  [)        U R                  5      nO[        4nU R                  (       a  [        X5      (       d  e [+        XR                  S9n[-        UUS   US	   S
S9s SnA$ SnAff = f)a  Execute tool call with configured error handling.

Args:
    request: Tool execution request.
    input_type: Input format.
    config: Runnable configuration.

Returns:
    ToolMessage or Command.

Raises:
    Exception: If tool fails and handle_tool_errors is False.
NTool rY     is not registered with ToolNoderh   r:   r   r   r   r   r   rY   r   status
str | list returned unexpected type: )r:   r!   _validate_tool_call	TypeError_inject_tool_argsrM   invoker.   r   rk   r   ry   r'   ru   ri   r   rh   r   r   r   r   r   r   r*   _validate_tool_commandr   rw   r   rX   requestr   r   r   r!   invalid_tool_messager   injected_call	call_argsresponseexcinjectedr   r   handled_typesr   s                    rF   _execute_tool_syncToolNode._execute_tool_sync~  sp   &   || <'+'?'?'EE#E++$v,'GHCC.  ..t__dK:}:fk:	3		;;y9h h((..x9J9JJWWh,,#L2DXEUEU2VWHOd6l^#>tH~>NOnw # ..224<@";C"J)L#F|_$  	 	 $22D99j(()? ? "&!9!9 ;D44e<< $ 8 8$2233J(($= = !5T5M5M N "+ ++:a3O3O )1I1IJG&\!$Z	 /	s7   0D 
E=EEE J0DJ
J
Jc                  ^ ^^	 T R                   R                  US   5      n[        UUUR                  US9nUR                  m	T R
                  c  T R                  UTT	5      $ S	U	UU 4S jjn T R                  XV5      $ ! [         aV  nT R                  (       d  e [        UT R                  S9n[        UUR                  S   UR                  S   SS9s SnA$ SnAff = f)
zExecute single tool call with wrap_tool_call wrapper if configured.

Args:
    call: Tool call dict.
    input_type: Input format.
    tool_runtime: Tool runtime.

Returns:
    ToolMessage or Command.
rY   r:   r!   r;   rM   Nc                *   > TR                  U TT5      $ )>Execute tool with given request. Can be called multiple times.r+  reqr   r   rX   s    rF   execute"ToolNode._run_one.<locals>.execute      **3
FCCrE   r  r   r   r  r3  rI   ra   ToolMessage | Command)r   rk   rI   r;   r   r   r+  ru   r   r   r   r:   )
rX   r   r   r   r!   tool_requestr4  r   r   r   s
   ` `      @rF   r   ToolNode._run_one  s    $ !!%%d6l3 '$$ 	
 $$'**<VLL	D 	D
	''>> 	++(1I1IJG!++F3)33D9	 	s   /B   
C 
ACC C c                @  #    UR                   nUR                  nUc.  U R                  U5      =n(       a  U$ SUS    S3n[        U5      eU R	                  XAR
                  U5      n0 UESS0En	  UR                  X5      I Sh  vN n
[        U
[.        5      (       a  U R1                  XR                   U5      $ [        U
[,        5      (       a&  [3        S[5        U
R6                  5      5      U
l        U
$ SUS    S[!        U
5       3n[        U5      e N! [         aB  nU R                  R                  US   5      n[        X5      n[        US   XS   U5      UeSnAff = f! [         a    e [         Ga  n[        U R                  [         5      (       a-  [#        U R                  [        5      (       a  U R                  4nO[        U R                  [$        5      (       a  U R                  nOV['        U R                  5      (       a5  [        U R                  [         5      (       d  [)        U R                  5      nO[        4nU R                  (       a  [        X5      (       d  e [+        XR                  S9n[-        UUS   US	   S
S9s SnA$ SnAff = f7f)a  Execute tool call asynchronously with configured error handling.

Args:
    request: Tool execution request.
    input_type: Input format.
    config: Runnable configuration.

Returns:
    ToolMessage or Command.

Raises:
    Exception: If tool fails and handle_tool_errors is False.
Nr  rY   r  rh   r:   r   r  r   r   r  r  r  )r:   r!   r  r  r  rM   ainvoker.   r   rk   r   ry   r'   ru   ri   r   rh   r   r   r   r   r   r   r*   r!  r   rw   r   r"  s                    rF   _execute_tool_asyncToolNode._execute_tool_async  sy    &   || <'+'?'?'EE#E++$v,'GHCC.  ..t__dK:}:fk:	3		!%i!@@h h((..x9J9JJWWh,,#L2DXEUEU2VWHOd6l^#>tH~>NOny A" ..224<@";C"J)L#F|_$  	 	 $22D99j(()? ? "&!9!9 ;D44e<< $ 8 8$2233J(($= = !5T5M5M N "+ ++:a3O3O )1I1IJG&\!$Z	 /	sb   A.J2D DD BJD 
E$"=EE$$E' 'J<DJJJJJc                  ^ ^^
#    T R                   R                  US   5      n[        UUUR                  US9nUR                  m
T R
                  c(  T R                  c  T R                  UTT
5      I Sh  vN $ SU
UU 4S jjnSU
UU 4S jjn T R
                  b  T R                  XV5      I Sh  vN $ [        ST R                  5      T l        T R                  XW5      $  Nk N2! [         aV  nT R                  (       d  e [        UT R                  S9n	[        U	UR                  S   UR                  S   S	S
9s SnA$ SnAff = f7f)zExecute single tool call asynchronously with awrap_tool_call wrapper if configured.

Args:
    call: Tool call dict.
    input_type: Input format.
    tool_runtime: Tool runtime.

Returns:
    ToolMessage or Command.
rY   r.  Nc                F   >#    TR                  U TT5      I Sh  vN $  N7f)r0  N)r=  r2  s    rF   r4  #ToolNode._arun_one.<locals>.execute  s!     11#z6JJJJs   !!c                *   > TR                  U TT5      $ )z'Sync execute fallback for sync wrapper.r1  r2  s    rF   _sync_execute)ToolNode._arun_one.<locals>._sync_execute  r6  rE   ToolCallWrapperr  r   r   r  r7  )r   rk   rI   r;   r   r   r   r=  r   ru   r   r   r   r:   )rX   r   r   r   r!   r9  r4  rC  r   r   r   s   ` `       @rF   r  ToolNode._arun_oney  sJ    $ !!%%d6l3 '$$ 	
 $$  (T-A-A-I11,
FSSS	K 	K	D 	D
	$$0!22<III#'(94;O;O#PD ''DD# T J  	++(1I1IJG!++F3)33D9	 	sa   A1E	6C"7E	!C& 1C$2C& 5E	6+C& !E	$C& &
E0AE;E<E	EE	c                   [        U[        5      (       aF  [        US   [        5      (       a)  US   R                  S5      S:X  a  Sn[	        SU5      nX24$ SnUnO[        U[        5      (       a+  UR                  S5      S:X  a  [	        S	U5      nSnUS   /U4$ [        U[        5      (       a&  UR                  U R
                  / 5      =n(       a  S
nO-[        XR
                  / 5      =n(       a  S
nOSn[        U5      e [        S [        U5       5       5      n[        UR                  5      nX24$ ! [         a    Sn[        U5      ef = f)Nrh   r:   r   zlist[ToolCall]rq   __typetool_call_with_contextrd   rj   zNo message found in inputc              3  T   #    U  H  n[        U[        5      (       d  M  Uv   M      g 7fr}   )ri   r   )rl   ms     rF   rn   (ToolNode._parse_input.<locals>.<genexpr>  s      %-aAy1I-s   (	(zNo AIMessage found in input)ri   rq   rj   rk   r   r   getattrr   nextreversedStopIterationr   )rX   r   r   r   r   input_with_ctxr   latest_ai_messages           rF   r   ToolNode._parse_input  sX   
 eT""%)T**uRy}}V/D/S)
!"2E:
!--JHud##		((;?W(W ""7?N%J";/0*<<t$$		$"4"4b99H9J (:(:B??X?J-CS/!	" $ %#H-% ! +667
%%  	"/CS/!	"s   D8 8Ec                    US   nX R                   ;  aT  [        U R                   R                  5       5      n[        R	                  USR                  U5      S9n[        XBUS   SS9$ g )NrY   , )requested_toolavailable_toolsr   r   )rY   r   r  )r   rq   r    INVALID_TOOL_NAME_ERROR_TEMPLATEr   r   r   )rX   r   rW  all_tool_namesr   s        rF   r  ToolNode._validate_tool_call  ss    f!3!33!$"4"4"9"9";<N6==- $		. 9 > G 4:g  rE   c                d    [        U[        5      (       a  UR                  S5      S:X  a  US   $ U$ )zExtract state from input, handling ToolCallWithContext if present.

Args:
    input: The input which may be raw state or ToolCallWithContext.

Returns:
    The actual state to pass to wrap_tool_call wrappers.
rI  rJ  r;   )ri   rj   rk   )rX   r   s     rF   r   ToolNode._extract_state  s1     eT""uyy':>V'V>!rE   c                X   U R                   R                  US   5      nU(       d  Ub  [        U5      nU(       d  U$ [        U5      n0 nUR                  (       Ga`  UR                  n[        U[        5      (       a  [        UR                  R                  5       5      n[        U5      S:X  a  US   U R                  :X  d  US   c  U R                  U0nOdSUS    S3n	[        S UR                  R                  5        5       5      (       a!  SR                  S U 5       5      n
U	S	U
 S
3-  n	[        U	5      e[        U[        5      (       a3  UR                  R                  5        H  u  pU(       a  X|   OUXk'   M     O9UR                  R                  5        H  u  pU(       a  [        X|5      OUXk'   M     UR                   (       a2  UR                   c  Sn[        U5      eUR                   XdR                   '   UR"                  (       a  X&UR"                  '   0 US   EUEUS'   U$ )a>  Inject graph state, store, and runtime into tool call arguments.

This is an internal method that enables tools to access graph context that
should not be controlled by the model. Tools can declare dependencies on graph
state, persistent storage, or runtime context using InjectedState, InjectedStore,
and ToolRuntime annotations. This method automatically identifies these
dependencies and injects the appropriate values.

The injection process preserves the original tool call structure while adding
the necessary context arguments. This allows tools to be both model-callable
and context-aware without exposing internal state management to the model.

Args:
    tool_call: The tool call dictionary to augment with injected arguments.
        Must contain 'name', 'args', 'id', and 'type' fields.
    tool_runtime: The ToolRuntime instance containing all runtime context
        (state, config, store, context, stream_writer) to inject into tools.
    tool: Optional tool instance. When provided, allows injection for
        dynamically registered tools that are not in self.tools_by_name
        (e.g., tools added via middleware's wrap_tool_call).

Returns:
    A new ToolCall dictionary with the same structure as the input but with
    additional arguments injected based on the tool's annotation requirements.

Raises:
    ValueError: If a tool requires store injection but no store is provided,
        or if state injection requirements cannot be satisfied.

!!! note
    This method is called automatically during tool execution. It should not
    be called from outside the `ToolNode`.
rY   r   r   z Invalid input to ToolNode. Tool z$ requires graph state dict as input.c              3  $   #    U  H  ov   M     g 7fr}   r<   )rl   state_fields     rF   rn   -ToolNode._inject_tool_args.<locals>.<genexpr>C  s     R:Q;;:Qs   rV  c              3  6   #    U  H  o(       d  M  Uv   M     g 7fr}   r<   )rl   fs     rF   rn   ra  D  s     7X?aVW?s   
	z State should contain fields r{   zgCannot inject store into tools with InjectedStore annotations - please compile your graph with a store.r   )r   rk   r   r   r;   ri   rq   r   r   r   r  r   r   rj   r   rN  r   rM   )rX   r:   r   r!   r)  tool_call_copyr   r;   required_fieldserr_msgrequired_fields_strtool_argr`  r   s                 rF   r  ToolNode._inject_tool_args   s   N &&**9V+<=D, .d3H#'	? >>> &&E%&&"&x~~'<'<'>"?(A-'*d.@.@@$Q'/!//7E ;9V;L:M N5 6  R(..:O:O:QRRR.2ii7X?7X.X+;<O;PPQR %W-- %&&-5^^-A-A-C)H.9*u "+ .D
 .6^^-A-A-C)H7B3 "+ .D >>!!)>  !o%,8,>,>M..) .:(**+!LN6$:!Lm!LvrE   c                .   [        UR                  [        5      (       a{  US;  a.  SU R                   SUR                   SUS    S3n[	        U5      e[        U5      n[        SUR                  5      =(       d    0 nUR                  U R                  / 5      nO`[        UR                  [        5      (       a?  US:w  a!  S	UR                   SUS    S3n[	        U5      e[        U5      nUR                  nOU$ [        U5      nU[        [        S
9/:X  a  U$ SnU H;  n	[        U	[        5      (       d  M  U	R                  US   :X  d  M/  US   U	l        SnM=     UR                  c+  U(       d$  US:X  a  SOSn
SUS    SU SU
 S3n[	        U5      eU$ )N)rj   r   zFTools can provide a dict in Command.update only when using dict with 'z' key as ToolNode input, got: z for tool 'rY   'r   rq   zpTools can provide a list of messages in Command.update only when using list of messages as ToolNode input, got: )r   Fr   Trj   z]`Command(update={"messages": [ToolMessage("Success", tool_call_id=tool_call_id), ...]}, ...)`zO`Command(update=[ToolMessage("Success", tool_call_id=tool_call_id), ...], ...)`zDExpected to have a matching ToolMessage in Command.update for tool 'z', got: z. Every tool call (LLM requesting to call a tool) in the message history MUST have a corresponding ToolMessage. You can fix it by modifying the tool to return r{   )ri   r   rj   r   r   r   r   rk   rq   r   r   r(   r   r   rY   r  )rX   commandr   r   r   updated_commandstate_updatemessages_updatehas_matching_tool_messager   example_updates              rF   r!  ToolNode._validate_tool_commandg  s    gnnd++ !77!//0 1#NN+;tF|nAG 
 !o%&w/O 0/2H2HIORL*..t/A/A2FO-- V##NN+;tF|nAG 
 !o%&w/O-44ON .o> }0CDEE""$)!&Gg{33##tDz1#F|,0) '   (1J 'SR	 !&\N(?2C DB CQAQQR	T  S/!rE   )r   r   r   r   r   r   )r   zSequence[BaseTool | Callable]rY   r`   r   zlist[str] | Noner   Obool | str | Callable[..., str] | type[Exception] | tuple[type[Exception], ...]r   r`   r   zToolCallWrapper | Noner   zAsyncToolCallWrapper | Nonera   rb   )ra   zdict[str, BaseTool])r   -list[AnyMessage] | dict[str, Any] | BaseModelr   r   rM   r2   ra   r   )r  zlist[ToolMessage | Command]r   %Literal['list', 'dict', 'tool_calls']ra   z@list[Command | list[ToolMessage] | dict[str, list[ToolMessage]]])r#  rI   r   ru  r   r   ra   r8  )r   r   r   ru  r   rL   ra   r8  )r   rt  ra   z<tuple[list[ToolCall], Literal['list', 'dict', 'tool_calls']])r   r   ra   zToolMessage | None)r   rt  ra   rt  r}   )r:   r   r   rL   r!   rK   ra   r   )rl  r*   r   r   r   ru  ra   r*   )r>   r?   r@   rA   rB   rY   rC   r   r   propertyr   r   r   r   r+  r   r=  r  r   r  r   r  r!  rD   r   r   s   @rF   r   r   h  sw   tl D# !%
 )D&157;+L,+L 	+L
 +L&+L +L /+L 5+L 
+L +LZ # #?<? ? 	?
 
?@?<? ? 	?
 
?>* ,*  :*  
J	* X` ` :` 	`
 
`D55 :5 "	5
 
5n` ` :` 	`
 
`D== := "	=
 
=~*&<*& 
F*&XB	6& !%	ee "e 	e
 
eNFF F :	F
 
F FrE   r   c                >   [        U [        5      (       a  U S   nOW[        U [        5      (       a  U R                  U/ 5      =n(       d  [	        X/ 5      =n(       a  US   nOSU  3n[        U5      e[        US5      (       a  [        UR                  5      S:  a  gg)a  Conditional routing function for tool-calling workflows.

This utility function implements the standard conditional logic for ReAct-style
agents: if the last `AIMessage` contains tool calls, route to the tool execution
node; otherwise, end the workflow. This pattern is fundamental to most tool-calling
agent architectures.

The function handles multiple state formats commonly used in LangGraph applications,
making it flexible for different graph designs while maintaining consistent behavior.

Args:
    state: The current graph state to examine for tool calls. Supported formats:
        - Dictionary containing a messages key (for `StateGraph`)
        - `BaseModel` instance with a messages attribute
    messages_key: The key or attribute name containing the message list in the state.
        This allows customization for graphs using different state schemas.

Returns:
    Either `'tools'` if tool calls are present in the last `AIMessage`, or `'__end__'`
        to terminate the workflow. These are the standard routing destinations for
        tool-calling conditional edges.

Raises:
    ValueError: If no messages can be found in the provided state format.

Example:
    Basic usage in a ReAct agent:

    ```python
    from langgraph.graph import StateGraph
    from langchain.tools import ToolNode
    from langchain.tools.tool_node import tools_condition
    from typing_extensions import TypedDict


    class State(TypedDict):
        messages: list


    graph = StateGraph(State)
    graph.add_node("llm", call_model)
    graph.add_node("tools", ToolNode([my_tool]))
    graph.add_conditional_edges(
        "llm",
        tools_condition,  # Routes to "tools" or "__end__"
        {"tools": "tools", "__end__": "__end__"},
    )
    ```

    Custom messages key:

    ```python
    def custom_condition(state):
        return tools_condition(state, messages_key="chat_history")
    ```

!!! note
    This function is designed to work seamlessly with `ToolNode` and standard
    LangGraph patterns. It expects the last message to be an `AIMessage` when
    tool calls are present, which is the standard output format for tool-calling
    language models.
rH  z/No messages found in input state to tool_edge: r   r   r   __end__)	ri   rq   rj   rk   rN  r   rS   r   r   )r;   r   
ai_messager   r   s        rF   tools_conditionrz    s    D %2Y

UD
!
!599\23N'Nx'NE444b\
?wGoz<((S1F1F-G!-KrE   c                  V    \ rS rSr% SrS\S'   S\S'   S\S'   S	\S
'   S\S'   S\S'   Srg)rL   i   a*  Runtime context automatically injected into tools.

!!! note

    This is distinct from `Runtime` (from `langgraph.runtime`), which is injected
    into graph nodes and middleware. `ToolRuntime` includes additional tool-specific
    attributes like `config`, `state`, and `tool_call_id` that `Runtime` does not
    have.

When a tool function has a parameter named `runtime` with type hint
`ToolRuntime`, the tool execution system will automatically inject an instance
containing:

- `state`: The current graph state
- `tool_call_id`: The ID of the current tool call
- `config`: `RunnableConfig` for the current execution
- `context`: Runtime context (shared with `Runtime`)
- `store`: `BaseStore` instance for persistent storage (shared with `Runtime`)
- `stream_writer`: `StreamWriter` for streaming output (shared with `Runtime`)

No `Annotated` wrapper is needed - just use `runtime: ToolRuntime`
as a parameter.

Example:
    ```python
    from langchain_core.tools import tool
    from langchain.tools import ToolRuntime

    @tool
    def my_tool(x: int, runtime: ToolRuntime) -> str:
        """Tool that accesses runtime context."""
        # Access state
        messages = tool_runtime.state["messages"]

        # Access tool_call_id
        print(f"Tool call ID: {tool_runtime.tool_call_id}")

        # Access config
        print(f"Run ID: {tool_runtime.config.get('run_id')}")

        # Access runtime context
        user_id = tool_runtime.context.get("user_id")

        # Access store
        tool_runtime.store.put(("metrics",), "count", 1)

        # Stream output
        tool_runtime.stream_writer.write("Processing...")

        return f"Processed {x}"
    ```

!!! note
    This is a marker class used for type checking and detection.
    The actual runtime object will be constructed during tool execution.
r4   r;   r6   r   r   r   r,   r   r   r   zBaseStore | Noner   r<   Nr=   r<   rE   rF   rL   rL      s-    7r MrE   rL   c                  &    \ rS rSrSrSSS jjrSrg)InjectedStateiC  a%	  Annotation for injecting graph state into tool arguments.

This annotation enables tools to access graph state without exposing state
management details to the language model. Tools annotated with `InjectedState`
receive state data automatically during execution while remaining invisible
to the model's tool-calling interface.

Args:
    field: Optional key to extract from the state dictionary. If `None`, the entire
        state is injected. If specified, only that field's value is injected.
        This allows tools to request specific state components rather than
        processing the full state structure.

Example:
    ```python
    from typing import List
    from typing_extensions import Annotated, TypedDict

    from langchain_core.messages import BaseMessage, AIMessage
    from langchain.tools import InjectedState, ToolNode, tool


    class AgentState(TypedDict):
        messages: List[BaseMessage]
        foo: str


    @tool
    def state_tool(x: int, state: Annotated[dict, InjectedState]) -> str:
        '''Do something with state.'''
        if len(state["messages"]) > 2:
            return state["foo"] + str(x)
        else:
            return "not enough messages"


    @tool
    def foo_tool(x: int, foo: Annotated[str, InjectedState("foo")]) -> str:
        '''Do something else with state.'''
        return foo + str(x + 1)


    node = ToolNode([state_tool, foo_tool])

    tool_call1 = {"name": "state_tool", "args": {"x": 1}, "id": "1", "type": "tool_call"}
    tool_call2 = {"name": "foo_tool", "args": {"x": 1}, "id": "2", "type": "tool_call"}
    state = {
        "messages": [AIMessage("", tool_calls=[tool_call1, tool_call2])],
        "foo": "bar",
    }
    node.invoke(state)
    ```

    ```python
    [
        ToolMessage(content="not enough messages", name="state_tool", tool_call_id="1"),
        ToolMessage(content="bar2", name="foo_tool", tool_call_id="2"),
    ]
    ```

!!! note
    - `InjectedState` arguments are automatically excluded from tool schemas
        presented to language models
    - `ToolNode` handles the injection process during execution
    - Tools can mix regular arguments (controlled by the model) with injected
        arguments (controlled by the system)
    - State injection occurs after the model generates tool calls but before
        tool execution
Nc                    Xl         g)z*Initialize the `InjectedState` annotation.Nfield)rX   r  s     rF   r   InjectedState.__init__  s    
rE   r  r}   )r  r   ra   rb   )r>   r?   r@   rA   rB   r   rD   r<   rE   rF   r}  r}  C  s    DL rE   r}  c                      \ rS rSrSrSrg)InjectedStorei  a	  Annotation for injecting persistent store into tool arguments.

This annotation enables tools to access LangGraph's persistent storage system
without exposing storage details to the language model. Tools annotated with
`InjectedStore` receive the store instance automatically during execution while
remaining invisible to the model's tool-calling interface.

The store provides persistent, cross-session data storage that tools can use
for maintaining context, user preferences, or any other data that needs to
persist beyond individual workflow executions.

!!! warning
    `InjectedStore` annotation requires `langchain-core >= 0.3.8`

Example:
    ```python
    from typing_extensions import Annotated
    from langgraph.store.memory import InMemoryStore
    from langchain.tools import InjectedStore, ToolNode, tool

    @tool
    def save_preference(
        key: str,
        value: str,
        store: Annotated[Any, InjectedStore()]
    ) -> str:
        """Save user preference to persistent storage."""
        store.put(("preferences",), key, value)
        return f"Saved {key} = {value}"

    @tool
    def get_preference(
        key: str,
        store: Annotated[Any, InjectedStore()]
    ) -> str:
        """Retrieve user preference from persistent storage."""
        result = store.get(("preferences",), key)
        return result.value if result else "Not found"
    ```

    Usage with `ToolNode` and graph compilation:

    ```python
    from langgraph.graph import StateGraph
    from langgraph.store.memory import InMemoryStore

    store = InMemoryStore()
    tool_node = ToolNode([save_preference, get_preference])

    graph = StateGraph(State)
    graph.add_node("tools", tool_node)
    compiled_graph = graph.compile(store=store)  # Store is injected automatically
    ```

    Cross-session persistence:

    ```python
    # First session
    result1 = graph.invoke({"messages": [HumanMessage("Save my favorite color as blue")]})

    # Later session - data persists
    result2 = graph.invoke({"messages": [HumanMessage("What's my favorite color?")]})
    ```

!!! note
    - `InjectedStore` arguments are automatically excluded from tool schemas
        presented to language models
    - The store instance is automatically injected by `ToolNode` during execution
    - Tools can access namespaced storage using the store's get/put methods
    - Store injection requires the graph to be compiled with a store instance
    - Multiple tools can share the same store instance for data consistency
r<   N)r>   r?   r@   rA   rB   rD   r<   rE   rF   r  r    s    GrE   r  c                J  ^ [        U T5      (       d&  [        U [        5      (       a  [        U T5      (       a  g[        U 5      nU[        L d	  U[
        L a  [        U4S j[        U 5       5       5      $ Ub,  UTL d&  [        U[        5      (       a  [        UT5      (       a  gg)a  Check if a type argument represents an injection annotation.

This utility function determines whether a type annotation indicates that
an argument should be injected with state or store data. It handles both
direct annotations and nested annotations within Union or Annotated types.

Args:
    type_arg: The type argument to check for injection annotations.
    injection_type: The injection type to look for (InjectedState or InjectedStore).

Returns:
    True if the type argument contains the specified injection annotation.
Tc              3  <   >#    U  H  n[        UT5      v   M     g 7fr}   )_is_injection)rl   tainjection_types     rF   rn    _is_injection.<locals>.<genexpr>  s     R?Q=^44?Qs   F)ri   rh   r   r   r   r   r  r   )type_argr  origin_s    ` rF   r  r    s    " (N++8T""z(N'K'K"G%7i/Rx?QRRR>!w%%*Wn*M*MrE   c                   [        U 5      nU Vs/ s H  n[        X15      (       d  M  UPM     nn[        U5      S:  a  SUR                   SU 3n[	        U5      e[        U5      S:X  a  US   $ [        X5      (       a  ggs  snf )a  Extract injection instance from a type annotation.

Args:
    type_: The type annotation to check.
    injection_type: The injection type to look for.

Returns:
    The injection instance if found, True if injection marker found without instance, None otherwise.
r   z-A tool argument should not be annotated with z more than once. Found: r   TN)r   r  r   r>   r   )type_r  	type_argsr   matchesr   s         rF   _get_injection_from_typer    s     I'Nis=+MsiGN
7|a;N<S<S;T U&&-Y0 	 o
7|qqz	u	-	- Os
   BBc                   U R                  5       n[        U5      n[        U SS5      =(       d    [        U SS5      nU(       a
  [        USS9O0 n0 UEUEn0 nSnSnUR	                  5        H  u  pU	S:X  a  U	n[        U
[        5      =n(       a9  [        U[        5      (       a   UR                  (       a  UR                  Xi'   OSXi'   [        U
[        5      (       a  U	n[        U
[        5      (       d  M  U	nM     [        UUUS9$ )a]  Extract all injected arguments from tool in a single pass.

This function analyzes both the tool's input schema and function signature
to identify all arguments that should be injected (state, store, runtime).

Args:
    tool: The tool to analyze for injection requirements.

Returns:
    _InjectedArgs structure containing all detected injections.
funcN	coroutineT)include_extrasrM   )r;   r   rM   )get_input_schemar%   rN  r   r   r  r}  ri   r  r  rL   r   )r!   full_schemaschema_annotationsr  func_annotationsall_annotations
state_args	store_argruntime_argrY   r  	state_injs               rF   r   r     s    '')K6{C4&J'$T*JDDH~d4@b A)@-?@O )+J I"K&,,.9K 1FF9F)]33	#,??
 #'
  $E=99I $E;77K% /(  rE   )rv   r   ra   zstr | list[dict])r   ru   ra   r`   )r   ru   r   rs  ra   r`   )r   zCallable[..., str]ra   ztuple[type[Exception], ...])r   r.   r   z_InjectedArgs | Nonera   zlist[ErrorDetails])r   )r;   rt  r   r`   ra   zLiteral['tools', '__end__'])r  r   r  1type[InjectedState | InjectedStore | ToolRuntime]ra   r   )r  r   r  r  ra   z
Any | None)r!   r   ra   r   )crB   
__future__r   r  r   rs   collections.abcr   r   r   r   dataclassesr   r	   typesr
   typingr   r   r   r   r   r   r   r   r   r   r   langchain_core.messagesr   r   r   r   r   r   langchain_core.runnables.configr   r   r   langchain_core.toolsr   r    r!   r   langchain_core.tools.baser"   r#   r$   r%   langgraph._internal._runnabler&   langgraph.errorsr'   langgraph.graph.messager(   langgraph.store.baser)   langgraph.typesr*   r+   r,   pydanticr-   r.   typing_extensionsr/   r0   r1   langgraph.runtimer2   pydantic_corer3   rj   r4   r6   rY  r   TOOL_EXECUTION_ERROR_TEMPLATEr   r8   rI   rE  AsyncToolCallWrapperrd   rw   ry   r   r   r   r   r   r   rz  rL   r}  r  r  r  r   r<   rE   rF   <module>r     s  %N #    /  *      
 ; 4  ; * 7 * 7 7 / -()*
 
4	(:t, S ! H + + 	  B* B* B*J h0+2GGHI'GR  h0)K'<Q2RRSTkG#$&  E7) 7.<)'- )'X//"/ 	/d?D5%5'5 5p 2 2 2jE ET #M8MM !M` ?*GHf4D,E ? ?DIO IXHO HVE 
B Q<4rE   