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
35 changes: 35 additions & 0 deletions core/providers/anthropic/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,41 @@ func ToAnthropicChatRequest(ctx *schemas.BifrostContext, bifrostReq *schemas.Bif
}
}

// Fallbacks — Anthropic native server-side fallback objects arrive via
// ExtraParams["fallbacks"]. Promote them onto the typed Fallbacks field so
// they marshal natively and drive server-side-fallback beta-header injection
// (mirrors ToAnthropicResponsesRequest); Bifrost string fallbacks are not
// carried here (they travel on BifrostChatRequest.Fallbacks).
if fbVal, exists := bifrostReq.Params.ExtraParams["fallbacks"]; exists {
var natives []AnthropicNativeFallback
switch v := fbVal.(type) {
case []AnthropicNativeFallback:
natives = v
default:
if data, err := providerUtils.MarshalSorted(v); err == nil {
_ = sonic.Unmarshal(data, &natives)
}
}
if len(natives) > 0 {
delete(anthropicReq.ExtraParams, "fallbacks")
entries := make([]AnthropicFallbackEntry, len(natives))
for i := range natives {
n := natives[i]
entries[i] = AnthropicFallbackEntry{Native: &n}
}
anthropicReq.Fallbacks = entries
}
}

// Fallback credit token — same promotion, so the retry marshals the token
// top-level and picks up the fallback-credit beta header.
if tokenVal, exists := bifrostReq.Params.ExtraParams["fallback_credit_token"]; exists {
if token, ok := tokenVal.(string); ok && token != "" {
delete(anthropicReq.ExtraParams, "fallback_credit_token")
anthropicReq.FallbackCreditToken = &token
}
}

// TaskBudget — maps onto output_config.task_budget. If an OutputConfig
// already exists (e.g. from structured outputs), attach the budget to
// it; otherwise create one.
Expand Down
16 changes: 14 additions & 2 deletions core/providers/anthropic/requestbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,21 @@ func BuildAnthropicResponsesRequestBody(ctx *schemas.BifrostContext, request *sc
return nil, newErr(schemas.ErrProviderRequestMarshal, err, jsonBody)
}

jsonBody, err = providerUtils.DeleteJSONField(jsonBody, "fallbacks")
// Strip Bifrost cross-provider fallback strings, but preserve Anthropic
// native server-side fallback objects (server-side-fallback-2026-06-01).
jsonBody, err = stripBifrostFallbacksFromBody(jsonBody, cfg.Provider)
if err != nil {
return nil, newErr(schemas.ErrProviderRequestMarshal, err, jsonBody)
}

if cfg.IsCountTokens {
// The count_tokens endpoint rejects fallback_credit_token outright.
jsonBody, err = providerUtils.DeleteJSONField(jsonBody, "fallback_credit_token")
if err != nil {
return nil, newErr(schemas.ErrProviderRequestMarshal, err, jsonBody)
}
}

if defaults.DeleteStreamField {
jsonBody, err = providerUtils.DeleteJSONField(jsonBody, "stream")
if err != nil {
Expand Down Expand Up @@ -597,7 +607,9 @@ func BuildAnthropicChatRequestBody(ctx *schemas.BifrostContext, request *schemas
return nil, newErr(schemas.ErrProviderRequestMarshal, err, jsonBody)
}

jsonBody, err = providerUtils.DeleteJSONField(jsonBody, "fallbacks")
// Strip Bifrost cross-provider fallback strings, but preserve Anthropic
// native server-side fallback objects (server-side-fallback-2026-06-01).
jsonBody, err = stripBifrostFallbacksFromBody(jsonBody, cfg.Provider)
if err != nil {
return nil, newErr(schemas.ErrProviderRequestMarshal, err, jsonBody)
}
Expand Down
Loading
Loading