[PR #7086] fix(provider): trim whitespace and filter empty content in transformations #12238

Open
opened 2026-02-16 18:17:09 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/7086

State: open
Merged: No


Summary

Implement consistent whitespace trimming across all provider transformation functions to prevent API validation errors from empty or whitespace-only message content.

Changes

  • Trim all text content before length checks and assignments across all provider transformations
  • Filter out whitespace-only messages and parts to prevent empty content from reaching APIs
  • Two-phase filter+map approach in normalizeMessages for consistent handling
  • Preserve tool results and streaming deltas without modification (whitespace may be significant)
  • Add 65 comprehensive tests for whitespace handling scenarios

Affected Files

File Changes Description
anthropic.ts 44 changes Trim content in all Anthropic transformations
openai.ts 42 changes Trim content in all OpenAI transformations
openai-compatible.ts 32 changes Trim content in OpenAI-compatible transformations
transform.ts 31 changes Update normalizeMessages with two-phase filtering
transform.test.ts 91 additions Comprehensive test coverage

Total: 5 files changed, 184 insertions(+), 56 deletions(-)

Why This Change?

APIs like Anthropic and OpenAI reject requests with empty or whitespace-only text content, causing validation errors. This change ensures all text content is trimmed and empty parts are filtered before being sent to providers.

Testing

All 65 tests pass, covering:

  • Whitespace-only content filtering
  • Leading/trailing whitespace trimming
  • Empty array handling
  • Tool result preservation
  • Mixed content scenarios
  • Streaming delta preservation

Pattern Applied

// Before
if (content.length > 0) {
  parts.push({ type: "text", text: content })
}

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

Closes #8903
Closes #8679

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/7086 **State:** open **Merged:** No --- ## Summary Implement consistent whitespace trimming across all provider transformation functions to prevent API validation errors from empty or whitespace-only message content. ## Changes - **Trim all text content** before length checks and assignments across all provider transformations - **Filter out whitespace-only messages and parts** to prevent empty content from reaching APIs - **Two-phase filter+map approach** in `normalizeMessages` for consistent handling - **Preserve tool results and streaming deltas** without modification (whitespace may be significant) - **Add 65 comprehensive tests** for whitespace handling scenarios ## Affected Files | File | Changes | Description | |------|---------|-------------| | `anthropic.ts` | 44 changes | Trim content in all Anthropic transformations | | `openai.ts` | 42 changes | Trim content in all OpenAI transformations | | `openai-compatible.ts` | 32 changes | Trim content in OpenAI-compatible transformations | | `transform.ts` | 31 changes | Update `normalizeMessages` with two-phase filtering | | `transform.test.ts` | 91 additions | Comprehensive test coverage | **Total**: 5 files changed, 184 insertions(+), 56 deletions(-) ## Why This Change? APIs like Anthropic and OpenAI reject requests with empty or whitespace-only text content, causing validation errors. This change ensures all text content is trimmed and empty parts are filtered before being sent to providers. ## Testing ✅ All 65 tests pass, covering: - Whitespace-only content filtering - Leading/trailing whitespace trimming - Empty array handling - Tool result preservation - Mixed content scenarios - Streaming delta preservation ## Pattern Applied ```typescript // Before if (content.length > 0) { parts.push({ type: "text", text: content }) } // After const t = content.trim() if (t.length > 0) { parts.push({ type: "text", text: t }) } ``` Closes #8903 Closes #8679
yindo added the pull-request label 2026-02-16 18:17:09 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12238