Structured Output via Tool Use & JSON Schemas

Core

Enforce structured output using tool use and JSON schemas · Difficulty 3/5

0%
structured-outputjson-schematool-use

For automated pipelines, you need guaranteed Structured Output -- not narrative text that might vary. Tool use with JSON schemas is the most reliable approach.

Approaches (Least to Most Reliable)

  • Prompt-based formatting: Ask for Structured Output in instructions -- unreliable
  • Few-shot examples: Demonstrate exact format -- good for interactive use
  • **CLI Structured Output**: --output-format json --json-schema -- guaranteed for CLI
  • API tool use: Define tools with JSON Schema for structured responses -- most reliable
  • Why Tool Use Wins

    Tool use with JSON schemas guarantees schema-compliant output. The model MUST return data matching the schema, eliminating JSON syntax errors entirely. However, it does NOT prevent semantic errors (e.g., line items that don't sum to total, values in wrong fields).

    Schema Design Best Practices

  • Required vs optional fields: Make fields optional (nullable) when source data may not contain the information. This prevents the model from fabricating values to satisfy required fields.
  • Enum fields: Use enum values like "unclear" for ambiguous cases.
  • Extensible categories: Use "other" + detail string pattern for categories that may expand.
  • Format normalization: Include normalization rules in prompts alongside strict schemas to handle inconsistent source formatting.
  • Key Takeaways

    • Tool use with JSON schemas eliminates syntax errors but not semantic errors
    • Make fields optional/nullable when source data may not contain the information
    • Use enum + 'other' + detail string pattern for extensible categories
    • Prompt-based formatting is unreliable for automated pipelines

    Test Yourself1 of 1

    Your CI pipeline runs the Claude Code CLI using CLAUDE.md to provide project context for code reviews. However, developers report that integrating findings into your workflow is difficult — Claude produces narrative paragraphs that must be manually copied into PR comments. Your team wants to automatically post each finding as a separate inline PR comment at the relevant code location. What's the most effective approach?