[BUG] `claude -p --output-format json` omits 'result' field on --resume when session already has a last-prompt entry

Resolved 💬 3 comments Opened Mar 20, 2026 by nopa12 Closed May 4, 2026

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet (closest: #8126, #7124 — same symptom, different trigger)
  • [x] This is a single bug report
  • [x] I am using the latest version of Claude Code

What's Wrong?

When using --output-format json with --resume, the stdout JSON is missing the result field entirely (not just empty — the key is absent). All other metadata fields are present and correct (session_id, num_turns, usage, stop_reason=end_turn, is_error=false, total_cost_usd, duration_ms).

The actual response text is generated correctly — it appears in the session .jsonl file as the last type=assistant / stop_reason=end_turn entry. But it is not surfaced in the stdout JSON.

What Should Happen?

The result field should contain the assistant's final response text, as it does on the first (non-resumed) invocation.

Steps to Reproduce

# First invocation — works correctly, result is present in stdout JSON
SESSION=$(claude -p "explain x509 errors" --output-format json | jq -r .session_id)

# Second invocation — result field is ABSENT from stdout JSON
claude -p "now dig deeper: search tickets, code, and docs" \
  --output-format json \
  --resume "$SESSION"

The second invocation produces JSON like:

{
  "session_id": "3823925d-28c3-428d-9cc0-ffc90921699e",
  "is_error": false,
  "stop_reason": "end_turn",
  "num_turns": 9,
  "duration_ms": 66100,
  "total_cost_usd": 0.5041,
  "usage": { "input_tokens": 1, "cache_read_input_tokens": 99376, "output_tokens": 1011, ... }
}

Note: result key is completely absent. Not "", not null — absent.

Root Cause Hypothesis

The session .jsonl file written by the first invocation ends with a {"type":"last-prompt",...} entry. When the second --resume invocation runs, this entry is already present. The CLI appears to use the last-prompt entry to locate the response to serialize into result, but when a pre-existing last-prompt is present, the lookup fails and result is silently omitted from the stdout JSON.

Evidence from the session .jsonl:

  • Line N: {"type":"last-prompt","lastPrompt":"...first prompt...","sessionId":"..."} ← written by first invocation
  • Lines N+1 to N+M: second invocation conversation (user message → tool calls → final assistant response)
  • Line N+M: {"type":"assistant","message":{"stop_reason":"end_turn","content":[{"type":"text","text":"<full response>"}]},...} ← text IS here
  • Line N+M+1: {"type":"last-prompt","lastPrompt":"...second prompt...","sessionId":"..."} ← written by second invocation

The response text at line N+M is correct and complete. The result field in stdout JSON should contain it but doesn't.

Relation to Existing Issues

This is a different trigger from #8126 and #7124 (which involve stream-json and TodoWrite). Those issues describe result: "" (empty string); this issue produces a missing key (result is not in the JSON at all). The common thread is that result is not reliably populated in the stdout JSON.

Error Messages/Logs

No error from the CLI. Exit code 0. The only indication is the missing key in the stdout JSON.

Claude Code Version

2.1.77

Claude Model

claude-sonnet-4-6

Platform

Anthropic API

Operating System

Linux (Ubuntu)

Terminal/Shell

Non-interactive/CI environment (via docker exec)

Additional Information

Running Claude CLI inside a Docker container via docker exec, invoked programmatically. The --resume flag is used to maintain conversation context across Slack thread messages in a bot application. The bug causes the bot to post an empty response to users.

Workaround: detect missing result key after parsing stdout JSON and return an error asking the user to retry.

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