Parsing Actions from Model Output
What You'll Learn
You'll learn how to reliably extract structured action commands from the free-form text that language models generate, enabling your AI system to actually do things.
The Core Challenge
When a model uses ReAct prompting (Reasoning + Acting), it generates thoughts and proposes actions in natural language. But your code can't execute phrases like "I should search for..." or "Let me calculate..." — you need to extract the actual action name and parameters in a format your system understands.
Think of it like parsing voice commands: when someone says "Alexa, play jazz music," the system must extract action=play and parameter=jazz music from that sentence.
How Parsing Works
You typically guide the model to use specific markers or formats in its output:
Pattern 1: Delimited Actions
Action: search
Action Input: "weather in Paris"
Pattern 2: JSON-like Structure
{"action": "calculator", "input": "15 * 42"}
Your parsing code then:
- Searches for the action delimiter or pattern
- Extracts the action name
- Extracts the parameters/input
- Validates the action exists in your allowed action space
- Returns a structured object your code can execute
Common Pitfalls
- Models sometimes add extra text before/after the action
- Formatting may be inconsistent (extra spaces, capitalization)
- Action parameters might be malformed
- The model might propose invalid actions not in your action space
Your parser must be robust — use regex, string matching, or even a second LLM call to extract structured data from messy output.
Key Takeaway: Parsing transforms unstructured model responses into executable commands by extracting action names and parameters using delimiters, patterns, and validation against your defined action space.