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.
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 askHint 1
What exactly should fail if the model returns an array instead of an object?