Skip to content
Draft
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ def parse(self, output: str, is_streaming: bool = False) -> BaseReasoningStep:
```
"""
# Use regex to find properly formatted keywords at line boundaries
thought_match = re.search(r"Thought:", output, re.MULTILINE)
action_match = re.search(r"Action:", output, re.MULTILINE)
answer_match = re.search(r"Answer:", output, re.MULTILINE)
thought_match = re.search(r"^Thought:", output, re.MULTILINE)
action_match = re.search(r"^Action:", output, re.MULTILINE)
answer_match = re.search(r"^Answer:", output, re.MULTILINE)

thought_idx = thought_match.start() if thought_match else None
action_idx = action_match.start() if action_match else None
Expand Down
17 changes: 17 additions & 0 deletions llama-index-core/tests/agent/react/test_react_output_parser.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from llama_index.core.agent.react.output_parser import (
ReActOutputParser,
extract_final_response,
extract_tool_use,
parse_action_reasoning_step,
)
from llama_index.core.agent.react.types import ResponseReasoningStep


def test_parse_action_reasoning_step() -> None:
Expand Down Expand Up @@ -206,3 +208,18 @@ def test_react_output_parser_handles_action_in_answer() -> None:
answer
== "Answer contains Legislative Action: here is the legislative action content."
)


def test_react_output_parser_ignores_action_keyword_inside_thought() -> None:
mock_input_text = """\
Thought: The phrase Action: is part of this thought, not a tool call.
Answer: final answer
"""

reasoning_step = ReActOutputParser().parse(mock_input_text)

assert isinstance(reasoning_step, ResponseReasoningStep)
assert reasoning_step.thought == (
"The phrase Action: is part of this thought, not a tool call."
)
assert reasoning_step.response == "final answer"