Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
45 changes: 45 additions & 0 deletions core/providers/anthropic/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -4650,6 +4650,21 @@ func convertAnthropicContentBlocksToResponsesMessagesGrouped(contentBlocks []Ant
bifrostMessages = append(bifrostMessages, bifrostMsg)
}

case AnthropicContentBlockTypeContainerUpload:
if block.FileID != nil {
bifrostMsg := schemas.ResponsesMessage{
Type: schemas.Ptr(schemas.ResponsesMessageTypeMessage),
Role: role,
Content: &schemas.ResponsesMessageContent{
ContentBlocks: []schemas.ResponsesMessageContentBlock{block.toBifrostResponsesContainerUploadBlock()},
},
}
if isOutputMessage {
bifrostMsg.ID = schemas.Ptr("msg_" + providerUtils.GetRandomString(50))
}
bifrostMessages = append(bifrostMessages, bifrostMsg)
}

case AnthropicContentBlockTypeThinking:
if block.Thinking != nil {
bifrostMsg := schemas.ResponsesMessage{
Expand Down Expand Up @@ -4965,6 +4980,20 @@ func convertAnthropicContentBlocksToResponsesMessages(ctx *schemas.BifrostContex
}
bifrostMessages = append(bifrostMessages, bifrostMsg)
}
case AnthropicContentBlockTypeContainerUpload:
if block.FileID != nil {
bifrostMsg := schemas.ResponsesMessage{
Type: schemas.Ptr(schemas.ResponsesMessageTypeMessage),
Role: role,
Content: &schemas.ResponsesMessageContent{
ContentBlocks: []schemas.ResponsesMessageContentBlock{block.toBifrostResponsesContainerUploadBlock()},
},
}
if isOutputMessage {
bifrostMsg.ID = schemas.Ptr("msg_" + providerUtils.GetRandomString(50))
}
bifrostMessages = append(bifrostMessages, bifrostMsg)
}
case AnthropicContentBlockTypeThinking:
if block.Thinking != nil {
// Collect reasoning blocks to create a single reasoning message
Expand Down Expand Up @@ -7124,6 +7153,14 @@ func convertContentBlockToAnthropic(block schemas.ResponsesMessageContentBlock)
)
return &anthropicBlock
}
case schemas.ResponsesInputMessageContentBlockTypeContainer:
if block.FileID != nil {
return &AnthropicContentBlock{
Type: AnthropicContentBlockTypeContainerUpload,
FileID: block.FileID,
CacheControl: block.CacheControl,
}
}
case schemas.ResponsesOutputMessageContentTypeReasoning:
if block.Text != nil {
return &AnthropicContentBlock{
Expand Down Expand Up @@ -7163,6 +7200,14 @@ func (block AnthropicContentBlock) toBifrostResponsesImageBlock() schemas.Respon
}
}

func (block AnthropicContentBlock) toBifrostResponsesContainerUploadBlock() schemas.ResponsesMessageContentBlock {
return schemas.ResponsesMessageContentBlock{
Type: schemas.ResponsesInputMessageContentBlockTypeContainer,
FileID: block.FileID,
CacheControl: block.CacheControl,
}
}

func (block AnthropicContentBlock) toBifrostResponsesDocumentBlock() schemas.ResponsesMessageContentBlock {
resultBlock := schemas.ResponsesMessageContentBlock{
Type: schemas.ResponsesInputMessageContentBlockTypeFile,
Expand Down
79 changes: 79 additions & 0 deletions core/providers/anthropic/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,82 @@ func TestRoundTrip_ContentBlocks_Bedrock(t *testing.T) {
}
}
}

// --- container_upload -------------------------------------------------------

// findContainerUploadBlocks collects every container_upload block across a
// message slice.
func findContainerUploadBlocks(msgs []AnthropicMessage) []AnthropicContentBlock {
var out []AnthropicContentBlock
for _, m := range msgs {
for _, b := range m.Content.ContentBlocks {
if b.Type == AnthropicContentBlockTypeContainerUpload {
out = append(out, b)
}
}
}
return out
}

