Home / File/ output_parser.py — langchain Source File

output_parser.py — langchain Source File

Architecture documentation for output_parser.py, a python file in the langchain codebase. 5 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  e19c3095_7c11_1cbd_db92_84d82e0878fb["output_parser.py"]
  59e0d3b0_0f8e_4b79_d442_e9b4821561c7["langchain_core.agents"]
  e19c3095_7c11_1cbd_db92_84d82e0878fb --> 59e0d3b0_0f8e_4b79_d442_e9b4821561c7
  049d69ec_d53a_d170_b6fa_35c395793702["langchain_core.exceptions"]
  e19c3095_7c11_1cbd_db92_84d82e0878fb --> 049d69ec_d53a_d170_b6fa_35c395793702
  1e4f206f_c5d6_57b9_9194_b86d8eff032d["langchain_core.utils.json"]
  e19c3095_7c11_1cbd_db92_84d82e0878fb --> 1e4f206f_c5d6_57b9_9194_b86d8eff032d
  138c8655_8752_7665_c90c_fe40e072df42["langchain_classic.agents"]
  e19c3095_7c11_1cbd_db92_84d82e0878fb --> 138c8655_8752_7665_c90c_fe40e072df42
  f4cd6540_469f_5788_59b7_6ab3c2723621["langchain_classic.agents.conversational_chat.prompt"]
  e19c3095_7c11_1cbd_db92_84d82e0878fb --> f4cd6540_469f_5788_59b7_6ab3c2723621
  style e19c3095_7c11_1cbd_db92_84d82e0878fb fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from __future__ import annotations

from langchain_core.agents import AgentAction, AgentFinish
from langchain_core.exceptions import OutputParserException
from langchain_core.utils.json import parse_json_markdown

from langchain_classic.agents import AgentOutputParser
from langchain_classic.agents.conversational_chat.prompt import FORMAT_INSTRUCTIONS


# Define a class that parses output for conversational agents
class ConvoOutputParser(AgentOutputParser):
    """Output parser for the conversational agent."""

    format_instructions: str = FORMAT_INSTRUCTIONS
    """Default formatting instructions"""

    def get_format_instructions(self) -> str:
        """Returns formatting instructions for the given output parser."""
        return self.format_instructions

    def parse(self, text: str) -> AgentAction | AgentFinish:
        """Attempts to parse the given text into an AgentAction or AgentFinish.

        Raises:
             OutputParserException if parsing fails.
        """
        try:
            # Attempt to parse the text into a structured format (assumed to be JSON
            # stored as markdown)
            response = parse_json_markdown(text)

            # If the response contains an 'action' and 'action_input'
            if "action" in response and "action_input" in response:
                action, action_input = response["action"], response["action_input"]

                # If the action indicates a final answer, return an AgentFinish
                if action == "Final Answer":
                    return AgentFinish({"output": action_input}, text)
                # Otherwise, return an AgentAction with the specified action and
                # input
                return AgentAction(action, action_input, text)
            # If the necessary keys aren't present in the response, raise an
            # exception
            msg = f"Missing 'action' or 'action_input' in LLM output: {text}"
            raise OutputParserException(msg)
        except Exception as e:
            # If any other exception is raised during parsing, also raise an
            # OutputParserException
            msg = f"Could not parse LLM output: {text}"
            raise OutputParserException(msg) from e

    @property
    def _type(self) -> str:
        return "conversational_chat"

Subdomains

Dependencies

  • langchain_classic.agents
  • langchain_classic.agents.conversational_chat.prompt
  • langchain_core.agents
  • langchain_core.exceptions
  • langchain_core.utils.json

Frequently Asked Questions

What does output_parser.py do?
output_parser.py is a source file in the langchain codebase, written in python. It belongs to the AgentOrchestration domain, ClassicChains subdomain.
What does output_parser.py depend on?
output_parser.py imports 5 module(s): langchain_classic.agents, langchain_classic.agents.conversational_chat.prompt, langchain_core.agents, langchain_core.exceptions, langchain_core.utils.json.
Where is output_parser.py in the architecture?
output_parser.py is located at libs/langchain/langchain_classic/agents/conversational_chat/output_parser.py (domain: AgentOrchestration, subdomain: ClassicChains, directory: libs/langchain/langchain_classic/agents/conversational_chat).

Analyze Your Own Codebase

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

Try Supermodel Free