Skip to content

pi-agent-core: crash when calling .filter() on message.content / assistantMessage.content when content is not an array #2538

@jepson-liu

Description

@jepson-liu

What happened?

In @mariozechner/pi-agent-core’s agent-loop, two call sites invoke .filter() directly on message.content and assistantMessage.content without checking that the value is an array. When a sub-agent (or another code path) yields a message whose content is a string or other non-array value, Node throws:

TypeError: xxx.content.filter is not a function

The assistant message ends up with stopReason: "error" and the agent loop stops. This was reproduced upstream in OpenClaw when using sessions_spawn with runtime: "subagent"; see openclaw/openclaw#51390.

Corresponding source in this repo:

  • Main loop: message.content.filter(...) in packages/agent/src/agent-loop.ts (around line 201)
  • executeToolCalls: assistantMessage.content.filter(...) (around line 343)

Other parts of the same file already iterate content defensively; these sites should follow the same pattern (e.g. (Array.isArray(content) ? content : []).filter(...)).

Steps to reproduce

  1. Run an agent through a path that can produce a message with non-array content (as in upstream: parent uses sessions_spawn, child runtime: "subagent").
  2. Ensure the child returns a message where content is a string (or otherwise not an array).
  3. When the parent agent-loop processes that message, execution hits one of the .filter() sites and throws.

Expected behavior

The agent loop must not crash with .filter is not a function when content is not an array. Non-array content should be treated as “no structured content blocks” or normalized according to the type contract before resolving tool calls.

Version

0.60.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpossibly-openclaw-clankerUser has activity on openclaw/openclaw

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions