Claude Code script output: result versus structured_output
Separate the JSON response envelope from schema output; check field presence before business validation and error handling.
Read the response envelope first · Separate schema validity from format checks · Stop consumption when structured_output is missing
At a glance
The goal is to read a non-interactive Claude Code response correctly in a script. Original local parsing fixtures follow the official headless page. Claude Code and a model were not invoked, and no real project file was used. Parseable JSON does not establish valid business fields.
Runtime and applicability
- Article type
- Spec file
- Verified
- Applies to
- Claude Code Applies to Claude Code as documented; version, model and environment prerequisites are stated in the body.
- Tools
File locations
| Path | Scope | How it is read |
|---|---|---|
脚本的 Claude Code CLI 参数 / Claude Code CLI arguments in a script | Non-interactive CLI output contract; these are not settings.json keys. | Read by claude -p as --output-format and --json-schema arguments at invocation. |
When to use
- When a script needs structured results and explicit handling of responses with no valid schema output.
When not to use
- When model quality or tool isolation must be established, check those separately; output format provides neither.
Read the response envelope first
--output-format text is the default plain-text format; json returns a JSON envelope with the result, session ID and metadata; stream-json returns newline-delimited events. The ordinary summary example places text in result. Schema output uses json with --json-schema and places the business object in structured_output. Do not pass the entire envelope to a downstream consumer that expects business fields only.
Separate schema validity from format checks
The docs say an invalid JSON Schema makes the CLI exit with a validator diagnostic. format is accepted as an annotation and not enforced, so format: email does not guarantee a valid email address. The exercise business field is functions, requiring an array of strings. Check that required fields and names match the response path, then validate actual business requirements on the client.
Stop consumption when structured_output is missing
The docs explicitly note that a run may finish without valid structured output, for example after a refusal, leaving no structured_output field. Do not silently fall back to result as though it were the same contract. Original fixtures include a valid functions array, a missing-field envelope and a wrong field type. The local parser accepts only the valid array and routes the others to error handling; it never invents function names.
Record the actual failure scope
Only synthetic JSON parsing and field-type assertions were executed. CLI exit codes, service refusal, schema enforcement and model extraction accuracy were not verified. In a disposable environment, inspect exit status, response fields and client validation separately and retain original errors. stream-json is not one JSON document; do not JSON.parse its entire multiline output. If switching to streaming, implement parsing against the documented event format.
Sources
All links are official pages; verification dates are listed per source, and links point to the version checked.