From b043eb89aa9c267cc8e16623f7047fe9fa54d699 Mon Sep 17 00:00:00 2001 From: Brandon Hancock Date: Tue, 18 Feb 2025 16:02:01 -0500 Subject: [PATCH] drop trailing set --- src/crewai/agents/parser.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/crewai/agents/parser.py b/src/crewai/agents/parser.py index 7dd3ba8e7..71444a20a 100644 --- a/src/crewai/agents/parser.py +++ b/src/crewai/agents/parser.py @@ -94,8 +94,13 @@ class CrewAgentParser: elif includes_answer: final_answer = text.split(FINAL_ANSWER_ACTION)[-1].strip() - # Remove all backticks (```) from the final answer - final_answer = final_answer.replace("```", "").strip() + # Check whether the final answer ends with triple backticks. + if final_answer.endswith("```"): + # Count occurrences of triple backticks in the final answer. + count = final_answer.count("```") + # If count is odd then it's an unmatched trailing set; remove it. + if count % 2 != 0: + final_answer = final_answer[:-3].rstrip() return AgentFinish(thought, final_answer, text) if not re.search(r"Action\s*\d*\s*:[\s]*(.*?)", text, re.DOTALL):