parse_llm_response_with_ir

Function parse_llm_response_with_ir 

Source
pub fn parse_llm_response_with_ir(
    ir: &IR,
    raw_response: &str,
    target_type: &FieldType,
) -> Result<BamlValue>
Expand description

Parse an LLM response using IR type definitions

This function takes a raw LLM response string and parses it into a typed BamlValue based on the IR (Intermediate Representation). It handles:

  • Extracting JSON from markdown code blocks
  • Lenient JSON parsing
  • Type coercion (e.g., string “30” → int 30)
  • Enum validation with case-insensitive matching
  • Nested structure validation

§Arguments

  • ir - The Intermediate Representation containing type definitions
  • raw_response - The raw string response from the LLM
  • target_type - The expected output type to parse into

§Returns

The parsed and type-coerced BamlValue

§Example

use simplify_baml::*;

let ir = IR::new();
let raw_response = r#"```json
{"name": "John", "age": "30"}
```"#;

let result = parse_llm_response_with_ir(
    &ir,
    raw_response,
    &FieldType::Class("Person".to_string())
).unwrap();