AI engineering traps · Chapter 2 of 16

Trusting the model's JSON

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Calling the model
  • 3 tasks
  • Python
  • Free chapter

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.

The lesson

By the end of this case

You can spot model JSON that was trusted without a check, make it happen on purpose with the lab model, check every field with pydantic before the reply is trusted, and pin it down with a test that sends replies of the wrong shape.

The ticket

SUN-215: A complaint about water and the lift was never assigned. Assigned to you by Farah. Priority: high. Since: this morning.

Rohit, in the operations channel at 9:05 am: "Mr. Srivastava in B-304 had no water since 6 am, and then the lift got stuck on the 3rd floor with his mother in it. He put both in one complaint. The app said it was received, and then nothing: no team picked it up for two hours. The guard got her out in the end."

The triage worker's log, from this morning:

06:52:11.904  triage  triage: B-304, ['water', 'lift'], high
06:52:11.906  triage  TypeError: unhashable type: 'list'
06:52:11.906  tickets complaint 8841 created, team: (none)

Farah: "The model is asked for JSON and it sends JSON, every time. It parsed fine. Look at what came back for a complaint about two things, and at what the code believed it was. The worker logged the error and moved on, so the complaint just sat there. Reproduce it first, then fix it, then make sure it can never come back. You are in triage.py."

JSON is not the same as the right JSON

response_format={"type": "json_object"} asks the model for JSON, and you get JSON: json.loads will not raise. But nothing promises the shape. A model asked for "a category" may send two in a list, a word you never listed, a number as text, or leave a field out. Real models do all of these, especially on inputs nobody tried.

json.loads returns whatever the JSON holds, and cast() only tells the type checker what to believe: at run time it does nothing at all. Here the reply for a clubhouse booking has a list where the type says one word, and text where it says a number:

import json
from typing import Literal, TypedDict, cast


class Booking(TypedDict):
    slot: Literal["morning", "evening"]
    hours: int


reply = '{"slot":["morning","evening"],"hours":"2"}'
booking = cast(Booking, json.loads(reply))
print(f"Clubhouse booked: {booking['slot']}, {booking['hours'] * 2} hours with the cleaning")
print(booking["slot"].upper())

Python prints the first line, then stops at the second (trimmed):

Clubhouse booked: ['morning', 'evening'], 22 hours with the cleaning
AttributeError: 'list' object has no attribute 'upper'

The first line is worse than the crash: "2" * 2 is "22", so it is wrong and looks fine. The crash, when it comes, points at a line far from the real cause, the way the ticket's TypeError points at a dictionary lookup.

Checking before you trust

The model's reply is text from outside your program, like a request body. Check it at run time before you trust it. pydantic (in the lab) does that from the type itself: TypeAdapter(Booking) checks every field's type and allowed values, and raises a ValidationError that says what was wrong:

from pydantic import TypeAdapter, ValidationError

BOOKING = TypeAdapter(Booking)
print(BOOKING.validate_json('{"slot":"evening","hours":2}'))
try:
    BOOKING.validate_json('{"slot":["morning","evening"],"hours":"two"}')
except ValidationError as e:
    print(e)
{'slot': 'evening', 'hours': 2}
2 validation errors for Booking
slot
  Input should be 'morning' or 'evening' [type=literal_error, input_value=['morning', 'evening'], input_type=list]
hours
  Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='two', input_type=str]

(Trimmed: each error also has a link to pydantic's docs.) When a reply fails the check, the program needs a safe path: here, a person at the Front desk reads the complaint.

A test with replies you choose

The lab model answers the ticket's complaint the same way every time, so the repro is the real complaint. The guard is easier with a fake model (case 1): it can send any reply you like, including shapes the real model has not sent yet.

Your turn

Three tasks, one file each. In repro.py, crash route with the ticket's complaint on purpose, and run it with python3 repro.py. In triage.py, write parse_triage and use it, so only a reply of the right shape is trusted. In test_triage.py, write the guard: the check runs it beside the original code, the fix and a wrong fix that takes the first category from a list. Then answer Farah's interview question at the bottom of the case.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.py: crash route with the ticket's complaint, on purpose

    Write reproduce() in repro.py. Send the ticket's complaint to route from triage.py, word for word: Flat B-304 here. No water since 6 am and now the lift is stuck on the 3rd floor too. Return what route gives back; if route raises instead, catch the error and return its type's name, a colon and a space, then the error as a str.

    The check runs your reproduce() twice: beside the original triage.py it must return "TypeError: unhashable type: 'list'" (the crash in the ticket), and beside a fixed triage.py 'Assigned to Front desk'. So it must really route the ticket's complaint. route uses the lab model when you leave out its client; reproduce() must finish within 10 seconds.

  2. Task 2

    triage.py: check the model's JSON before you trust it

    Write parse_triage(text) in triage.py. It gives back a Triage only when text is JSON for an object (not a list, not null) whose flat is a str or None, whose category is one of the seven categories as a single str, and whose urgency is 'low', 'medium' or 'high'; for anything else, text that is not JSON included, it gives back None and never raises. pydantic's TypeAdapter(Triage).validate_json does these checks, and raises ValidationError when one fails.

    Then make triage use it, so it returns the Triage or None. route sends a None to the Front desk ('Assigned to Front desk') and prints triage: could not read the model's reply; for a good reply it keeps its printed line and team. Keep Triage, Category and TEAMS as they are. The check sends its own replies, broken ones too, through parse_triage and through route. Taking the first category from a list is not a fix.

  3. Task 3

    test_triage.py: a test that would have caught it

    Write a pytest file, test_triage.py, that would have caught this bug. The check runs your file three times: beside the original triage.py, where at least one test must fail; beside the fixed triage.py, where every test must pass; and beside a wrong fix that takes the first category from a list and checks nothing else, where at least one test must fail.

    So test route with fake models: a good reply goes to its team, and replies of the wrong shape (a list of categories, a category the app does not know, no urgency) go to the Front desk. Use only route, which every version has. Each test must finish within 10 seconds.

What you will learn

  • repro.py: crash route with the ticket's complaint, on purpose
  • triage.py: check the model's JSON before you trust it
  • test_triage.py: a test that would have caught it

More in Calling the model

Build "Trusting the model's JSON" yourself

Sunwai, a Lucknow app for housing societies, runs on a model, and Farah, its CTO, keeps assigning you tickets. Each is a trap AI engineering interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.