【Python×Anthropic】Claude 3.5 Sonnetで「構造化JSON」を100%確実に抽出する堅牢パイプラインの実装手法 #AI - Qiita
@ai_hack_lab(AI Hack Lab)

【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
0
Posted at 2026-11-12

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.

  1. Inclusion of Markdown formatting: Code fences such as ```json ... ``` are included, causing the standard json.loads() to crash immediately.
  2. 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.
  3. 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.

import os from typing import List, Literal from pydantic import BaseModel, Field import instructor from anthropic import Anthropic # 1. Strictly define the expected output structure with Pydantic class TaskItem(BaseModel): title: str = Field(description ="Specific task name")priority: Literal["High", "Medium", "Low"] = Field(description= "Priority") estimated_hours: float = Field(description= "Estimated workload (hours)", ge= 0.5) tags: List[str] = Field(description= "Related category tags (up to 3)") class TaskExtractionResponse(BaseModel): summary: str = Field(description= "Overall summary (within 100 characters)")tasks: List[TaskItem] = Field(description="List of extracted tasks") total_estimated_hours: float = Field(description= "Total estimated hours") # 2. Wrap the Anthropic client with Instructorclient = instructor.from_anthropic(Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))) def extract_structured_tasks(raw_text: str) -> TaskExtractionResponse: """Safely extract task structures from unstructured text""" response = client.messages.create( model="claude-3-5-sonnet-20241022", max_tokens=2048, response_model=TaskExtractionResponse, max_retries=3, # Automatically retry when validation fails messages=[ { "role": "user", "content": f"Please extract tasks from the following memo and return them in the specified structure:\n\n{raw_text}", } ], ) return response # 3. Execution sample if __name__ == "__main__": sample_memo = """ About preparations for next week's release. First, finish unit testing of the payment functionality by Monday (approximately 3 hours). Also, change the LP catchphrase and replace the main visual on Tuesday morning (2 hours). Integration testing in the staging environment is scheduled to be conducted by the entire team on Wednesday afternoon (4 hours). """ result = extract_structured_tasks(sample_memo) print(f"【Overview】: {result.summary}") print(f"【Total estimated hours】: {result.total_estimated_hours} hours ") for t in result.tasks:print(f"- [{t.priority}] {t.title} ({t.estimated_hours}h) Tags: {', '.join(t.tags)}")

4. Three Important Tips for Practical Use

  1. 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.

  2. 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.

  3. 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.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
Sign upLogin