AI Engineer Portal
Your personal operating system for career transition.
Exercise
Parse and extract structured data from LLM JSON responses
Parse and Extract Structured Data from LLM JSON Responses
LLMs asked to return JSON do so imperfectly. They wrap output in markdown code fences, add explanatory prose before the JSON, omit required fields, or return strings where you expect numbers. A single fragile json.loads(response) call is not a production parsing strategy.
What to build
Implement three functions and a Pydantic model:
1. strip_fences(text: str) -> str — remove markdown code fences (```json, ```) from the start and end of a string.
2. extract_json_object(text: str) -> str | None — find the first complete {...} or [...] block in the text using a regex. Return None if none is found.
3. parse_into(text: str, model_class: type[T]) -> T | None — combine the above: strip fences, extract JSON, parse with json.loads, validate with Pydantic. Return None on any failure, never raise.
4. EntityExtraction — a Pydantic model with:
entities: list[str]— required, at least 1 itemsentiment: Literal["positive", "neutral", "negative"]confidence: float— between 0.0 and 1.0
Test cases to pass
Input: '{"entities": ["Paris", "Eiffel Tower"], "sentiment": "positive", "confidence": 0.9}'
Expected: EntityExtraction parsed successfully
Input: '```json\n{"entities": ["London"], "sentiment": "neutral", "confidence": 0.8}\n```'
Expected: fences stripped, parsed successfully
Input: 'Here is the result: {"entities": ["NYC"], "sentiment": "positive", "confidence": 0.95}'
Expected: JSON extracted from prose, parsed successfully
Input: '{"sentiment": "positive", "confidence": 0.7}' (missing entities)
Expected: returns None (validation failure)
Input: 'No JSON here at all'
Expected: returns None
Why this matters
This three-layer pattern — fence stripping, JSON extraction, schema validation — handles 95% of real LLM structured output failures. Build it once and reuse it across every prompt task that returns structured data.
Python Refresh / medium / Step 14 of 16
Core runtime habits
Normalize the boundary first, keep return shapes stable, and make the middle of the function boring on purpose.
- - Returns one stable shape across branches
- - Makes failure handling obvious
- - Keeps the core logic readable in under a minute
- - Did I make the input and output shapes explicit?
- - Would a teammate know where validation happens?
- - Did I avoid silent mutation or vague branching?
Practice
Generate a variation
Generate a new exercise variation to deepen understanding or practice a related concept.
Attempt history
Recent submissions
Before you submit, decide what a strong answer should make obvious to the reviewer.
No attempts yet.