Log inBook a Demo
BuildMidData / MLAuto-graded

Extract valid JSON from messy LLM replies

Recover the JSON object from model replies with prose, code fences or syntax slips, and fail cleanly when there's none.extract_json(text) in parser.py should turn a model's reply into a Python dict. Today it only copes with clean JSON. Real replies arrive with prose around the object, inside a code fence, with trailing commas or single quotes, and sometimes with all of these at once. Make it recover the intended object in each case, and raise ParseError when the reply holds no JSON object at all.
30 min
python

You're done when

  • JSON with prose before or after it is found and parsed.
  • JSON inside a code fence, with or without a json tag, is parsed.
  • Trailing commas and single-quoted keys and strings are accepted.
  • A reply that holds no JSON object raises ParseError instead of returning a value.

What it measures

LLM output handlingTurning what a model actually returns into data the rest of the code can use.
Tolerant parsingHandling each kind of messy reply, including several mixed in one response.
Failing cleanlyRaising `ParseError` when a reply has no JSON, so an empty dict never reaches the pipeline.

Your environment

Terminal + editorA full shell in the browser
Claude CodePre-authenticated, no key setup
Isolated microVMYours alone, wiped afterwards

Start this lab

30 min
Start labYou'll sign in to Anthropos to start. The lab opens in a private cloud workspace with Claude Code ready to use.
AI Labs is part of Anthropos Workforce. Not on your plan yet? Book a demo