Every dream reading in my app comes back from an LLM as JSON with three keys. The app shows two of them as text and uses the third to pick a hieroglyph. The first version asked for that third key as "a single, concrete English noun", and the app could only draw a fraction of the nouns a model can think of: many dreams fell back to the same default sign. The JSON was always valid. It just wasn't the JSON I needed.
This post is the whole path from prompt to screen in Resut: what the prompt asks for, what JSON mode does and doesn't promise, and the checks on the server (and again on the phone) that make the answer safe to render.
The contract, written in the prompt
The system prompt is the contract. Here it is, trimmed only at the end where the list of allowed words is inserted:
const THOTH_SYSTEM_PROMPT = `You are Thoth, the ancient Egyptian god of wisdom and patron of scribes. Your tone is solemn, mystical, and deeply poetic. The user will share a dream. Output your response strictly in JSON format with exactly three keys: 1. 'the_omen': A short, prophetic interpretation of the dream, written as if translated from an ancient papyrus. 2. 'the_scribe': A modern, grounded psychological interpretation of the same dream, offering practical life advice. 3. 'hieroglyph_candidates': An array of up to ${MAX_CANDIDATES} keywords for the hieroglyph of this dream, best first. Each must be copied exactly from the list below, each must come from a different line, and each must be a fitting symbol for the dream: [...] The allowed hieroglyph keywords, one line per sign ("Sign name: keywords"): ${vocabularyLines()}`; Three things I'd point out:
- Exact key names, numbered, with a type each. "A short interpretation", "an array of up to 3 keywords". The model follows a list better than a paragraph.
- The word "JSON" is in the prompt. OpenAI's JSON mode expects the messages themselves to ask for JSON; the prompt has to say it, not just the request flag.
- The hard field is a closed choice. An open question ("any noun") has infinite answers. Now the model picks from a list that's pasted into the prompt, one line per sign. The list is generated from the app's own data, which is a story for the next post.
JSON mode: syntax, not schema
The request turns on JSON mode:
body: JSON.stringify({ model, response_format: { type: 'json_object' }, // Newer models (GPT-5 and later) reject any temperature but the default. ...(model.startsWith('gpt-4') ? { temperature: 0.9 } : {}), messages: [ { role: 'system', content: THOTH_SYSTEM_PROMPT }, { role: 'user', content: dreamMessage(dream, question) }, ], }), json_object means the content will parse. It says nothing about which keys are there, what types they have, or whether a string is empty. So the server treats the parsed object as untrusted input, exactly like a request body from a stranger.
The temperature line is there because of a real failure. The readings use temperature: 0.9 for some variety in the wording, and that's fine for gpt-4o-mini, the default. Newer models (GPT-5 and later) reject any temperature but the default, so pointing the OPENAI_MODEL secret at one of them would have broken every reading. Now the parameter is only sent to gpt-4 models, so switching models is a config change, not a deploy.
Parse, then check every field
let raw: Record<string, unknown>; try { const data = await res.json(); raw = JSON.parse(data?.choices?.[0]?.message?.content ?? ''); } catch { throw new HttpError(502, 'oracle_unreadable', 'The papyrus could not be read. Ask the Oracle once more.'); } const omen = typeof raw.the_omen === 'string' ? raw.the_omen.trim() : ''; const scribe = typeof raw.the_scribe === 'string' ? raw.the_scribe.trim() : ''; if (!omen || !scribe) { throw new HttpError(502, 'oracle_unreadable', 'The papyrus came back torn. Ask the Oracle once more.'); } const { hieroglyph_keyword, hieroglyph_candidates } = dreamGlyph(raw); return { the_omen: omen, the_scribe: scribe, hieroglyph_keyword, hieroglyph_candidates }; The optional chaining on data?.choices?.[0]?.message?.content isn't paranoia: if the response is cut off or shaped differently, JSON.parse('') throws and we land in the same clean error instead of a TypeError in the logs.
The two text fields are required. If either is missing, not a string, or blank after trimming, the reading fails. I'd rather ask the user to try again than show half a reading.
The error messages are written for the user, in the app's voice ("The papyrus came back torn"). Every server error is { error, message }, and the app shows the message as-is.
The closed field: drop what's not allowed, never fail on it
The hieroglyph is different. A bad keyword shouldn't cost the user their reading, so instead of failing, the server filters:
export function readCandidates(list) { const out = []; const signs = new Set(); for (const word of Array.isArray(list) ? list : [list]) { const keyword = toKeyword(word); if (!keyword || signs.has(KEYWORD_SIGNS[keyword])) continue; signs.add(KEYWORD_SIGNS[keyword]); out.push(keyword); if (out.length === MAX_CANDIDATES) break; } return out; } export function dreamGlyph(raw) { const given = [ ...(Array.isArray(raw?.hieroglyph_candidates) ? raw.hieroglyph_candidates : []), raw?.hieroglyph_keyword, ]; const candidates = readCandidates(given.filter((word) => typeof word === 'string')); return { hieroglyph_candidates: candidates, hieroglyph_keyword: candidates[0] ?? FALLBACK_KEYWORD }; } toKeyword lowercases, keeps the first word, strips punctuation, tries the singular ("cats" → "cat") and maps a few modern things to the nearest ancient sign ("car" → road, "phone" → mouth), because models still sometimes answer with the thing in the dream instead of the word from the list. Anything left unknown is dropped. Two words that draw the same sign ("river" and "sea") count once. If nothing survives, the reading gets the Eye of Horus ('eye'), which is a perfectly good sign for a dream.
Notice that dreamGlyph also reads hieroglyph_keyword, the field name from the old prompt, and still returns hieroglyph_keyword. Apps already installed only know that field. Changing a contract on the server while old clients are in the wild means answering in both shapes for a while.
Optional input, without inventing it
Resut has an "Incubation" feature: before sleeping you can ask the night a question, and the next reading answers it. The question is optional, so the prompt has to handle both cases without the model making one up:
const dreamMessage = (dream: string, question: string) => question ? `The question asked before sleeping: "${question}"\n\nThe dream:\n${dream}` : dream; The system prompt adds: "When the message carries one, both 'the_omen' and 'the_scribe' must answer that question through this dream... Never invent a question when there is none." Before it gets there, the question is collapsed to single spaces and cut to 500 characters, and it's never stored.
Check again on the phone
The app doesn't trust the server blindly either. In src/services/oracle.js:
const scribe = text(result.the_scribe); if (typeof result.readingId !== 'string' || !scribe) throw new OracleError(MSG_UNREADABLE, { code: 'bad_response' }); const keyword = text(result.hieroglyph_keyword).toLowerCase().split(/\s+/)[0]; const offered = Array.isArray(result.hieroglyph_candidates) ? result.hieroglyph_candidates : []; return { readingId: result.readingId, the_scribe: scribe, hieroglyph_keyword: keyword || 'eye', hieroglyph_candidates: readCandidates([...offered, keyword]), the_omen: text(result.the_omen) || null, ...readAllowance(result), // isPharaoh, free readings left, etc. }; Same readCandidates rule, the app's own copy. It costs nothing, and it means an older server, a proxy error page or a future bug can't put something on screen that the app can't draw. the_omen becomes null when it's missing, which is also exactly what a free user gets (the omen is sealed), so the UI only has one "no omen" case to handle.
When it fails after you've counted it
One more piece: the server counts a reading before calling the model, so parallel requests can't overshoot the free allowance. If the model then fails or returns junk, the counted row is deleted again. A bad answer from the LLM never uses up one of the user's two free readings.
Takeaways
- JSON mode (or any "structured output" flag) gives you parseable text. Validate the object like any untrusted input.
- Required fields fail loudly, with a message the user can read. Decorative fields degrade quietly to a safe default.
- Replace open questions with closed lists when the answer drives code. The model will mostly comply, and the filter handles the rest.
- Keep answering in the old shape while old clients exist.
- Don't charge the user for the model's mistakes.
Resut is the dream app I'm building in public. Previous post in the series: "Never ship your OpenAI key: moving an LLM call into a Supabase Edge Function".


