StructuredChatOutputParser Class — langchain Architecture
Architecture documentation for the StructuredChatOutputParser class in output_parser.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 2a95d029_9ce2_559a_374e_c9256ca9f87e["StructuredChatOutputParser"] d9685a4b_26b0_bca9_f857_03bb2ffc9dd4["AgentOutputParser"] 2a95d029_9ce2_559a_374e_c9256ca9f87e -->|extends| d9685a4b_26b0_bca9_f857_03bb2ffc9dd4 2617733a_ade1_11ca_e0e5_eb0cecb70df6["output_parser.py"] 2a95d029_9ce2_559a_374e_c9256ca9f87e -->|defined in| 2617733a_ade1_11ca_e0e5_eb0cecb70df6 8ae1b605_5f96_9c45_9d6e_e0bd57a87dd3["get_format_instructions()"] 2a95d029_9ce2_559a_374e_c9256ca9f87e -->|method| 8ae1b605_5f96_9c45_9d6e_e0bd57a87dd3 c8b3b8b6_c924_2b06_cfd4_9d645b98239b["parse()"] 2a95d029_9ce2_559a_374e_c9256ca9f87e -->|method| c8b3b8b6_c924_2b06_cfd4_9d645b98239b c5bb2aea_f732_f6d1_d7de_99e5d9bf8826["_type()"] 2a95d029_9ce2_559a_374e_c9256ca9f87e -->|method| c5bb2aea_f732_f6d1_d7de_99e5d9bf8826
Relationship Graph
Source Code
libs/langchain/langchain_classic/agents/structured_chat/output_parser.py lines 21–59
class StructuredChatOutputParser(AgentOutputParser):
"""Output parser for the structured chat agent."""
format_instructions: str = FORMAT_INSTRUCTIONS
"""Default formatting instructions"""
pattern: Pattern = re.compile(r"```(?:json\s+)?(\W.*?)```", re.DOTALL)
"""Regex pattern to parse the output."""
@override
def get_format_instructions(self) -> str:
"""Returns formatting instructions for the given output parser."""
return self.format_instructions
@override
def parse(self, text: str) -> AgentAction | AgentFinish:
try:
action_match = self.pattern.search(text)
if action_match is not None:
response = json.loads(action_match.group(1).strip(), strict=False)
if isinstance(response, list):
# gpt turbo frequently ignores the directive to emit a single action
logger.warning("Got multiple action responses: %s", response)
response = response[0]
if response["action"] == "Final Answer":
return AgentFinish({"output": response["action_input"]}, text)
return AgentAction(
response["action"],
response.get("action_input", {}),
text,
)
return AgentFinish({"output": text}, text)
except Exception as e:
msg = f"Could not parse LLM output: {text}"
raise OutputParserException(msg) from e
@property
def _type(self) -> str:
return "structured_chat"
Extends
Source
Frequently Asked Questions
What is the StructuredChatOutputParser class?
StructuredChatOutputParser is a class in the langchain codebase, defined in libs/langchain/langchain_classic/agents/structured_chat/output_parser.py.
Where is StructuredChatOutputParser defined?
StructuredChatOutputParser is defined in libs/langchain/langchain_classic/agents/structured_chat/output_parser.py at line 21.
What does StructuredChatOutputParser extend?
StructuredChatOutputParser extends AgentOutputParser.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free