Repository navigation
Description
Summary
process_stream_line in claude_code.py crashes with AttributeError: 'str' object has no attribute 'get' when a Claude Code stream-json message's content array contains a string element (not a dict). The exception kills the stdout reader thread mid-execution, leaving 0 tool calls / 0 turns recorded and causing the execution to be classified as a 502 "Execution completed without a result message".
This is distinct from the pipe-drain fix in #531 — that fix addressed the final result line being dropped when the pipe was closed early. This bug is a missing type guard in the content block iteration loop inside process_stream_line.
Component
Agent Runtime / claude_code.py
Priority
P1 — Scheduled tasks silently fail with 502 whenever Claude Code emits a message with a string content block. No workaround.
Error
ERROR:agent_server.services.claude_code:[Headless Task] Error reading stdout: 'str' object has no attribute 'get'
ERROR:agent_server.services.task_execution_service:[TaskExecService] Failed to execute task on [agent]: Execution completed without a result message after 0 tool calls / 0 turns (raw_messages=5).
Location
- File:
docker/agent-server/agent_server/services/claude_code.py - Function:
process_stream_line - Approximate line:
for content_block in message_content: block_type = content_block.get("type")
Root Cause
process_stream_line correctly guards the top-level msg object:
if not isinstance(msg, dict):
returnBut the inner loop over message_content does not guard individual content_block elements:
message_content = message.get("content", [])
for content_block in message_content:
block_type = content_block.get("type") # ← AttributeError if content_block is strClaude Code's stream-json content array can contain plain strings (e.g. in user role messages or certain tool-result payloads). When it does, content_block.get("type") raises AttributeError: 'str' object has no attribute 'get'.
The exception propagates up through read_stdout() and is caught by:
except Exception as e:
logger.error(f"[Headless Task] Error reading stdout: {e}")This terminates the for loop, so the reader exits immediately. Since metadata was never fully populated, _classify_empty_result fires and the execution is marked as a 502 failure.
Note: the error-content loop just a few lines above this one already has the correct guard:
for block in error_content:
if isinstance(block, dict) and block.get("type") == "text":The same pattern needs to be applied to the main content loop.
Reproduction Steps
- Run a headless task on any agent using Claude Code stream-json output
- Claude Code emits an
assistantorusermessage where thecontentarray contains at least one string element (not a dict) process_stream_lineraisesAttributeErrorand the reader thread exits- Execution is recorded as failed: "Execution completed without a result message after 0 tool calls / 0 turns"
Suggested Fix
Add an isinstance guard at the top of the content_block loop in process_stream_line:
for content_block in message_content:
if not isinstance(content_block, dict):
continue # stream-json content arrays can contain plain strings
block_type = content_block.get("type")
...This mirrors the existing guard on the error_content loop a few lines above.
Evidence
Observed execution:
- Task:
/synthesize-learnings(scheduled) - Duration: ~71s before crash
raw_messages=5,0 tool calls,0 turns- Execution model:
claude-opus-4-6 - Error log:
[Headless Task] Error reading stdout: 'str' object has no attribute 'get' - Backend log:
Agent responded: HTTP 502 (71755ms)
Related
- bug: drain_reader_threads closes stdout pipe before reader can drain backlog — silently loses final result line on long agentic tasks #531 — drain pipe fix (separate issue: pipe closed before reader drains; this is unrelated)
docker/agent-server/agent_server/services/claude_code.py—process_stream_linefunctiondocker/agent-server/agent_server/services/claude_code.py—read_stdoutinner function in headless task execution path
Activity
vybe commentedon Apr 27, 2026
/claim
github-actions commentedon Apr 27, 2026
Claimed by @vybe. Label
status-in-progressadded.Next steps:
feature/542-<short-slug>Fixes #542vybe commentedon Apr 27, 2026
Fixed by #543 (merged).
fix(agent-runtime): guard content_block isinstance in process_stream_…
Release: v0.5.0 (#608)