Skip to content

bug: process_stream_line() crashes with AttributeError when message.content is a string #585

Closed as not planned

Description

@vybe

Summary

When Claude Code emits a user message whose content field is a plain string (not an array), process_stream_line() in the agent-server crashes with AttributeError: 'str' object has no attribute 'get'. This exits the stdout reader thread early. Claude keeps running and writes output to the pipe, but nobody reads it. The final {\"type\":\"result\"} JSON is lost when the execution drains, producing a 502 "no result message" error on every affected execution.

Component

Agent Runtime — docker/base-image/agent_server/services/claude_code.py

Priority

P0 — every execution fails if the initial task prompt triggers this path (consistently reproducible)

Error

ERROR: [Headless Task] Error reading stdout: 'str' object has no attribute 'get'
ERROR: [Headless Task] Execution completed without a result message after 0 tool calls / 0 turns (raw_messages=5).

Location

  • File: docker/base-image/agent_server/services/claude_code.py
  • Lines: process_stream_line() (~line 222) and parse_stream_json_output() (~line 399)
  • Pattern: Both functions iterate over message.get("content", []) without an isinstance guard

Root Cause

Claude Code's stream-json format allows message.content to be either a list of content blocks or a plain string (the initial user task message uses a string). The code assumes it is always a list:

message_content = message.get("content", [])
for content_block in message_content:          # iterates over chars if content is a string
    block_type = content_block.get("type")     # AttributeError: 'str' has no attribute 'get'

The process_stream_line() call is made outside the inner except json.JSONDecodeError block, so the AttributeError propagates to the outer except Exception handler in read_stdout(), which logs the error and returns — silently exiting the reader thread.

Reproduction Steps

  1. Create an agent and trigger any execution where the task prompt is a direct string (standard scheduled/manual tasks)
  2. The first {\"type\":\"user\"} stream line from Claude Code will have content as a string
  3. process_stream_line() crashes immediately
  4. Execution is recorded as 502 / "no result message"

Suggested Fix

Add an isinstance guard before iterating content in both process_stream_line() and parse_stream_json_output():

message_content = message.get("content", [])
if not isinstance(message_content, list):
    message_content = []
for content_block in message_content:
    block_type = content_block.get("type")
    # ...

This matches the Claude API spec where content can be a string or array.

Environment

  • Trinity version: f199383
  • Affected: docker/base-image/agent_server/services/claude_code.py

Related

  • Same pattern exists at two locations in the same file (process_stream_line and parse_stream_json_output)
  • The outer reader function read_stdout() silently swallows the exception and exits, making this failure mode difficult to diagnose without checking logs

Activity

  1. vybe commented on Apr 29, 2026

    @vybe
    ContributorAuthor

    Duplicate of fix already merged in #542/#543 (commit 415017f, Apr 28). The isinstance(content_block, dict) guard is in the current codebase at both parse_stream_json_output (line 225) and process_stream_line (line 404). The symptom persists only because the base image hasn't been rebuilt since f199383. Closing — action needed is an image rebuild, not a code fix.

  2. added
    complexity-low
    on Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-lowpriority-p0type-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions