#26Parse the SEARCH / ANSWER protocolEasy

Parse the SEARCH / ANSWER protocol

Background

In ReAct, the LLM speaks a two-token protocol. Each turn it outputs exactly one of:

  • SEARCH: <query> → the agent runs a search and loops.
  • ANSWER: <text> → the agent returns the text and stops.

Anything else is unexpected freeform output.

Problem statement

Implement parse_action(response) returning (kind, payload).

Input

  • response — the raw LLM output string.

Output

Returns a tuple:

  • ("search", query) if it starts with "SEARCH:" (query stripped),
  • ("answer", text) if it starts with "ANSWER:" (text stripped),
  • ("other", response) otherwise (response unchanged).

Examples

Example 1

Input:  "SEARCH: AlphaCode benchmark"
Output: ("search", "AlphaCode benchmark")

Example 2

Input:  "ANSWER: It used CodeContests."
Output: ("answer", "It used CodeContests.")

Example 3

Input:  "I'm not sure what to do"
Output: ("other", "I'm not sure what to do")

Constraints

  • Strip whitespace from the payload of SEARCH: / ANSWER:.
  • For "other", return the original response unchanged.

Notes

  • A precise parser is what keeps the agent's loop deterministic — one stray prefix and the control flow breaks.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Reference example: SEARCH parsed
  • Sample: ANSWER parsed
  • Reference: payload whitespace is stripped