Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
AI

The AI Prompt That Turns an API Response Into a Typed Model

- 24.11.26 - ErcanOPAK

🤖 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.

— AI Power User

Related posts:

The AI Prompt That Finds Every Spot Where a Loop Recomputes the Same Expensive Value on Every Iterat...

The AI Prompt That Finds Every Spot Where a Loop Recomputes the Same Expensive Value on Every Iterat...

AI Prompt: Creating Perfect OpenAPI (Swagger) Documentation

AI Prompt: Creating Perfect OpenAPI (Swagger) Documentation

The AI Prompt That Catches Missing Signature Verification in Your Webhook Handler Before It Trusts t...

The AI Prompt That Catches Missing Signature Verification in Your Webhook Handler Before It Trusts t...

Views: 12
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

CSS: Fix a :has() Selector Missing Matches After a DOM Swap
The AI Prompt That Spots a Memory Leak in a Long-Running Loop

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 629 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com