Home / Function/ _to_args_and_kwargs() — langchain Function Reference

_to_args_and_kwargs() — langchain Function Reference

Architecture documentation for the _to_args_and_kwargs() function in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  89d802c7_d6ac_3fc5_68f4_5fa72c2afc76["_to_args_and_kwargs()"]
  5ebe56ae_0ac8_cb13_b5a9_ee567b924009["BaseTool"]
  89d802c7_d6ac_3fc5_68f4_5fa72c2afc76 -->|defined in| 5ebe56ae_0ac8_cb13_b5a9_ee567b924009
  51b059af_20e0_9233_077d_cd590dd94dc8["run()"]
  51b059af_20e0_9233_077d_cd590dd94dc8 -->|calls| 89d802c7_d6ac_3fc5_68f4_5fa72c2afc76
  c2f33b98_e5b2_c424_d7d3_8cbd05da78d1["arun()"]
  c2f33b98_e5b2_c424_d7d3_8cbd05da78d1 -->|calls| 89d802c7_d6ac_3fc5_68f4_5fa72c2afc76
  0f980582_36e3_bd64_ec07_fc1d2414d385["_parse_input()"]
  89d802c7_d6ac_3fc5_68f4_5fa72c2afc76 -->|calls| 0f980582_36e3_bd64_ec07_fc1d2414d385
  style 89d802c7_d6ac_3fc5_68f4_5fa72c2afc76 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/tools/base.py lines 838–875

    def _to_args_and_kwargs(
        self, tool_input: str | dict, tool_call_id: str | None
    ) -> tuple[tuple, dict]:
        """Convert tool input to positional and keyword arguments.

        Args:
            tool_input: The input to the tool.
            tool_call_id: The ID of the tool call, if available.

        Returns:
            A tuple of `(positional_args, keyword_args)` for the tool.

        Raises:
            TypeError: If the tool input type is invalid.
        """
        if (
            self.args_schema is not None
            and isinstance(self.args_schema, type)
            and is_basemodel_subclass(self.args_schema)
            and not get_fields(self.args_schema)
        ):
            # StructuredTool with no args
            return (), {}
        tool_input = self._parse_input(tool_input, tool_call_id)
        # For backwards compatibility, if run_input is a string,
        # pass as a positional argument.
        if isinstance(tool_input, str):
            return (tool_input,), {}
        if isinstance(tool_input, dict):
            # Make a shallow copy of the input to allow downstream code
            # to modify the root level of the input without affecting the
            # original input.
            # This is used by the tool to inject run time information like
            # the callback manager.
            return (), tool_input.copy()
        # This code path is not expected to be reachable.
        msg = f"Invalid tool input type: {type(tool_input)}"
        raise TypeError(msg)

Subdomains

Called By

Frequently Asked Questions

What does _to_args_and_kwargs() do?
_to_args_and_kwargs() is a function in the langchain codebase, defined in libs/core/langchain_core/tools/base.py.
Where is _to_args_and_kwargs() defined?
_to_args_and_kwargs() is defined in libs/core/langchain_core/tools/base.py at line 838.
What does _to_args_and_kwargs() call?
_to_args_and_kwargs() calls 1 function(s): _parse_input.
What calls _to_args_and_kwargs()?
_to_args_and_kwargs() is called by 2 function(s): arun, run.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free