THINK FIRST·CODE LATER

← All labs

Guardrail: validate an AI-generated specification

Problem

Input: schema field:T|N|O|Q …, then a Vega-Lite specification produced by an AI (possibly invalid JSON).

Checks: valid JSON (else print REJECT: not valid JSON (<message> at line <n>)); the mark (string or object type) is one of bar, line, point, circle, square, area, rect, rule, text, tick, arc, boxplot, geoshape; for each encoding channel (alphabetical): the aggregate (if any) is one of count, sum, mean, average, median, min, max, distinct, stdev, variance; a channel without field is allowed only for count; the field exists in the schema (or is created by a transform's as); the encoding type is compatible (Q → quantitative; N or O → nominal/ordinal; T → temporal/ordinal; count fields may be quantitative); sum/mean/average/median/stdev/variance only on Q fields.

Print ACCEPT: line chart with color, x, y or REJECT (3 problem(s)): followed by lines - x: field 'District' does not exist (columns: date, district, pm25, station_id), - y: unknown aggregate 'avg', - x: 'date' is T in the data but encoded as nominal, - y: mean of non-quantitative field 'station_id', - unknown mark 'pie', - y: no field.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

v12-2-data-1.txt

schema date:T district:N pm25:Q station_id:N
{"mark": "bar", "encoding": {"x": {"field": "District", "type": "nominal"}, "y": {"field": "pm25", "aggregate": "avg", "type": "quantitative"}}}
What your program must print for v12-2-data-1.txt
REJECT (2 problem(s)):
  - x: field 'District' does not exist (columns: date, district, pm25, station_id)
  - y: unknown aggregate 'avg'

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), in the folder that holds both files:

python my_lab.py < v12-2-data-1.txt

Windows PowerShell (the default terminal of VS Code on Windows) does not understand <:

Get-Content v12-2-data-1.txt | python my_lab.py

PyCharm: Run → Edit Configurations → tick Redirect input from and choose the file.

Jupyter: put this at the top of the cell that holds your program:

import sys
sys.stdin = open("v12-2-data-1.txt", encoding="utf-8")

Write it here, in Jupyter or in your IDE, then paste it. Run it on the sample input yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.