Skip to main content

parse_choice

Function parse_choice 

Source
pub fn parse_choice<'a>(
    response: &str,
    valid_choices: &[&'a str],
) -> Result<&'a str, ParseError>
Expand description

Extract a single choice from a set of valid options.

Handles common LLM response patterns:

  • Direct match: "positive"
  • Bold: "**positive**"
  • Quoted: "'positive'" or "\"positive\""
  • In prose: "I would classify this as positive because..."
  • Parenthesized: "(positive)"

Matching is case-insensitive. If multiple valid choices appear, returns the first one found in the text.

ยงExamples

use llm_output_parser::parse_choice;

let result = parse_choice("I'd classify this as positive", &["positive", "negative"]).unwrap();
assert_eq!(result, "positive");