#!/usr/bin/env bash # Structured outputs: output_config.format {type:"json_schema", schema} — the text block is guaranteed-valid JSON. # STATUS: LIVE_VERIFIED 2026-09-18 (claude-haiku-4-5-20251001): {"name":"Ada","age":36,"city":"Lyon"} (~$0.0003). # Run: bash examples/anthropic/structured-output/json_schema.sh set -eo pipefail cd "$(dirname "$0")/../../.." && source scripts/lib.sh OUTF="$(mktemp)"; trap 'rm -f "$OUTF"' EXIT # `ant` sets ANT_STATUS; calling it inside $(...) would lose it, so write the body to a file instead. call() { ant "$@" > "$OUTF"; OUT="$(cat "$OUTF")"; } MODEL="${ANTHROPIC_MODEL:-claude-haiku-4-5-20251001}" call POST /v1/messages -d "$(jq -n --arg m "$MODEL" '{model:$m, max_tokens:100, output_config:{format:{type:"json_schema", schema:{type:"object", properties:{name:{type:"string"}, age:{type:"integer"}, city:{type:"string"}}, required:["name","age","city"], additionalProperties:false}}}, messages:[{role:"user", content:"Ada is 36 and lives in Lyon. Extract the person."}]}')" echo "HTTP $ANT_STATUS" echo "$OUT" | jq -r '.content[0].text' | jq -c . # second jq proves it parses log_request anthropic POST /v1/messages "$ANT_STATUS" 0.0003 "example structured-output/json_schema.sh $MODEL"