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
  30aa90f1_0751_7049_033f_08f0b1613c9b["output_parser.py"]
  67ec3255_645e_8b6e_1eff_1eb3c648ed95["re"]
  30aa90f1_0751_7049_033f_08f0b1613c9b --> 67ec3255_645e_8b6e_1eff_1eb3c648ed95
  80d582c5_7cc3_ac96_2742_3dbe1cbd4e2b["langchain_core.agents"]
  30aa90f1_0751_7049_033f_08f0b1613c9b --> 80d582c5_7cc3_ac96_2742_3dbe1cbd4e2b
  75137834_4ba7_dc43_7ec5_182c05eceedf["langchain_core.exceptions"]
  30aa90f1_0751_7049_033f_08f0b1613c9b --> 75137834_4ba7_dc43_7ec5_182c05eceedf
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  30aa90f1_0751_7049_033f_08f0b1613c9b --> 91721f45_4909_e489_8c1f_084f8bd87145
  e160f068_75de_4342_6673_9969b919de85["langchain_classic.agents.agent"]
  30aa90f1_0751_7049_033f_08f0b1613c9b --> e160f068_75de_4342_6673_9969b919de85
  style 30aa90f1_0751_7049_033f_08f0b1613c9b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import re

from langchain_core.agents import AgentAction, AgentFinish
from langchain_core.exceptions import OutputParserException
from typing_extensions import override

from langchain_classic.agents.agent import AgentOutputParser


class ReActOutputParser(AgentOutputParser):
    """Output parser for the ReAct agent."""

    @override
    def parse(self, text: str) -> AgentAction | AgentFinish:
        action_prefix = "Action: "
        if not text.strip().split("\n")[-1].startswith(action_prefix):
            msg = f"Could not parse LLM Output: {text}"
            raise OutputParserException(msg)
        action_block = text.strip().split("\n")[-1]

        action_str = action_block[len(action_prefix) :]
        # Parse out the action and the directive.
        re_matches = re.search(r"(.*?)\[(.*?)\]", action_str)
        if re_matches is None:
            msg = f"Could not parse action directive: {action_str}"
            raise OutputParserException(msg)
        action, action_input = re_matches.group(1), re_matches.group(2)
        if action == "Finish":
            return AgentFinish({"output": action_input}, text)
        return AgentAction(action, action_input, text)

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

Subdomains

Dependencies

  • langchain_classic.agents.agent
  • langchain_core.agents
  • langchain_core.exceptions
  • re
  • typing_extensions

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, ToolInterface subdomain.
What does output_parser.py depend on?
output_parser.py imports 5 module(s): langchain_classic.agents.agent, langchain_core.agents, langchain_core.exceptions, re, typing_extensions.
Where is output_parser.py in the architecture?
output_parser.py is located at libs/langchain/langchain_classic/agents/react/output_parser.py (domain: AgentOrchestration, subdomain: ToolInterface, directory: libs/langchain/langchain_classic/agents/react).

Analyze Your Own Codebase

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

Try Supermodel Free