Home / Function/ parse_result() — langchain Function Reference

parse_result() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  4a07aa41_64ab_8daa_7921_549a6341be1b["parse_result()"]
  6e7de992_0082_1b36_9cd5_7cc5d7de4879["JsonOutputFunctionsParser"]
  4a07aa41_64ab_8daa_7921_549a6341be1b -->|defined in| 6e7de992_0082_1b36_9cd5_7cc5d7de4879
  style 4a07aa41_64ab_8daa_7921_549a6341be1b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/output_parsers/openai_functions.py lines 80–141

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

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

        Returns:
            The parsed JSON object.

        Raises:
            OutputParserException: If the output is not valid JSON.
        """
        if len(result) != 1:
            msg = f"Expected exactly one result, but got {len(result)}"
            raise OutputParserException(msg)
        generation = result[0]
        if not isinstance(generation, ChatGeneration):
            msg = "This output parser can only be used with a chat generation."
            raise OutputParserException(msg)
        message = generation.message
        try:
            function_call = message.additional_kwargs["function_call"]
        except KeyError as exc:
            if partial:
                return None
            msg = f"Could not parse function call: {exc}"
            raise OutputParserException(msg) from exc
        try:
            if partial:
                try:
                    if self.args_only:
                        return parse_partial_json(
                            function_call["arguments"], strict=self.strict
                        )
                    return {
                        **function_call,
                        "arguments": parse_partial_json(
                            function_call["arguments"], strict=self.strict
                        ),
                    }
                except json.JSONDecodeError:
                    return None
            elif self.args_only:
                try:
                    return json.loads(function_call["arguments"], strict=self.strict)
                except (json.JSONDecodeError, TypeError) as exc:
                    msg = f"Could not parse function call data: {exc}"
                    raise OutputParserException(msg) from exc
            else:
                try:
                    return {
                        **function_call,
                        "arguments": json.loads(
                            function_call["arguments"], strict=self.strict
                        ),
                    }
                except (json.JSONDecodeError, TypeError) as exc:
                    msg = f"Could not parse function call data: {exc}"
                    raise OutputParserException(msg) from exc
        except KeyError:
            return None

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_functions.py.
Where is parse_result() defined?
parse_result() is defined in libs/core/langchain_core/output_parsers/openai_functions.py at line 80.

Analyze Your Own Codebase

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

Try Supermodel Free