
【Python×Anthropic】How to Implement a Robust Pipeline for 100% Reliable Extraction of “Structured JSON” with Claude 3.5 Sonnet
- Python
- Claude
- AI
- LLM
- pydantic
Hello, this is AI Hack Lab (@ai_hack_lab). We usually share practical know-how on business automation using Python and generative AI, as well as hands-on prompt engineering techniques.
In the previous article (【Python×Claude API】Implementing a “Prompt Auto-Optimization & Quality Scoring” Pipeline for Practical Use), we explained a pipeline that uses LLMs to automatically validate and improve prompt quality.
This time, we’ll give a full walkthrough of how to fundamentally solve the “problem where malformed JSON output from an LLM causes parse errors” and build arobust implementation method for extracting and validating structured data with 100% certainty.
1. The “Three Major Failures” Common in LLM Structured Output
Even with high-performance models such as Claude 3.5 Sonnet and GPT-4o, merely specifying “Please output in JSON format” in the prompt makes the following failures highly likely in real-world automated batch processing.
- Inclusion of Markdown formatting: Code fences such as ```json ... ``` are included, causing the standard json.loads() to crash immediately.
- Missing required fields or inconsistent key names: Keys that were supposed to be specified are subtly changed (e.g., user_name becomes userName), or the nested structure breaks.
- Data type mismatch: A string is inserted where a number is expected, or null is returned instead of an empty array.
In an automated pipeline, processing being halted by a parse error is fatal. It is impossible to prevent all of these issues 100% of the time using prompt instructions alone.
2. Solution: A Robust Design Using Pydantic + Instructor
The simplest and most reliable way to solve this problem is the combination ofPydantic, a Python data validation library, andInstructor, which supports structured output from LLMs.
Key Architecture Points
- Schema-driven: Strictly define the output format and types using Pydantic's BaseModel
- Automatic validation: Immediately parse the LLM output and perform type checking
- Self-healing (Auto-retry): If a validation error occurs, feed the error details back to the LLM and automatically regenerate and correct the output
3. Full implementation code
The following is complete code that uses Claude 3.5 Sonnet (Anthropic API) to extract a “task list, priorities, and effort estimates” from unstructured text (business memos and meeting minutes) into structured JSON with 100% certainty.
4. Three Important Tips for Practical Use
-
Carefully write the Field's description
The LLM treats the explanations written in the description, not just the field names, as powerful prompts. By explicitly stating "what values are expected" and "what the constraints are," accuracy improves dramatically. -
Narrowing down the available options with Literal or Enum
For categorical values such as status and priority, instead of using a string type (str), fixing the available options, as in Literal["High", "Medium", "Low"], completely eliminates inconsistencies in how the values are written. -
Setting max_retries
Even if the LLM happens to return an invalid format, instructor automatically regenerates the output by internally including in the prompt which field encountered which validation error. In practice, setting max_retries=2 or 3 achieves nearly 100% success.
5. Summary
In automated tools and web services that incorporate LLMs, the robustness of the output is directly tied to the reliability of the product.
By combining Pydantic and Instructor, you can implement a safe, robust data pipeline with minimal code. We hope you find this useful in practice.
📚 Reference Links & Official Materials
Our lab publishes AI automation know-how and prompt collections that can be used for professional work and side hustles.
- 🔰 [Try for ¥100] 3 Fast-Acting Prompts for AI Side-Hustle Beginners to Earn Their “First ¥1”
https://note.com/calm_alpaca101/n/nfb2a297a7eb6
※Limited to the first 10—includes a bonus prompt for securing immediate-payment jobs - 📦 [Complete Collection] AI Hack Starter Complete Mastery Pack (Bundle Discount)
https://note.com/calm_alpaca101/m/ma9d054614818 - 🌐 AI Hack Lab Official Blog (Hatena Blog)
https://ai-hack-lab.hatenablog.com/
Go to list of users who liked
Go to list of comments
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme





