Search RU
Tasks / AI / LLM / Parsing a model's JSON response
Statement medium

Parsing a model's JSON response

Implement `parse_model_json(text, required_fields)`.

The model sometimes answers with pure JSON and sometimes wraps it in a Markdown block
```json ... ```. You need to get a dict or raise `ModelOutputError`.

Rules:

- empty text — `ModelOutputError`;
- exactly one outer wrapper ```json ... ``` or ``` ... ``` is stripped;
- the content must be a JSON object, not an array or a number;
- every field from `required_fields` must be present;
- extra fields are allowed;
- invalid JSON — `ModelOutputError`, with no `JSONDecodeError` leaking out.

aijsonstructured-output
solution.py Python 3.14
AI mentor

The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.

Log in and ask
Hints 1 of 3 unlocked · attempts: 0
Hint 1

What exactly should fail if the model returns an array instead of an object?

Hint 2 — unlocks after 1 attempt
Hint 3 — unlocks after 3 attempts