Home / Function/ parse_ai_message() — langchain Function Reference

parse_ai_message() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  8dd0a3f1_1bf7_bed3_d0f0_65a68d3ae18c["parse_ai_message()"]
  4dc989ff_9a85_a857_d208_5bc8b6a443dc["OpenAIFunctionsAgentOutputParser"]
  8dd0a3f1_1bf7_bed3_d0f0_65a68d3ae18c -->|defined in| 4dc989ff_9a85_a857_d208_5bc8b6a443dc
  284268df_49ac_b85d_6344_8b9a9c1d043b["parse_result()"]
  284268df_49ac_b85d_6344_8b9a9c1d043b -->|calls| 8dd0a3f1_1bf7_bed3_d0f0_65a68d3ae18c
  style 8dd0a3f1_1bf7_bed3_d0f0_65a68d3ae18c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/output_parsers/openai_functions.py lines 33–80

    def parse_ai_message(message: BaseMessage) -> AgentAction | AgentFinish:
        """Parse an AI message."""
        if not isinstance(message, AIMessage):
            msg = f"Expected an AI message got {type(message)}"
            raise TypeError(msg)

        function_call = message.additional_kwargs.get("function_call", {})

        if function_call:
            function_name = function_call["name"]
            try:
                if len(function_call["arguments"].strip()) == 0:
                    # OpenAI returns an empty string for functions containing no args
                    _tool_input = {}
                else:
                    # otherwise it returns a json object
                    _tool_input = json.loads(function_call["arguments"], strict=False)
            except JSONDecodeError as e:
                msg = (
                    f"Could not parse tool input: {function_call} because "
                    f"the `arguments` is not valid JSON."
                )
                raise OutputParserException(msg) from e

            # A hack here:
            # The code that encodes tool input into Open AI uses a special variable
            # name called `__arg1` to handle old style tools that do not expose a
            # schema and expect a single string argument as an input.
            # We unpack the argument here if it exists.
            # Open AI does not support passing in a JSON array as an argument.
            if "__arg1" in _tool_input:
                tool_input = _tool_input["__arg1"]
            else:
                tool_input = _tool_input

            content_msg = f"responded: {message.content}\n" if message.content else "\n"
            log = f"\nInvoking: `{function_name}` with `{tool_input}`\n{content_msg}\n"
            return AgentActionMessageLog(
                tool=function_name,
                tool_input=tool_input,
                log=log,
                message_log=[message],
            )

        return AgentFinish(
            return_values={"output": message.content},
            log=str(message.content),
        )

Subdomains

Called By

Frequently Asked Questions

What does parse_ai_message() do?
parse_ai_message() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/agents/output_parsers/openai_functions.py.
Where is parse_ai_message() defined?
parse_ai_message() is defined in libs/langchain/langchain_classic/agents/output_parsers/openai_functions.py at line 33.
What calls parse_ai_message()?
parse_ai_message() is called by 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