Parsing ReAct Agent Outputs
What you'll learn: How to reliably extract structured thoughts, actions, and action inputs from the free-form text your ReAct agent generates.
The Challenge of Unstructured Responses
When your ReAct agent responds, it produces text that follows the ReAct format—mixing thoughts, actions, and inputs—but it's still just a string. Your code needs to parse this output to understand what the agent is thinking and what it wants to do next. Think of it like reading a recipe: the ingredients and steps are there, but you need to identify which is which before you can cook.
What to Extract
ReAct outputs typically contain three components:
- Thought: The agent's reasoning ("I need to find the weather")
- Action: The tool name to call (
get_weather) - Action Input: The parameters for that tool (
{"city": "Boston"})
Your parser must identify these markers (often prefixes like Thought:, Action:, Action Input:) and extract the content following each.
Basic Parsing Logic
A simple approach uses string splitting or regular expressions:
- Split the response by lines
- Look for lines starting with known markers
- Extract the content after each marker
- Validate that required fields are present
Robust parsing handles variations: extra whitespace, missing fields, malformed JSON in action inputs, or the agent writing in unexpected ways. You may need fallback logic or error prompts when parsing fails.
Key Takeaway: Parsing ReAct outputs transforms unstructured LLM text into structured data your agent loop can execute—think of it as the translator between your agent's "thoughts" and your code's actions.