fix(provider): trim whitespace and filter empty content in transformations#7086
fix(provider): trim whitespace and filter empty content in transformations#7086jerome-benoit wants to merge 21 commits intoanomalyco:devfrom
Conversation
…tions Implement consistent whitespace trimming across all provider transformation functions (Anthropic, OpenAI, OpenAI-compatible) to prevent API validation errors from empty or whitespace-only message content. - Trim all text content before length checks and assignments - Filter out whitespace-only messages and parts - Preserve tool results and streaming deltas without modification - Add 65 comprehensive tests for whitespace handling 5 files changed: 184 insertions(+), 56 deletions(-)
|
The following comment was made by an LLM, it may be inaccurate: No duplicate PRs found |
There was a problem hiding this comment.
Pull request overview
This PR implements comprehensive whitespace handling across all provider transformation functions to prevent API validation errors from empty or whitespace-only message content. The changes ensure that all text content is trimmed before being sent to provider APIs (Anthropic, OpenAI, and OpenAI-compatible providers), while carefully preserving tool results and streaming deltas where whitespace may be semantically significant.
Key changes:
- Two-phase filter+map approach in
normalizeMessagesthat first removes whitespace-only parts, then trims remaining text - Consistent trimming of all text content in request transformations across all three provider implementations
- 65 new tests covering whitespace filtering, trimming, and edge cases
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
transform.test.ts |
Adds 65 comprehensive tests for whitespace-only content filtering, leading/trailing whitespace trimming, empty array handling, and mixed content scenarios |
transform.ts |
Updates normalizeMessages with two-phase filtering approach: filters out whitespace-only text/reasoning parts, then trims remaining text content |
openai.ts |
Adds .trim() checks and trimming to all text content in fromOpenaiRequest, toOpenaiRequest, and toOpenaiResponse; preserves tool results and streaming deltas |
openai-compatible.ts |
Applies consistent trimming pattern to fromOaCompatibleRequest, toOaCompatibleRequest, and fromOaCompatibleResponse |
anthropic.ts |
Implements trimming across fromAnthropicRequest, toAnthropicRequest, and toAnthropicResponse; preserves tool results without modification |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
… calls The message() function signature was updated to require 3 arguments (msgs, model, options) but 5 test calls were still using only 2 arguments, causing TypeScript errors. Fixes typecheck failures in CI.
00637c0 to
71e0ba2
Compare
f1ae801 to
08fa7f7
Compare
Summary
Implement consistent whitespace trimming across all provider transformation functions to prevent API validation errors from empty or whitespace-only message content.
Changes
normalizeMessagesfor consistent handlingAffected Files
anthropic.tsopenai.tsopenai-compatible.tstransform.tsnormalizeMessageswith two-phase filteringtransform.test.tsTotal: 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:
Pattern Applied
Closes #8903
Closes #8679