Skip to content

Commit 2281f00

Browse files
authored
langchain: Standardize output_parser.py across all agent types for custom FORMAT_INSTRUCTIONS (#17168)
- **Description:** This PR standardizes the `output_parser.py` file across all agent types to ensure a uniform parsing mechanism is implemented. It introduces a cohesive structure and common interface for output parsing, facilitating easier modifications and extensions by users. The standardized approach enhances maintainability and scalability of the codebase by providing a consistent pattern for output parsing, which can be easily understood and utilized across different agent types. This PR builds upon the foundation set by a previously merged PR, which focused exclusively on standardizing the `output_parser.py` for the `conversational_agent` ([PR #16945](#16945)). With this new update, I extend the standardization efforts to encompass `output_parser.py` files across all agent types. This enhancement not only unifies the parsing mechanism across the board but also introduces the flexibility for users to incorporate custom `FORMAT_INSTRUCTIONS`. - **Issue:** #10721 #4044 - **Dependencies:** No new dependencies required for this change - **Twitter handle:** With my github user is enough. Thanks I hope you accept my PR.
1 parent 1cf5a58 commit 2281f00

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

libs/langchain/langchain/agents/chat/output_parser.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@
1414
class ChatOutputParser(AgentOutputParser):
1515
"""Output parser for the chat agent."""
1616

17+
format_instructions: str = FORMAT_INSTRUCTIONS
18+
"""Default formatting instructions"""
19+
1720
pattern = re.compile(r"^.*?`{3}(?:json)?\n(.*?)`{3}.*?$", re.DOTALL)
1821
"""Regex pattern to parse the output."""
1922

2023
def get_format_instructions(self) -> str:
21-
return FORMAT_INSTRUCTIONS
24+
"""Returns formatting instructions for the given output parser."""
25+
return self.format_instructions
2226

2327
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
2428
includes_answer = FINAL_ANSWER_ACTION in text

libs/langchain/langchain/agents/conversational/output_parser.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ class ConvoOutputParser(AgentOutputParser):
1414
ai_prefix: str = "AI"
1515
"""Prefix to use before AI output."""
1616

17+
format_instructions: str = FORMAT_INSTRUCTIONS
18+
"""Default formatting instructions"""
19+
1720
def get_format_instructions(self) -> str:
18-
return FORMAT_INSTRUCTIONS
21+
"""Returns formatting instructions for the given output parser."""
22+
return self.format_instructions
1923

2024
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
2125
if f"{self.ai_prefix}:" in text:

libs/langchain/langchain/agents/conversational_chat/output_parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class ConvoOutputParser(AgentOutputParser):
1515
"""Output parser for the conversational agent."""
1616

1717
format_instructions: str = FORMAT_INSTRUCTIONS
18+
"""Default formatting instructions"""
1819

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

libs/langchain/langchain/agents/mrkl/output_parser.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@
2222
class MRKLOutputParser(AgentOutputParser):
2323
"""MRKL Output parser for the chat agent."""
2424

25+
format_instructions: str = FORMAT_INSTRUCTIONS
26+
"""Default formatting instructions"""
27+
2528
def get_format_instructions(self) -> str:
26-
return FORMAT_INSTRUCTIONS
29+
"""Returns formatting instructions for the given output parser."""
30+
return self.format_instructions
2731

2832
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
2933
includes_answer = FINAL_ANSWER_ACTION in text

libs/langchain/langchain/agents/structured_chat/output_parser.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@
2020
class StructuredChatOutputParser(AgentOutputParser):
2121
"""Output parser for the structured chat agent."""
2222

23+
format_instructions: str = FORMAT_INSTRUCTIONS
24+
"""Default formatting instructions"""
25+
2326
pattern = re.compile(r"```(?:json\s+)?(\W.*?)```", re.DOTALL)
27+
"""Regex pattern to parse the output."""
2428

2529
def get_format_instructions(self) -> str:
26-
return FORMAT_INSTRUCTIONS
30+
"""Returns formatting instructions for the given output parser."""
31+
return self.format_instructions
2732

2833
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
2934
try:

0 commit comments

Comments
 (0)