Sub topic 9
Requesting and Validating Structured Output
This chapter assumes you can write short shell scripts, read process exit codes, and have a headless agent job that already runs on a schedule. It covers how to make the agent's result machine-readable and how to check it before anything else acts on it.
Why free text is a poor result format
An unattended job has no person to read the agent's final message. Later steps, such as opening a pull request or sending an alert, need to read the result. Parsing prose with grep is fragile, because a small wording change in the agent's reply can break your script without any error.
Ask the agent to write a JSON file instead. Then your script can check the file's shape and fail loudly if it is wrong.
Requesting the output
Put the format in the prompt, and name the file path. Be exact about field names and types. Here is a section you might add to prompts/nightly.md:
When you finish, write a file named result.json in the repository root.
It must contain only valid JSON with exactly these fields:
- "status": the string "ok" if you completed the task, or "failed" if you did not
- "changed_files": an array of file paths you modified (empty if none)
- "summary": one sentence describing what you did
Do not print the JSON to the terminal. Write it to the file only.Writing to a file keeps the result apart from the agent's logging output, so progress messages cannot corrupt the JSON.
Validating before acting
The agent can still produce a missing file, invalid JSON, or the wrong field types. Treat the file as untrusted input. Check it, and exit non-zero if the check fails.
# Fail the job if the agent's result file is not the shape we asked for.
if ! jq -e '.status == "ok" and (.changed_files | type == "array")' result.json > /dev/null; then
echo "result.json is missing or malformed" >&2
exit 1
fiThe -e flag makes jq set its exit status from the last expression. It exits 0 only when the result is neither false nor null. A missing file, a parse error, or a failed condition all produce a non-zero status, so the if ! branch runs in each case.
Where validation fits
Run the check after the agent exits, and only when the agent's own exit status was 0. Delete any old result.json before each run. Otherwise a stale file from a previous night can pass validation when the agent never ran.
Common errors
| Problem | Cause | Fix |
|---|---|---|
| Validation passes on a run that did nothing | Stale `result.json` from an earlier run | Remove the file before each run |
| `jq` reports a parse error | Agent wrapped the JSON in prose or code fences | State "only valid JSON" in the prompt and write to a file |
| Check passes but values are nonsense | Validation tested only the types | Check values your next step relies on, such as paths inside the repository |
| `jq: command not found` under the scheduler | Minimal `PATH` in cron | Set `PATH` in the crontab or call `jq` by full path |
Validation checks the shape of the output, not whether the agent told the truth. Confirm important claims independently. For example, compare changed_files against git status before committing.
Exercise
Your job runs the agent, then validates result.json with jq -e. One night the agent crashes at startup, yet validation passes and the job reports success. What is the most likely cause, and how do you prevent it?
Show answer
A result.json from a previous successful run was still on disk, so the check passed on stale data. Delete result.json before starting the agent, and also check that the agent's exit status is 0 before running the validation.