
Lesson 3: Required vs Optional Fields
Master the enforcement of data acquisition. Learn how to use the 'required' array in JSON Schema to force Claude to seek out specific information before completing a task.
Module 8: Structured Output and Schema Design
Lesson 3: Required vs Optional Fields
One of the most common failures in AI extraction is Missing Data. You ask Claude to extract info from an invoice, and it "Forgets" the tax ID because it wasn't prominently displayed. In a standard prompt, you can only hope. In a Schema, you can Enforce.
In this lesson, we master the required field and learn how it influences the model's "Search Priority" during the perceive phase (Module 2, Lesson 2).
1. The required Array
In JSON Schema, "Required-ness" is not defined inside the property. It is defined in a separate top-level array called required.
{
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" },
"email": { "type": "string" }
},
"required": ["name", "email"]
}
In the example above, Claude MUST provide name and email. It may provide age if it finds it, but it won't fail if it doesn't.
2. Using "Required" for Behavioral Steering
When you mark a field as required, Claude's attention mechanism prioritizes it. If it doesn't see a "Name" in the input, and the name is "Required," Claude will often:
- Hallucinate a name (if not guardrailed).
- Ask the user for the name (if in an agent loop).
- Report an error explaining the missing info.
Architect's Move: Only require fields that are truly mission-critical. If you require 50 non-essential fields, the model will waste "Reasoning Power" trying to guess them.
3. The "Conditional Optional" Pattern
Sometimes a field is only required if another field exists. While advanced JSON Schema supports this (if/then), for AI, it is better to handle this via Prompt Instructions.
- Advice: Keep your schemas flat and simple. If logic gets complex, split it into two different tools (Module 4, Lesson 4).
4. Default Values and placeholders
JSON Schema allows for default values.
- Warning: If you provide a default value (e.g.,
"country": "USA"), Claude might stop looking for the actual country in the text. - Architect's Advice: Avoid
defaultvalues for data extraction. Let the model fail or outputnullso your system knows the data was missing.
5. Visualizing the Required Filter
graph TD
Input[Unstructured Data] --> Model[Claude]
Model --> Check{Required Fields?}
Check -->|Missing One| Fail[Error / Ask User]
Check -->|All Present| Pass[Valid JSON Output]
Check -->|Extras Present| Pass
6. Summary
requiredis a list of strings at the top of an object.- Requiring a field tells Claude "Don't finish until you have this."
- Avoid excessive required fields to maintain high model focus (SNR).
In the next lesson, we look at the most powerful constraint: Enums and Constraints.
Interactive Quiz
- Where is the
requiredkeyword placed in a JSON schema? - What is the difference between a
requiredfield and anoptionalfield in terms of model attention? - Why should you avoid using the
defaultkeyword in data extraction tasks? - Scenario: You want an agent to extract "Date of Birth." Is it better to make it required or optional if some users might not provide it? Why?
Reference Video: