Home / Function/ parse_result() — langchain Function Reference

parse_result() — langchain Function Reference

Architecture documentation for the parse_result() function in openai_tools.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  39c4ea69_6a74_bb81_d201_8124b23279c8["parse_result()"]
  4ba49395_4286_3792_e35b_973ad01106e4["PydanticToolsParser"]
  39c4ea69_6a74_bb81_d201_8124b23279c8 -->|defined in| 4ba49395_4286_3792_e35b_973ad01106e4
  6362cf2f_ac6d_027b_84a9_3cafe28b4ba5["parse_result()"]
  39c4ea69_6a74_bb81_d201_8124b23279c8 -->|calls| 6362cf2f_ac6d_027b_84a9_3cafe28b4ba5
  style 39c4ea69_6a74_bb81_d201_8124b23279c8 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/output_parsers/openai_tools.py lines 314–384

    def parse_result(self, result: list[Generation], *, partial: bool = False) -> Any:
        """Parse the result of an LLM call to a list of Pydantic objects.

        Args:
            result: The result of the LLM call.
            partial: Whether to parse partial JSON.

                If `True`, the output will be a JSON object containing all the keys that
                have been returned so far.

                If `False`, the output will be the full JSON object.

        Returns:
            The parsed Pydantic objects.

        Raises:
            ValueError: If the tool call arguments are not a dict.
            ValidationError: If the tool call arguments do not conform to the Pydantic
                model.
        """
        json_results = super().parse_result(result, partial=partial)
        if not json_results:
            return None if self.first_tool_only else []

        json_results = [json_results] if self.first_tool_only else json_results
        name_dict_v2: dict[str, TypeBaseModel] = {
            tool.model_config.get("title") or tool.__name__: tool
            for tool in self.tools
            if is_pydantic_v2_subclass(tool)
        }
        name_dict_v1: dict[str, TypeBaseModel] = {
            tool.__name__: tool for tool in self.tools if is_pydantic_v1_subclass(tool)
        }
        name_dict: dict[str, TypeBaseModel] = {**name_dict_v2, **name_dict_v1}
        pydantic_objects = []
        for res in json_results:
            if not isinstance(res["args"], dict):
                if partial:
                    continue
                msg = (
                    f"Tool arguments must be specified as a dict, received: "
                    f"{res['args']}"
                )
                raise ValueError(msg)

            try:
                tool = name_dict[res["type"]]
            except KeyError as e:
                available = ", ".join(name_dict.keys()) or "<no_tools>"
                msg = (
                    f"Unknown tool type: {res['type']!r}. Available tools: {available}"
                )
                raise OutputParserException(msg) from e

            try:
                pydantic_objects.append(tool(**res["args"]))
            except (ValidationError, ValueError):
                if partial:
                    continue
                has_max_tokens_stop_reason = any(
                    generation.message.response_metadata.get("stop_reason")
                    == "max_tokens"
                    for generation in result
                    if isinstance(generation, ChatGeneration)
                )
                if has_max_tokens_stop_reason:
                    logger.exception(_MAX_TOKENS_ERROR)
                raise
        if self.first_tool_only:
            return pydantic_objects[0] if pydantic_objects else None
        return pydantic_objects

Domain

Subdomains

Frequently Asked Questions

What does parse_result() do?
parse_result() is a function in the langchain codebase, defined in libs/core/langchain_core/output_parsers/openai_tools.py.
Where is parse_result() defined?
parse_result() is defined in libs/core/langchain_core/output_parsers/openai_tools.py at line 314.
What does parse_result() call?
parse_result() calls 1 function(s): parse_result.

Analyze Your Own Codebase

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

Try Supermodel Free