Skip to content

Fix: Handle array-style content blocks in messagesToPrompt#19

Open
superdav42 wants to merge 1 commit intoatalovesyou:mainfrom
superdav42:fix/handle-array-content-blocks
Open

Fix: Handle array-style content blocks in messagesToPrompt#19
superdav42 wants to merge 1 commit intoatalovesyou:mainfrom
superdav42:fix/handle-array-content-blocks

Conversation

@superdav42
Copy link

Summary

  • OpenAI API supports both string and array content formats for message content. Some clients (e.g. OpenClaw, multi-modal clients) send content as an array of typed blocks: [{"type": "text", "text": "Hello"}]
  • When content is an array, JavaScript's template literal interpolation converts it to [object Object] instead of the actual text, causing garbled prompts sent to Claude CLI
  • Adds an extractContent() helper that handles both formats: returns strings as-is, extracts and joins text from array-style content blocks, and falls back to String() for any unexpected type
  • Updates all three msg.content references in messagesToPrompt (system, user, and assistant cases) to use extractContent(msg.content)

Details

The OpenAI chat completions API defines content as either a string or an array of content parts:

// String format (already handled)
{ "role": "user", "content": "Hello" }

// Array format (was broken, now fixed)
{ "role": "user", "content": [{"type": "text", "text": "Hello"}] }

The previous code used msg.content directly in template literals and parts.push(), which works fine for strings but produces [object Object] for arrays.

The new extractContent() function:

function extractContent(content: string | Array<{type: string; text?: string}>): string {
  if (typeof content === "string") return content;
  if (Array.isArray(content)) {
    return content
      .filter((block) => block.type === "text")
      .map((block) => block.text ?? "")
      .join("\n");
  }
  return String(content ?? "");
}

Test plan

  • Verify string content still works as before (no regression)
  • Verify array content [{"type": "text", "text": "..."}] is correctly extracted
  • Verify mixed content arrays with non-text blocks are filtered properly
  • Verify empty/null content is handled gracefully

🤖 Generated with Claude Code

OpenAI API supports both string and array content formats for messages.
When content is an array of objects (e.g. [{"type": "text", "text": "..."}]),
JavaScript's template literal interpolation converts it to "[object Object]"
instead of the actual text content.

This adds an extractContent() helper that:
- Returns string content as-is
- Extracts and joins text from array-style content blocks
- Falls back to String() for any other type

All msg.content references in messagesToPrompt are updated to use
extractContent(msg.content) for the system, user, and assistant cases.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant