BasicsReActParsingOpenAIAnthropicGoogle
Problem
Implement parse_react_trace(text) that parses a multi-line ReAct trace containing Thought:, Action:, and Observation: prefixed lines into a dictionary {"thought": ..., "action": ..., "observation": ...} with stripped values (defaulting missing fields to "").
Why It Matters: Parsing structured Thought / Action / Observation traces is essential for ReAct agent loops, trajectory logging, and automated evaluation harnesses.
Function Signature
def parse_react_trace(text: str) -> dict[str, str]:
"""Extract thought, action, and observation strings from a ReAct trace."""
Sample Input & Output
Example 1: Complete 3-Line ReAct Step
Input:
text = "Thought: Need weather\nAction: get_weather(NYC)\nObservation: 72F"
Expected Output:
{"thought": "Need weather", "action": "get_weather(NYC)", "observation": "72F"}
Explanation: Each labeled line is matched, its prefix removed, and surrounding whitespace stripped.
Example 2: Missing Observation (Pre-Tool Execution)
Input:
text = " Thought: Ready to answer \n Action: finish(42) "
Expected Output:
{"thought": "Ready to answer", "action": "finish(42)", "observation": ""}
Explanation: Leading/trailing spaces are stripped and omitted fields default cleanly to "".
Constraints & Edge Cases
- Return a dict with keys
'thought','action','observation'(default''if missing). - Always return all three keys:
'thought','action', and'observation'. - Strip whitespace both from lines and from extracted values.
Constraints & Evaluation Criteria
- Return a dict with keys `'thought'`, `'action'`, `'observation'` (default `''` if missing).
Hints
Learn the Concept First
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
Thought/Action/Observation lines
Expected Output / Criteria
dict with all 3 keys