Skip to content

Commit a52cdbc

Browse files
Update output_parser.ts (#3975)
Co-authored-by: Ben Burns <[email protected]>
1 parent 8ab3773 commit a52cdbc

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

langchain/src/agents/xml/output_parser.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,18 @@ export class XMLAgentOutputParser extends AgentActionOutputParser {
3838
*/
3939
async parse(text: string): Promise<AgentAction | AgentFinish> {
4040
if (text.includes("</tool>")) {
41-
const [tool, toolInput] = text.split("</tool>");
42-
const _tool = tool.split("<tool>")[1];
43-
const _toolInput = toolInput.split("<tool_input>")[1];
41+
const _toolMatch = text.match(/<tool>([^<]*)<\/tool>/);
42+
const _tool = _toolMatch ? _toolMatch[1] : "";
43+
const _toolInputMatch = text.match(
44+
/<tool_input>([^<]*?)(?:<\/tool_input>|$)/
45+
);
46+
const _toolInput = _toolInputMatch ? _toolInputMatch[1] : "";
4447
return { tool: _tool, toolInput: _toolInput, log: text };
4548
} else if (text.includes("<final_answer>")) {
46-
const [, answer] = text.split("<final_answer>");
49+
const answerMatch = text.match(
50+
/<final_answer>([^<]*?)(?:<\/final_answer>|$)/
51+
);
52+
const answer = answerMatch ? answerMatch[1] : "";
4753
return { returnValues: { output: answer }, log: text };
4854
} else {
4955
throw new OutputParserException(`Could not parse LLM output: ${text}`);

0 commit comments

Comments
 (0)