🤖 The AI Prompt That Turns an API Response Into a Typed Model
Hand-writing a model class from a JSON response means guessing which fields are ever actually null, which numbers are really integers versus decimals, and which arrays can come back empty, and most of the time what gets typed is just whatever one example response happened to contain. A better pattern is to paste two or three real response samples, including at least one edge case like an empty array or a null field, and ask the model to infer each field’s type from what’s actually present across every sample rather than only the friendliest one.
🔎 The Prompt
Here are 3 real JSON responses from the same API endpoint, including one with a null optional field and one with an empty results array. Generate a [C#/TypeScript/ Python] model class that: 1. Marks a field nullable only if it is actually null or absent in at least one sample 2. Uses the narrowest numeric type that fits every sample's values, not just the first 3. Names any field that could be a date/time field and asks me to confirm the format before assuming one 4. Flags any field present in only one sample as possibly optional rather than required Do not silently assume the shape of the first sample applies to every response.
✅ What Makes This Prompt Actually Work
- Always include at least one messy real sample, an error response, an empty collection, or a field present in one record but absent in another, alongside the happy-path example, since a model generated from only the cleanest sample encodes the cleanest case as if it were the only case.
- Ask the model to list every field it marked non-nullable and why, so you can spot-check its reasoning against a sample you didn’t paste in, rather than trusting a plausible-looking class blind.
- Re-run the same prompt whenever the API’s documentation changes or a new error code is introduced, since a typed model generated once against last year’s samples silently drifts from what the API actually returns today.
🚨 Why This Is Dangerous Without the Edge Cases
- A model generated from a single clean sample response will compile and work perfectly in every manual test, then throw a null-reference or type-mismatch exception the first time production traffic hits the one field the sample never happened to exercise.
A typed model is only a hypothesis about the API’s shape — the quality of that hypothesis depends entirely on how many real responses you showed it before asking.





