Bigger question: how do you ask for more than one field reliably?
prompt = '''Classify the following review.
Return JSON with exactly two fields:
- sentiment: one of "positive", "negative", "neutral"
- confidence: a float between 0 and 1
Review: "I love it"
Return only the JSON, no other text.'''The schema is the contract — the model has both the field names and their value constraints. Combined with day 12's try/except retry, this scales to 5-10 fields without falling apart.
And the asserts in verification — they check both fields?
Right. Multiple assert statements, one per invariant. If any fails, the lesson fails clearly. Pydantic typed output (day 12 mentioned briefly) does this validation automatically; the prompt-only version is what you do without that library.
When you need multiple structured fields, spell out the schema:
prompt = '''<task>
Return JSON with exactly these fields:
- field_a: <type> (constraint)
- field_b: <type> (constraint)
- field_c: <type> (constraint)
Return only the JSON, no other text.'''| Spec | Example |
|---|---|
| Enum | one of "positive", "negative", "neutral" |
| Number range | a float between 0 and 1 |
| Bounded int | an integer between 1 and 100 |
| String pattern | a date in YYYY-MM-DD format |
| Optional | or null if unknown |
| List | a list of strings, length ≤ 5 |
The more concrete the constraint, the more reliably the model honours it. Vague constraints (a number) get vague outputs ("high", "medium"); concrete (a float between 0 and 1) get 0.85.
Multiple invariants → multiple asserts:
assert isinstance(parsed, dict)
assert parsed.get("sentiment") in ("positive", "negative", "neutral")
assert isinstance(parsed.get("confidence"), (int, float))
assert 0 <= parsed["confidence"] <= 1Each assert is one shape claim. When the verification fails, the assert's message tells you which field was wrong.
Past 5-7 fields, the prompt-only approach gets unwieldy. Two upgrades:
BaseModel, pass output_type=YourModel, let the library validate.The prompt-only approach is the hands-on foundation; the upgrades are what production code uses.
Bigger question: how do you ask for more than one field reliably?
prompt = '''Classify the following review.
Return JSON with exactly two fields:
- sentiment: one of "positive", "negative", "neutral"
- confidence: a float between 0 and 1
Review: "I love it"
Return only the JSON, no other text.'''The schema is the contract — the model has both the field names and their value constraints. Combined with day 12's try/except retry, this scales to 5-10 fields without falling apart.
And the asserts in verification — they check both fields?
Right. Multiple assert statements, one per invariant. If any fails, the lesson fails clearly. Pydantic typed output (day 12 mentioned briefly) does this validation automatically; the prompt-only version is what you do without that library.
When you need multiple structured fields, spell out the schema:
prompt = '''<task>
Return JSON with exactly these fields:
- field_a: <type> (constraint)
- field_b: <type> (constraint)
- field_c: <type> (constraint)
Return only the JSON, no other text.'''| Spec | Example |
|---|---|
| Enum | one of "positive", "negative", "neutral" |
| Number range | a float between 0 and 1 |
| Bounded int | an integer between 1 and 100 |
| String pattern | a date in YYYY-MM-DD format |
| Optional | or null if unknown |
| List | a list of strings, length ≤ 5 |
The more concrete the constraint, the more reliably the model honours it. Vague constraints (a number) get vague outputs ("high", "medium"); concrete (a float between 0 and 1) get 0.85.
Multiple invariants → multiple asserts:
assert isinstance(parsed, dict)
assert parsed.get("sentiment") in ("positive", "negative", "neutral")
assert isinstance(parsed.get("confidence"), (int, float))
assert 0 <= parsed["confidence"] <= 1Each assert is one shape claim. When the verification fails, the assert's message tells you which field was wrong.
Past 5-7 fields, the prompt-only approach gets unwieldy. Two upgrades:
BaseModel, pass output_type=YourModel, let the library validate.The prompt-only approach is the hands-on foundation; the upgrades are what production code uses.
Create a free account to get started. Paid plans unlock all tracks.