Skip to content

Whitespace content causes API validation errors in provider transformations #8903

@jerome-benoit

Description

@jerome-benoit

Problem

Provider APIs (Anthropic, OpenAI) reject requests containing empty or whitespace-only text content, causing validation failures during message transformation.

Current Behavior:

  • Untrimmed whitespace in message content passes through transformation functions
  • Empty strings after whitespace removal are sent to provider APIs
  • APIs respond with validation errors rejecting empty content blocks

Root Cause:
No consistent whitespace trimming or empty content filtering across provider transformation functions (anthropic.ts, openai.ts, openai-compatible.ts, transform.ts).

Impact

  • User messages with accidental whitespace cause request failures
  • Poor UX when legitimate content is rejected due to formatting
  • Inconsistent behavior across different providers

Proposed Solution

Implement consistent whitespace trimming pattern across all transformations:

const t = content.trim()
if (t.length > 0) {
  parts.push({ type: "text", text: t })
}

Scope:

  • Apply trimming to all text content before API submission
  • Filter empty/whitespace-only messages and parts
  • Preserve tool results and streaming deltas unchanged
  • Add comprehensive test coverage

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions