Launch offer: the first 1,000 users get Settl free for a year* Claim your spot
settlbuilding in public

The null-proof API kit: Zod patterns that survive LLM inputs

The short version

Items appeared in the app and then silently disappeared about a second later, with no error shown to anyone. The same root cause shipped three separate times in three different features.

The distinction that caused it: a field that is missing and a field that is deliberately empty are not the same thing, and most default-filling logic only handles the first. When a bug is about a concept rather than a feature, finding it once is not finishing. Go and look for it everywhere else.

Copy these prompts

Paste them into Claude Code (or any AI that can see your project), in this order. Each one answers a different question, and the first is usually the one that matters.

1. Find where your app fills in missing values

Search my codebase for every place I apply a default value to data coming in from a user, a form or an AI. List them with the field and the default.

2. Check the difference between missing and empty

For each of those defaults, tell me whether it still applies when the field is present but deliberately empty, rather than simply absent. Those are different cases and most default logic only handles one.

3. Make silent save failures visible

Show me every place my app displays something on screen before it has confirmed the save. For each, tell me what the user sees if the save then fails, and make that failure visible instead of silent.

My users' money entries vanished exactly one second after they appeared, and I shipped that same bug three times (on the split-breakdown field, then the transcript, then the note). The chain: the LLM emitted an explicit null, Zod's .default() only fills undefined (not null), the API 400'd, the optimistic UI had already painted the row, the sync queue dropped the rejected write, and the next reconcile erased it. No error shown to anyone.

The core fix (the exact pattern)

// BROKEN: fills undefined, but an LLM's explicit null sails through and 400s
note: z.string().default("")

// FIXED: accepts string | null | undefined, coerces to ""
note: z.string().nullish().transform(v => v ?? "")

The kit

  1. Assume LLMs send explicit nulls. Closed models tend to omit optional fields; open-weight models (Cerebras gpt-oss, in my case) emit "note": null. Both must parse.
  2. Sweep your schemas for the pattern. Every .default(...) and .optional() on a field an AI (or generated client code) can write is a candidate. Convert to .nullish().transform(...):
const LlmSafe = <T>(schema: z.ZodType<T>, fallback: T) =>
  schema.nullish().transform(v => v ?? fallback);
  1. Test null explicitly, per field. undefined, null, and omitted are three different test cases. My regression test is literally named "accepts a null note from the voice agent."
  2. Never let a permanent 400 be silent. My sync queue treated 400 as "permanent reject, drop it" (correct) but nothing told the user. A dropped write must surface (toast, retry state, anything).
  3. Guard the reconcile step. If a locally-created row isn't on the server AND isn't in the outbound queue, don't silently prune it. That's the "vanish after 1 second" mechanic. Keep-local guards or a dead-letter state.
  4. Fix the class, not the instance. I fixed the split-breakdown field, then shipped the identical bug on the transcript field, then on the note. After the third, I audited every nullable field in one pass. Do the audit on bug #1.
  5. Downstream stays typed. The ?? "" coercion means .note.trim() never explodes on null. Pick fallbacks that keep downstream code branch-free.

Steal this for your app

Run this on your codebase

Paste this into Claude Code in your repo:

Audit this repo's Zod schemas for null-vs-undefined traps on LLM-facing inputs.
Find every .default() and .optional() on fields that an LLM, voice agent, or generated client can write.
For each, remember .default() only fills undefined, so an explicit null still fails parsing and 400s.
List every field an LLM could send null to, and propose z.string().nullish().transform(v => v ?? "") style fixes.
Check the sync or retry layer: flag anywhere a 400 rejection drops a write without telling the user.
Check reconcile logic for paths that silently prune locally created rows missing from the server.
Report findings as a checklist before changing anything.

Get the next one in your inbox