Skip to content

< all problems01 · Level 01, LLM APIs

Get the JSON Out of a Model's Reply

easy · implement · LLM Fundamentals

You asked the model for JSON. It sent you this:

```json
{"name": "Sarah", "issue": "can't log in"}
```

json.loads raises on that. The JSON is in there — it's just wearing a markdown fence, because the model was trained on a lot of documentation and it thinks it's being helpful.

Sometimes it adds a sentence first. Sometimes it does neither and gives you clean JSON. You don't get to choose.

Write parse_model_json(reply):

  • Return the JSON object from the reply, as a dict.
  • Return None if there isn't one.

That's it. A few lines.

This is the first thing that breaks in every LLM application, and it breaks in production rather than in testing, because the model behaved during testing.