AI Engineer Portal
Your personal operating system for career transition.
Exercise
Parse structured outputs with error recovery
Parse Structured Outputs with Error Recovery
LLMs are asked to produce JSON constantly — tool call arguments, structured answers, classification labels, extraction results. But LLM output is unreliable: it might be wrapped in markdown code fences, contain trailing commas, include explanatory text before or after the JSON, or be truncated mid-object.
What you are building
Create a parse_structured_output function and supporting utilities that:
- Extract JSON from markdown — strip
```json ... ```fences and other common wrappers. - Handle partial/truncated JSON — attempt to close unclosed braces and brackets to salvage partial responses.
- Validate against a schema — check the parsed object against a provided JSON Schema and return clear validation errors.
- Apply defaults — fill in missing optional fields with schema-defined defaults.
- Return a structured result — include the parsed data, whether recovery was needed, and any warnings.
Why this matters
In agent systems, every tool call argument and every structured response passes through a parsing step. If that step is fragile, your agent breaks on edge cases that are actually common in practice: the model wraps JSON in markdown 30% of the time, truncation happens when hitting token limits, and extra text before JSON is routine with weaker models.
Robust parsing is not a nice-to-have — it is what separates agents that work in demos from agents that work in production.
Constraints
- Do not use an LLM to fix the output — this must be deterministic.
- Handle at least: markdown fences, leading/trailing text, single trailing comma, unclosed braces/brackets (up to 3 levels).
- Return warnings for every recovery action taken so callers can log and monitor parse quality.
Agents / intermediate / Step 4 of 8
Agent architecture patterns
Focus on explicit control surfaces — function schemas, state machines, and structured outputs. Agents are most useful when the task requires dynamic tool selection or multi-step reasoning. Start with the simplest pattern (single tool call) before reaching for ReAct loops.
- - Tools return structured, typed responses
- - Agent completes the task within a bounded number of steps
- - All tool calls include error handling and retries
- - Memory/state management prevents unbounded context growth
- - Tool schemas validate inputs and handle errors gracefully
- - Agent loop has explicit termination conditions
- - State is serializable and inspectable between steps
- - Cost and token usage are tracked per invocation
- - Fallback behavior exists for tool call failures
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.