// container_upload (file staged into the code-execution container) must survive
// Anthropic → Bifrost → Anthropic, carrying file_id and cache_control.
func TestRoundTrip_ContainerUpload(t *testing.T) {
fileID := "file_011CcpBQA2BV1gthmNPSYzkh"
messages := []AnthropicMessage{
{
Role: AnthropicMessageRoleUser,
Content: AnthropicContent{
ContentBlocks: []AnthropicContentBlock{
{Type: AnthropicContentBlockTypeText, Text: schemas.Ptr("analyse and share model names")},
{
Type: AnthropicContentBlockTypeContainerUpload,
FileID: &fileID,
CacheControl: &schemas.CacheControl{Type: schemas.CacheControlTypeEphemeral},
},
},
},
},
}

outMsgs, _ := roundTrip(t, messages, nil, schemas.Anthropic, "claude-sonnet-4-6")

blocks := findContainerUploadBlocks(outMsgs)
if len(blocks) != 1 {
t.Fatalf("container_upload blocks = %d, want 1", len(blocks))
}
if blocks[0].FileID == nil || *blocks[0].FileID != fileID {
t.Errorf("file_id = %v, want %q", blocks[0].FileID, fileID)
}
if blocks[0].CacheControl == nil || blocks[0].CacheControl.Type != schemas.CacheControlTypeEphemeral {
t.Errorf("cache_control = %v, want ephemeral", blocks[0].CacheControl)
}
}

// The grouped converter (used for Bedrock-routed Anthropic models) must also
// emit a neutral container_upload block rather than dropping it.
func TestRoundTrip_ContainerUpload_Grouped(t *testing.T) {
fileID := "file_grouped_123"
role := schemas.ResponsesInputMessageRoleUser
msgs := convertAnthropicContentBlocksToResponsesMessagesGrouped(
[]AnthropicContentBlock{{Type: AnthropicContentBlockTypeContainerUpload, FileID: &fileID}},
&role, false,
)

var found *schemas.ResponsesMessageContentBlock
for i := range msgs {
if msgs[i].Content == nil {
continue
}
for j := range msgs[i].Content.ContentBlocks {
if msgs[i].Content.ContentBlocks[j].Type == schemas.ResponsesInputMessageContentBlockTypeContainer {
found = &msgs[i].Content.ContentBlocks[j]
}
}
}
if found == nil {
t.Fatalf("grouped converter dropped container_upload block")
}
if found.FileID == nil || *found.FileID != fileID {
t.Errorf("file_id = %v, want %q", found.FileID, fileID)
}
}
44 changes: 35 additions & 9 deletions core/providers/vertex/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ func resolveVertexRegion(ctx *schemas.BifrostContext, key schemas.Key) string {
return ""
}

// resolveVertexForceSingleRegion reports whether Bifrost must use the configured
// region as-is and skip promoting multi-region-only models to a multi-region pool
// endpoint. Priority: per-alias VertexAliasCfg.ForceSingleRegion (when set) >
// key-level VertexKeyConfig.ForceSingleRegion. Used by provisioned-throughput
// customers who can serve multi-region-only models (e.g. Opus 4.7/4.8) from a
// single region.
func resolveVertexForceSingleRegion(ctx *schemas.BifrostContext, key schemas.Key) bool {
if ra := schemas.GetResolvedAlias(ctx); ra != nil && ra.Config != nil && ra.Config.VertexAliasCfg != nil && ra.Config.VertexAliasCfg.ForceSingleRegion != nil {
return *ra.Config.VertexAliasCfg.ForceSingleRegion
}
return key.VertexKeyConfig != nil && key.VertexKeyConfig.ForceSingleRegion
}

