examples/anthropic/structured-outputs/README.md
This example demonstrates Anthropic's structured outputs feature, which ensures Claude's responses follow a specific schema. It shows both JSON outputs for structured data extraction and strict tool use for guaranteed schema validation on tool calls.
output_format to constrain Claude's responses to a JSON schemastrict: true on tools to guarantee type-safe function parametersexport ANTHROPIC_API_KEY=your_api_key_here
npx promptfoo@latest init --example anthropic/structured-outputs
npx promptfoo@latest eval
The first provider configuration shows how to extract structured data from unstructured text using a JSON schema:
providers:
- id: anthropic:messages:claude-sonnet-4-6
config:
output_format:
type: json_schema
schema:
type: object
properties:
customer_name:
type: string
customer_email:
type: string
# ... more fields
required:
- customer_name
- customer_email
additionalProperties: false
Use JSON outputs when:
The second provider configuration demonstrates how to ensure tool parameters exactly match your schema:
providers:
- id: anthropic:messages:claude-sonnet-4-6
config:
tools:
- name: book_demo
strict: true # Enable strict mode
input_schema:
type: object
properties:
customer_email:
type: string
# ... more properties
required:
- customer_email
- customer_name
additionalProperties: false
Use strict tool use when:
JSON.parse() errorsBoth modes share these JSON Schema limitations:
✅ Supported:
enum (primitives only)required and additionalProperties: falseminItems (only 0 and 1)❌ Not supported:
minimum, maximum)minLength, maxLength){n,m} quantifiers in regex patternsThe example includes comprehensive tests:
additionalProperties: false is enforcedStructured outputs are available for:
claude-sonnet-4-6)claude-opus-4-1-20250805)