# Reliable JSON from Gemini Nano — demo

Companion code for the blog post [_Prompt API: reliable JSON from Gemini Nano, no hand-parsing_](https://www.khriztianmoreno.dev/blog/prompt-api-structured-output-json-schema) (also in [Spanish](https://www.khriztianmoreno.dev/es/blog/prompt-api-structured-output-json-schema)), the second post in a series on Chrome's built-in AI task APIs.

Paste a WhatsApp-style message ("hola, me mandas 2 empanadas de pollo y una Coca-Cola sin azúcar…") and get back a validated order: items with quantity, name, and notes, plus fulfillment, address, and payment method. Gemini Nano runs on-device through the [Prompt API](https://developer.chrome.com/docs/ai/prompt-api), and its output is constrained by a JSON Schema passed as [`responseConstraint`](https://developer.chrome.com/docs/ai/structured-output-for-prompt-api). No server, no API key, and no text leaves the device.

There's no build step, like the other demos. Tailwind loads from its browser CDN; everything else is plain ES modules.

## Files

- [`order-parser.js`](./order-parser.js) is the reusable part, and what the post walks through:
  - `ORDER_SCHEMA` is the output contract, a plain JSON Schema object.
  - `getAvailability()` wraps `LanguageModel.availability()` (with the same `expectedInputs`/`expectedOutputs` used to create the session) and adds `'unsupported'` for browsers without the API.
  - `getBaseSession({ onProgress })` creates one session with the system prompt, reporting download progress through `monitor`. Each parse runs on a `clone()` of it.
  - `validateOrder(order, message)` checks the shape (cheap insurance) and then the meaning: the address and item names must actually appear in the message, a pickup can't carry a delivery address, no duplicated lines, and a delivery needs an address. Each problem is tagged `retry` (the model got it wrong) or `ask` (the information isn't in the message).
  - `parseOrder(base, message)` prompts with `responseConstraint`, runs `validateOrder()`, and retries once on the same session with feedback, but only for `retry` problems. `ask` problems go straight to a human: retrying them just lets the model hide the error.
  - `parseOrderUnconstrained(base, message)` runs the same prompt without a schema, for comparison.
- [`main.js`](./main.js) is the UI: preset messages, availability badge, download progress, the raw output per attempt, the receipt, and the optional side-by-side run.
- [`index.html`](./index.html) and [`style.css`](./style.css) are the page.

## Running it

ES modules need HTTP, not `file://`:

```bash
npx serve .
# or
python3 -m http.server
```

Open it in desktop Chrome 148 or later (Windows 10/11, macOS 13+, Linux, or a Chromebook Plus), on hardware that meets the [requirements](https://developer.chrome.com/docs/ai/get-started) for Gemini Nano. It doesn't work on mobile. If the model isn't on the device yet, the button reads **Download model & parse**: `create()` needs a user click to start that download.

In browsers without the API, the page says so and the button stays disabled.

## What to look for

- **ES · clean** and **EN · pickup** should come back valid on the first attempt.
- **ES · messy** has a repeated item with a different modifier, an address with an apartment and tower, and a payment app name. Check how the items and notes are split. Sometimes the model marks it as pickup; the validator sends it back once.
- **EN · no address** asks for a delivery without an address. The schema can't catch that; the validator can, and it ends at **needs a human** without retrying.
- Tick **Also run without `responseConstraint`** to see the same prompt with no schema. Sometimes it's fine; sometimes it's wrapped in Markdown fences or uses different field names.
