Skip to content

bug: process_stream_line crashes with 'str' object has no attribute 'get' when content block is a string #542

Closed

Description

@vybe

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):
    return

But 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 str

Claude 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

  1. Run a headless task on any agent using Claude Code stream-json output
  2. Claude Code emits an assistant or user message where the content array contains at least one string element (not a dict)
  3. process_stream_line raises AttributeError and the reader thread exits
  4. 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

Activity

  1. vybe commented on Apr 27, 2026

    @vybe
    ContributorAuthor

    /claim

  2. added
    status-in-progress
    on Apr 27, 2026
  3. github-actions commented on Apr 27, 2026

    @github-actions

    Claimed by @vybe. Label status-in-progress added.

    Next steps:

    1. Create branch: feature/542-<short-slug>
    2. Implement the feature
    3. Open a PR with Fixes #542
  4. vybe commented on Apr 27, 2026

    @vybe
    ContributorAuthor

    Fixed by #543 (merged).

  5. added
    status-in-dev
    and removed
    status-in-progress
    on Apr 28, 2026
  6. added a commit that references this issue on Apr 30, 2026
  7. added
    complexity-low
    theme-reliability
    on Jun 3, 2026
  8. removed
    status-in-dev
    on Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

complexity-lowpriority-p1theme-reliabilitytype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions