Skip to content
AgenticAIWiki
  1. Learn
  2. Part 2
  3. 14. Structured output
  4. 14.3

Lesson 14.3 · 3 of 8 · 4 min read · Reviewed 24 Nov 2026

Nullable fields so the model doesn't invent values

By the end, you can

  • Use nullable fields so the model doesn't make up values.
  • Write field descriptions that say what to do when data is missing.

A schema that says "phone_number": {"type": "string"} and makes it required gives the model no way to say “there isn’t one”. So sometimes it invents one that looks plausible.

Fix it in the schema

"phone_number": {
  "type": ["string", "null"],
  "description": "Customer's phone number exactly as written. Null if not stated."
}

Now null is a valid and honest answer.

Patterns that help

  • Nullable plus required: the field is always present, so code doesn’t have to check for it, and can be null.
  • “Exactly as written”: stops reformatting or guessing.
  • An “unknown” enum value for categories: ["bug", "billing", "other", "unclear"].
  • A confidence or evidence field for high-stakes values, such as "source_quote" containing the exact text the value came from.

Test with inputs that lack each field. A good extractor returns null, not fiction.

Key points

  • If a field is required and not nullable, the model must put something there, even when the source has nothing.
  • Make such fields nullable and say 'null if not stated in the text'.
  • This is one of the simplest and most effective ways to reduce invented values in extraction.
Hands-on labRuns in the Playground

Test an extractor on missing fields

Show how a required, non-nullable field invites invented values, and test that a nullable version returns null instead.

You will have: Two simulated extractors and a missing-field test suite that counts invented values for each.

Steps

  1. Read the two simulated extractors: one behaves as if every field must be filled, one as if fields are nullable.
  2. Complete the test cases: for each input, record which fields are truly present in the text.
  3. Complete invented so it counts fields that have a value although the source text does not contain it.
  4. Run both extractors and compare the invented counts.

Done when

  • The forced extractor shows at least one invented value.
  • The nullable extractor prints 'invented values: 0'.
  • Every test input lacks at least one field, so each field's null path is tested.

Open in the Playground No API key needed.

Starter code (Python)
import re

def forced_extractor(text):
    # Simulates a schema where every field is required and not nullable: something must go in each slot.
    phone = re.search(r"\b0\d{3,4} ?\d{6,7}\b", text)
    order = re.search(r"order (\d{5})", text)
    return {"phone_number": phone.group(0) if phone else "020 7946 0000",
            "order_id": order.group(1) if order else "10001"}

def nullable_extractor(text):
    # Simulates "type": ["string", "null"] with "null if not stated".
    phone = re.search(r"\b0\d{3,4} ?\d{6,7}\b", text)
    order = re.search(r"order (\d{5})", text)
    return {"phone_number": phone.group(0) if phone else None,
            "order_id": order.group(1) if order else None}

CASES = [
    # TODO: for each text, fill "present" with the fields that really appear in it.
    {"text": "Hi, order 58213 arrived cracked. Thanks, Priya", "present": []},
    {"text": "Please call me on 0161 4960123 about my parcel.", "present": []},
    {"text": "The box fell apart in the rain.", "present": []},
]

def invented(result, present):
    # TODO: count fields with a non-None value that are not in present.
    return 0

for name, fn in [("forced", forced_extractor), ("nullable", nullable_extractor)]:
    total = sum(invented(fn(c["text"]), c["present"]) for c in CASES)
    print(f"{name}: invented values: {total}")
Show a solution
import re

def forced_extractor(text):
    # Simulates a schema where every field is required and not nullable: something must go in each slot.
    phone = re.search(r"\b0\d{3,4} ?\d{6,7}\b", text)
    order = re.search(r"order (\d{5})", text)
    return {"phone_number": phone.group(0) if phone else "020 7946 0000",
            "order_id": order.group(1) if order else "10001"}

def nullable_extractor(text):
    # Simulates "type": ["string", "null"] with "null if not stated".
    phone = re.search(r"\b0\d{3,4} ?\d{6,7}\b", text)
    order = re.search(r"order (\d{5})", text)
    return {"phone_number": phone.group(0) if phone else None,
            "order_id": order.group(1) if order else None}

CASES = [
    {"text": "Hi, order 58213 arrived cracked. Thanks, Priya", "present": ["order_id"]},
    {"text": "Please call me on 0161 4960123 about my parcel.", "present": ["phone_number"]},
    {"text": "The box fell apart in the rain.", "present": []},
]

def invented(result, present):
    return sum(1 for k, v in result.items() if v is not None and k not in present)

for name, fn in [("forced", forced_extractor), ("nullable", nullable_extractor)]:
    total = sum(invented(fn(c["text"]), c["present"]) for c in CASES)
    print(f"{name}: invented values: {total}")
Quick checkAn extractor fills phone_number with a made-up value when the email has none. What's the best fix?
How sure are you?
Pick an answer and how sure you are