Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions core/providers/anthropic/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,20 @@ func ToAnthropicChatRequest(ctx *schemas.BifrostContext, bifrostReq *schemas.Bif
anthropicReq.Messages = anthropicMessages
anthropicReq.System = systemContent

// Trim trailing whitespace from the last assistant message text blocks
// ContentStr is converted to a single text ContentBlock during message conversion
// so we trim the text of that block instead.
lastMsgIndex := len(anthropicReq.Messages) - 1
if lastMsgIndex >= 0 && anthropicReq.Messages[lastMsgIndex].Role == AnthropicMessageRoleAssistant {
blocks := anthropicReq.Messages[lastMsgIndex].Content.ContentBlocks
for j := len(blocks) - 1; j >= 0; j-- {
if blocks[j].Type == AnthropicContentBlockTypeText && blocks[j].Text != nil {
anthropicReq.Messages[lastMsgIndex].Content.ContentBlocks[j].Text = schemas.Ptr(strings.TrimRight(*blocks[j].Text, " \n\r\t"))
break
}
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
}

// Strip request- and tool-level fields the target Anthropic-family
// provider does not support. Fail-closed tool validation stays in
// ValidateToolsForProvider; this is strip-silently for additive fields.
Expand Down
16 changes: 15 additions & 1 deletion core/providers/anthropic/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -3344,6 +3344,20 @@ func ConvertBifrostMessagesToAnthropicMessages(ctx *schemas.BifrostContext, bifr
// Flush any remaining pending tool calls (with tracking)
flushPendingToolCallsWithTracking()

// Trim trailing whitespace from the last assistant message
// ContentStr is converted to a single text ContentBlock during message conversion
// so we trim the text of that block instead.
lastMsgIndex := len(anthropicMessages) - 1
if isRequestMessage && lastMsgIndex >= 0 && anthropicMessages[lastMsgIndex].Role == AnthropicMessageRoleAssistant {
blocks := anthropicMessages[lastMsgIndex].Content.ContentBlocks
for j := len(blocks) - 1; j >= 0; j-- {
if blocks[j].Type == AnthropicContentBlockTypeText && blocks[j].Text != nil {
anthropicMessages[lastMsgIndex].Content.ContentBlocks[j].Text = schemas.Ptr(strings.TrimRight(*blocks[j].Text, " \n\r\t"))
break
}
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
}

return anthropicMessages, systemContent
}

Expand Down Expand Up @@ -5957,4 +5971,4 @@ func generateSyntheticInputJSONDeltas(argumentsJSON string, contentIndex *int) [
}

return events
}
}
14 changes: 14 additions & 0 deletions core/providers/bedrock/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bedrock
import (
"context"
"fmt"
"strings"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -42,6 +43,19 @@ func ToBedrockChatCompletionRequest(ctx *schemas.BifrostContext, bifrostReq *sch
bedrockReq.System = systemMessages
}

// Trim trailing whitespace from the last assistant message text blocks
// (only for Anthropic models which use text-based prefill)
lastMsgIndex := len(bedrockReq.Messages) - 1
if schemas.IsAnthropicModel(bifrostReq.Model) && lastMsgIndex >= 0 && bedrockReq.Messages[lastMsgIndex].Role == BedrockMessageRoleAssistant {
blocks := bedrockReq.Messages[lastMsgIndex].Content
for j := len(blocks) - 1; j >= 0; j-- {
if blocks[j].Text != nil {
bedrockReq.Messages[lastMsgIndex].Content[j].Text = schemas.Ptr(strings.TrimRight(*blocks[j].Text, " \n\r\t"))
break
}
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

// Convert parameters and configurations
if err := convertChatParameters(ctx, bifrostReq, bedrockReq); err != nil {
return nil, fmt.Errorf("failed to convert chat parameters: %w", err)
Expand Down
13 changes: 13 additions & 0 deletions core/providers/bedrock/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,19 @@ func ToBedrockResponsesRequest(ctx *schemas.BifrostContext, bifrostReq *schemas.
}
}
}

// Trim trailing whitespace from the last assistant message text blocks
// (only for Anthropic models which use text-based prefill)
lastMsgIndex := len(bedrockReq.Messages) - 1
if schemas.IsAnthropicModel(bifrostReq.Model) && lastMsgIndex >= 0 && bedrockReq.Messages[lastMsgIndex].Role == BedrockMessageRoleAssistant {
blocks := bedrockReq.Messages[lastMsgIndex].Content
for j := len(blocks) - 1; j >= 0; j-- {
if blocks[j].Text != nil {
bedrockReq.Messages[lastMsgIndex].Content[j].Text = schemas.Ptr(strings.TrimRight(*blocks[j].Text, " \n\r\t"))
break
}
}
}
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.

var responsesStructuredOutputTool *BedrockTool
Expand Down
Loading