Skip to main content

extract_json_from_response

Function extract_json_from_response 

Source
pub fn extract_json_from_response(response: &str) -> &str
Expand description

Extracts JSON from LLM response, handling markdown code blocks (CQ-H2).

This is a centralized utility for extracting JSON from LLM responses that may include markdown formatting, prose, or other surrounding text.

§Handling

  1. Markdown code blocks with json language marker
  2. Markdown code blocks without language marker
  3. Raw JSON objects (first { to last })
  4. JSON arrays (first [ to last ])

§Arguments

  • response - The raw LLM response text

§Returns

The extracted JSON string, or the trimmed input if no JSON found