// isVertexMultiRegionEndpoint reports whether the Vertex location uses Google's
// partner-model multi-region pool endpoint host instead of the single-region host.
func isVertexMultiRegionEndpoint(region string) bool {
Expand Down Expand Up @@ -88,7 +101,10 @@ func getVertexAPIHost(region string) string {
// the corresponding multi-region pool endpoint — but only for US (us-*) and
// Europe (europe-*) regions that have multi-region pools. Other regions
// (asia-*, me-*, etc.) stay on the single-region host.
func getVertexModelAwareAPIHost(region string, model string) string {
//
// When forceSingleRegion is set the promotion is skipped and the configured
// single-region host is used as-is (e.g. provisioned-throughput deployments).
func getVertexModelAwareAPIHost(region string, model string, forceSingleRegion bool, logger schemas.Logger) string {
if region == "global" {
return "aiplatform.googleapis.com"
}
Expand All @@ -99,7 +115,16 @@ func getVertexModelAwareAPIHost(region string, model string) string {
// Single-region: promote to multi-region pool if the model requires it
// and the region belongs to a pool that supports multi-region.
if providerUtils.IsVertexMultiRegionOnlyModel(model) {
if forceSingleRegion {
if logger != nil {
logger.Debug("[vertex] force_single_region set: keeping requested region %q for multi-region-only model %q; skipping multi-region pool promotion", region, model)
}
return fmt.Sprintf("%s-aiplatform.googleapis.com", region)
}
if pool, ok := vertexRegionToPool(region); ok {
if logger != nil {
logger.Debug("[vertex] promoting multi-region-only model %q from region %q to multi-region pool %q (host aiplatform.%s.rep.googleapis.com)", model, region, pool, pool)
}
return fmt.Sprintf("aiplatform.%s.rep.googleapis.com", pool)
}
}
Expand Down Expand Up @@ -134,8 +159,8 @@ func getVertexAPIBaseURL(region string, apiVersion string) string {

// getVertexModelAwareAPIBaseURL is like getVertexAPIBaseURL but uses model-aware
// host selection for multi-region endpoints.
func getVertexModelAwareAPIBaseURL(region string, apiVersion string, model string) string {
return fmt.Sprintf("https://%s/%s", getVertexModelAwareAPIHost(region, model), apiVersion)
func getVertexModelAwareAPIBaseURL(region string, apiVersion string, model string, forceSingleRegion bool, logger schemas.Logger) string {
return fmt.Sprintf("https://%s/%s", getVertexModelAwareAPIHost(region, model, forceSingleRegion, logger), apiVersion)
}

func getVertexProjectLocationURL(region string, apiVersion string, projectID string) string {
Expand All @@ -151,21 +176,22 @@ func getVertexPublisherModelURL(region string, apiVersion string, projectID stri
// inference endpoints that may need multi-region pool hosts.
// When a single-region is promoted to multi-region, both the host AND the
// locations/ path segment are updated to the pool region.
func getVertexModelAwarePublisherModelURL(region string, apiVersion string, projectID string, publisher string, model string, method string) string {
effectiveRegion := getVertexEffectiveRegion(region, model)
baseURL := fmt.Sprintf("https://%s/%s", getVertexModelAwareAPIHost(region, model), apiVersion)
func getVertexModelAwarePublisherModelURL(region string, apiVersion string, projectID string, publisher string, model string, method string, forceSingleRegion bool, logger schemas.Logger) string {
effectiveRegion := getVertexEffectiveRegion(region, model, forceSingleRegion)
baseURL := fmt.Sprintf("https://%s/%s", getVertexModelAwareAPIHost(region, model, forceSingleRegion, logger), apiVersion)
return fmt.Sprintf("%s/projects/%s/locations/%s/publishers/%s/models/%s%s", baseURL, projectID, effectiveRegion, publisher, model, method)
}

// getVertexEffectiveRegion returns the region to use in URL path segments.
// For multi-region locations it returns the region as-is. For single-region
// locations it returns the multi-region pool if the model is flagged, otherwise
// the original region.
func getVertexEffectiveRegion(region string, model string) string {
// the original region. When forceSingleRegion is set the region is returned
// as-is (no pool promotion).
func getVertexEffectiveRegion(region string, model string, forceSingleRegion bool) string {
if isVertexMultiRegionEndpoint(region) || region == "global" {
return region
}
if providerUtils.IsVertexMultiRegionOnlyModel(model) {
if !forceSingleRegion && providerUtils.IsVertexMultiRegionOnlyModel(model) {
if pool, ok := vertexRegionToPool(region); ok {
return pool
}
Expand Down
106 changes: 96 additions & 10 deletions core/providers/vertex/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,11 @@ func TestGetVertexModelAwareAPIHost(t *testing.T) {
})

tests := []struct {
name string
region string
model string
expected string
name string
region string
model string
forceSingleRegion bool
expected string
}{
{
name: "global endpoint ignores model flag",
Expand Down Expand Up @@ -275,13 +276,27 @@ func TestGetVertexModelAwareAPIHost(t *testing.T) {
model: "claude-sonnet-4-5",
expected: "us-central1-aiplatform.googleapis.com",
},
{
name: "force single region skips us pool promotion for flagged model",
region: "us-east5",
model: "claude-opus-4-7",
forceSingleRegion: true,
expected: "us-east5-aiplatform.googleapis.com",
},
{
name: "force single region skips eu pool promotion for flagged model",
region: "europe-west1",
model: "claude-opus-4-7",
forceSingleRegion: true,
expected: "europe-west1-aiplatform.googleapis.com",
},
}

for _, tt := range tests {
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

actual := getVertexModelAwareAPIHost(tt.region, tt.model)
actual := getVertexModelAwareAPIHost(tt.region, tt.model, tt.forceSingleRegion, nil)
if actual != tt.expected {
t.Fatalf("expected %q, got %q", tt.expected, actual)
}
Expand All @@ -299,10 +314,11 @@ func TestGetVertexModelAwarePublisherModelURL(t *testing.T) {
})

tests := []struct {
name string
region string
model string
expected string
name string
region string
model string
forceSingleRegion bool
expected string
}{
{
name: "us multi-region flagged model gets rep host URL",
Expand Down Expand Up @@ -340,13 +356,20 @@ func TestGetVertexModelAwarePublisherModelURL(t *testing.T) {
model: "claude-3-5-sonnet",
expected: "https://us-central1-aiplatform.googleapis.com/v1/projects/project-123/locations/us-central1/publishers/anthropic/models/claude-3-5-sonnet:rawPredict",
},
{
name: "force single region keeps flagged model on requested region host and path",
region: "us-east5",
model: "claude-opus-4-7",
forceSingleRegion: true,
expected: "https://us-east5-aiplatform.googleapis.com/v1/projects/project-123/locations/us-east5/publishers/anthropic/models/claude-opus-4-7:rawPredict",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

actual := getVertexModelAwarePublisherModelURL(tt.region, "v1", "project-123", "anthropic", tt.model, ":rawPredict")
actual := getVertexModelAwarePublisherModelURL(tt.region, "v1", "project-123", "anthropic", tt.model, ":rawPredict", tt.forceSingleRegion, nil)
if actual != tt.expected {
t.Fatalf("expected %q, got %q", tt.expected, actual)
}
Expand Down Expand Up @@ -530,6 +553,69 @@ func TestResolveVertexProjectNumber_AliasOverride(t *testing.T) {
}
}

// TestResolveVertexForceSingleRegion_AliasOverride verifies the per-alias
// VertexAliasCfg.ForceSingleRegion (*bool) override: an explicit alias-level
// value wins over the key-level bool — including an alias false overriding a key
// true — while a nil alias value falls through to the key-level bool.
func TestResolveVertexForceSingleRegion_AliasOverride(t *testing.T) {
keyForceTrue := schemas.Key{
VertexKeyConfig: &schemas.VertexKeyConfig{ForceSingleRegion: true},
}
keyForceFalse := schemas.Key{
VertexKeyConfig: &schemas.VertexKeyConfig{ForceSingleRegion: false},
}

// nil ctx uses the key-level value.
if got := resolveVertexForceSingleRegion(nil, keyForceTrue); !got {
t.Errorf("nil ctx: got %v, want key-level true", got)
}

// empty ctx (no resolved alias) uses the key-level value.
ctx0 := schemas.NewBifrostContext(nil, schemas.NoDeadline)
if got := resolveVertexForceSingleRegion(ctx0, keyForceFalse); got {
t.Errorf("empty ctx: got %v, want key-level false", got)
}

// Alias-level true overrides key-level false.
ctxTrue := schemas.NewBifrostContext(nil, schemas.NoDeadline)
ctxTrue.SetValue(schemas.BifrostContextKeyResolvedAlias, &schemas.ResolvedAlias{
Key: "best-claude",
Config: &schemas.AliasConfig{
ModelID: "claude-opus-4-7",
VertexAliasCfg: &schemas.VertexAliasCfg{ForceSingleRegion: schemas.Ptr(true)},
},
})
if got := resolveVertexForceSingleRegion(ctxTrue, keyForceFalse); !got {
t.Errorf("alias true should win over key false: got %v, want true", got)
}

// Alias-level false overrides key-level true (explicit set beats nil).
ctxFalse := schemas.NewBifrostContext(nil, schemas.NoDeadline)
ctxFalse.SetValue(schemas.BifrostContextKeyResolvedAlias, &schemas.ResolvedAlias{
Key: "best-claude",
Config: &schemas.AliasConfig{
ModelID: "claude-opus-4-7",
VertexAliasCfg: &schemas.VertexAliasCfg{ForceSingleRegion: schemas.Ptr(false)},
},
})
if got := resolveVertexForceSingleRegion(ctxFalse, keyForceTrue); got {
t.Errorf("alias false should win over key true: got %v, want false", got)
}

// Alias present but ForceSingleRegion nil falls through to the key-level value.
ctxNil := schemas.NewBifrostContext(nil, schemas.NoDeadline)
ctxNil.SetValue(schemas.BifrostContextKeyResolvedAlias, &schemas.ResolvedAlias{
Key: "best-claude",
Config: &schemas.AliasConfig{
ModelID: "claude-opus-4-7",
VertexAliasCfg: &schemas.VertexAliasCfg{ForceSingleRegion: nil},
},
})
if got := resolveVertexForceSingleRegion(ctxNil, keyForceTrue); !got {
t.Errorf("nil alias value should fall through to key true: got %v, want true", got)
}
}

// TestGeminiImageURLSchemesContract pins the exact allowlist that Vertex passes
// into the Gemini converter. vertex.go reuses geminiImageURLSchemes across
// ChatCompletion / ChatCompletionStream / Responses / ResponsesStream / CountTokens,
Expand Down
Loading
Loading