Parsing LLM Responses in DataWeave: 3 Layers of Defense Against Markdown Fences
I connected MuleSoft to GPT-4o last quarter for a support ticket classifier. The prompt builder worked (I covered that in a previous post). The LLM call worked. Then the response came back and my p...

Source: DEV Community
I connected MuleSoft to GPT-4o last quarter for a support ticket classifier. The prompt builder worked (I covered that in a previous post). The LLM call worked. Then the response came back and my parser crashed. The LLM was supposed to return clean JSON. Instead it returned: "Here is my analysis:\n json\n{\"ranking\": [\"TK-101\"]}\n \nLet me know if you need anything else." Markdown fences. Preamble text. Trailing conversation. My read() call choked on "Here is my analysis" — not valid JSON. TL;DR LLMs wrap JSON responses in markdown fences — your parser must extract the JSON first try() from dw::Runtime catches parse failures gracefully — without it, one malformed response crashes your flow Always validate required keys after parsing — LLMs hallucinate extra fields and omit required ones The regex for fence extraction: raw match /(?s)(?:json)?\s*(\{.*?\})\s*/ Test with 5 response variants: clean JSON, fenced JSON, fenced without language tag, no fences, broken JSON The Problem: LLMs