output_parser.py — langchain Source File
Architecture documentation for output_parser.py, a python file in the langchain codebase. 6 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR ddc27fdd_9d1e_cd56_1c6a_9d0e974eb220["output_parser.py"] 7025b240_fdc3_cf68_b72f_f41dac94566b["json"] ddc27fdd_9d1e_cd56_1c6a_9d0e974eb220 --> 7025b240_fdc3_cf68_b72f_f41dac94566b 67ec3255_645e_8b6e_1eff_1eb3c648ed95["re"] ddc27fdd_9d1e_cd56_1c6a_9d0e974eb220 --> 67ec3255_645e_8b6e_1eff_1eb3c648ed95 80d582c5_7cc3_ac96_2742_3dbe1cbd4e2b["langchain_core.agents"] ddc27fdd_9d1e_cd56_1c6a_9d0e974eb220 --> 80d582c5_7cc3_ac96_2742_3dbe1cbd4e2b 75137834_4ba7_dc43_7ec5_182c05eceedf["langchain_core.exceptions"] ddc27fdd_9d1e_cd56_1c6a_9d0e974eb220 --> 75137834_4ba7_dc43_7ec5_182c05eceedf e160f068_75de_4342_6673_9969b919de85["langchain_classic.agents.agent"] ddc27fdd_9d1e_cd56_1c6a_9d0e974eb220 --> e160f068_75de_4342_6673_9969b919de85 f83917be_1f84_fd52_efc7_b94ca46a954e["langchain_classic.agents.chat.prompt"] ddc27fdd_9d1e_cd56_1c6a_9d0e974eb220 --> f83917be_1f84_fd52_efc7_b94ca46a954e style ddc27fdd_9d1e_cd56_1c6a_9d0e974eb220 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import json
import re
from re import Pattern
from langchain_core.agents import AgentAction, AgentFinish
from langchain_core.exceptions import OutputParserException
from langchain_classic.agents.agent import AgentOutputParser
from langchain_classic.agents.chat.prompt import FORMAT_INSTRUCTIONS
FINAL_ANSWER_ACTION = "Final Answer:"
class ChatOutputParser(AgentOutputParser):
"""Output parser for the chat agent."""
format_instructions: str = FORMAT_INSTRUCTIONS
"""Default formatting instructions"""
pattern: Pattern = re.compile(r"^.*?`{3}(?:json)?\n(.*?)`{3}.*?$", re.DOTALL)
"""Regex pattern to parse the output."""
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:
"""Parse the output from the agent into an AgentAction or AgentFinish object.
Args:
text: The text to parse.
Returns:
An AgentAction or AgentFinish object.
Raises:
OutputParserException: If the output could not be parsed.
ValueError: If the action could not be found.
"""
includes_answer = FINAL_ANSWER_ACTION in text
try:
found = self.pattern.search(text)
if not found:
# Fast fail to parse Final Answer.
msg = "action not found"
raise ValueError(msg)
action = found.group(1)
response = json.loads(action.strip())
includes_action = "action" in response
if includes_answer and includes_action:
msg = (
"Parsing LLM output produced a final answer "
f"and a parse-able action: {text}"
)
raise OutputParserException(msg)
return AgentAction(
response["action"],
response.get("action_input", {}),
text,
)
except Exception as exc:
if not includes_answer:
msg = f"Could not parse LLM output: {text}"
raise OutputParserException(msg) from exc
output = text.rsplit(FINAL_ANSWER_ACTION, maxsplit=1)[-1].strip()
return AgentFinish({"output": output}, text)
@property
def _type(self) -> str:
return "chat"
Domain
Subdomains
Classes
Dependencies
- json
- langchain_classic.agents.agent
- langchain_classic.agents.chat.prompt
- langchain_core.agents
- langchain_core.exceptions
- re
Source
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 6 module(s): json, langchain_classic.agents.agent, langchain_classic.agents.chat.prompt, langchain_core.agents, langchain_core.exceptions, re.
Where is output_parser.py in the architecture?
output_parser.py is located at libs/langchain/langchain_classic/agents/chat/output_parser.py (domain: AgentOrchestration, subdomain: ToolInterface, directory: libs/langchain/langchain_classic/agents/chat).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free