Home / Function/ _parse_input() — langchain Function Reference

_parse_input() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  0f980582_36e3_bd64_ec07_fc1d2414d385["_parse_input()"]
  5ebe56ae_0ac8_cb13_b5a9_ee567b924009["BaseTool"]
  0f980582_36e3_bd64_ec07_fc1d2414d385 -->|defined in| 5ebe56ae_0ac8_cb13_b5a9_ee567b924009
  89d802c7_d6ac_3fc5_68f4_5fa72c2afc76["_to_args_and_kwargs()"]
  89d802c7_d6ac_3fc5_68f4_5fa72c2afc76 -->|calls| 0f980582_36e3_bd64_ec07_fc1d2414d385
  967c88a4_f24b_828c_a121_3b72c0a58b0b["get_all_basemodel_annotations()"]
  0f980582_36e3_bd64_ec07_fc1d2414d385 -->|calls| 967c88a4_f24b_828c_a121_3b72c0a58b0b
  5c55c769_8605_2bfb_879c_38c645ebb4f8["_is_injected_arg_type()"]
  0f980582_36e3_bd64_ec07_fc1d2414d385 -->|calls| 5c55c769_8605_2bfb_879c_38c645ebb4f8
  style 0f980582_36e3_bd64_ec07_fc1d2414d385 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/tools/base.py lines 656–775

    def _parse_input(
        self, tool_input: str | dict, tool_call_id: str | None
    ) -> str | dict[str, Any]:
        """Parse and validate tool input using the args schema.

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

        Returns:
            The parsed and validated input.

        Raises:
            ValueError: If `string` input is provided with JSON schema `args_schema`.
            ValueError: If `InjectedToolCallId` is required but `tool_call_id` is not
                provided.
            TypeError: If `args_schema` is not a Pydantic `BaseModel` or dict.
        """
        input_args = self.args_schema

        if isinstance(tool_input, str):
            if input_args is not None:
                if isinstance(input_args, dict):
                    msg = (
                        "String tool inputs are not allowed when "
                        "using tools with JSON schema args_schema."
                    )
                    raise ValueError(msg)
                key_ = next(iter(get_fields(input_args).keys()))
                if issubclass(input_args, BaseModel):
                    input_args.model_validate({key_: tool_input})
                elif issubclass(input_args, BaseModelV1):
                    input_args.parse_obj({key_: tool_input})
                else:
                    msg = f"args_schema must be a Pydantic BaseModel, got {input_args}"
                    raise TypeError(msg)
            return tool_input

        if input_args is not None:
            if isinstance(input_args, dict):
                return tool_input
            if issubclass(input_args, BaseModel):
                # Check args_schema for InjectedToolCallId
                for k, v in get_all_basemodel_annotations(input_args).items():
                    if _is_injected_arg_type(v, injected_type=InjectedToolCallId):
                        if tool_call_id is None:
                            msg = (
                                "When tool includes an InjectedToolCallId "
                                "argument, tool must always be invoked with a full "
                                "model ToolCall of the form: {'args': {...}, "
                                "'name': '...', 'type': 'tool_call', "
                                "'tool_call_id': '...'}"
                            )
                            raise ValueError(msg)
                        tool_input[k] = tool_call_id
                result = input_args.model_validate(tool_input)
                result_dict = result.model_dump()
            elif issubclass(input_args, BaseModelV1):
                # Check args_schema for InjectedToolCallId
                for k, v in get_all_basemodel_annotations(input_args).items():
                    if _is_injected_arg_type(v, injected_type=InjectedToolCallId):
                        if tool_call_id is None:
                            msg = (
                                "When tool includes an InjectedToolCallId "
                                "argument, tool must always be invoked with a full "
                                "model ToolCall of the form: {'args': {...}, "
                                "'name': '...', 'type': 'tool_call', "
                                "'tool_call_id': '...'}"
                            )
                            raise ValueError(msg)
                        tool_input[k] = tool_call_id
                result = input_args.parse_obj(tool_input)
                result_dict = result.dict()
            else:
                msg = (
                    f"args_schema must be a Pydantic BaseModel, got {self.args_schema}"
                )
                raise NotImplementedError(msg)

            # Include fields from tool_input, plus fields with explicit defaults.
            # This applies Pydantic defaults (like Field(default=1)) while excluding

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free