Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix output parsing for chat agent #5355

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion langchain/agents/chat/output_parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from typing import Union

from langchain.agents.agent import AgentOutputParser
Expand All @@ -18,7 +19,15 @@ def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
{"output": text.split(FINAL_ANSWER_ACTION)[-1].strip()}, text
)
try:
response = parse_json_markdown(text)
text = text.strip().replace("```json", "```")
markdown_section = re.search("```(.*?)```", text, re.DOTALL)
if markdown_section:
extracted_text = markdown_section.group(
1
).strip() # remove leading/trailing whitespaces
else:
extracted_text = ""
response = parse_json_markdown(extracted_text)
return AgentAction(response["action"], response["action_input"], text)

except Exception:
Expand Down