Put the Schema in the Prompt

schema-in-the-prompt

Part of: Structured JSON Extractor

The model returns whatever shape it feels like unless you tell it exactly what you want. "Give me the contact info" gets you a friendly paragraph. The fix is to put the schema in the prompt . Spell out the exact keys, the exact types, and ask for JSON and nothing else. Describe the shape, key by key Don't ask for "the data." Ask for a JSON object with named keys and say what each one holds: Building the prompt from the schema dict gives you one source of truth. Change the schema and the instructions change with it. They can't drift apart. Show one tiny example A single example pins down the shape faster than a paragraph of rules. This is one-shot prompting : include an input and its ideal JSON output right in the system prompt, and the model pattern-matches on it. Say what to do with missing data The most common bug in extraction is invention. There's no phone in the text, so the model makes one up. Head it off in the prompt: "If a field is not present in the text, use null. Never guess." That one sentence turns a hallucinating extractor into an honest one. The real call That json.loads is the happy path. It works when the model returns bare JSON. Later lessons handle the frequent

Challenge: Schema Prompt Builder