The Schema Sheriff

Difficulty: MEDIUMID: ai-schema-validation

The Scenario

Your app expects the LLM to return user profiles:

{"name": "Alice", "age": 30, "email": "alice@example.com"}

The LLM returns:

{"name": "Alice", "age": "thirty", "email": null}

Your code crashes because age is a string, not an int.

The Problem

You are trusting the LLM to return correct types.

user = json.loads(response)
db.save(age=user['age'] + 1)  # 💥 TypeError: can only concatenate str (not "int") to str

The Goal

Implement a Schema Validator:

  1. Check if required keys exist.
  2. Validate types (e.g., age must be int).
  3. Coerce types if possible (convert "30" to 30).
  4. Raise error if validation fails.

Requirements:

  • Required keys: name (str), age (int), email (str).
  • Convert string numbers to int for age.
  • Raise ValueError for missing keys or wrong types.
solution.py
Loading...
⚠️ Do not include PII or secrets in your code.
SYSTEM_LOGS
5/5
// Waiting for execution trigger...
PREVIEW MODE — SOLVE PREVIOUS MISSIONS TO UNLOCK