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
11 changes: 10 additions & 1 deletion internal/execution/bifrost/converted.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,19 @@ func buildConvertedResponsesRequest(spec execution.AttemptSpec, provider schemas
if request.Params == nil {
request.Params = &schemas.ResponsesParameters{}
}
stripResponsesControlParams(request.Params)
if err := mergeConvertedParameterOverrides(spec, &request.Params.ExtraParams); err != nil {
return nil, err
}
stripResponsesControlParams(request.Params)
if spec.RouteMode == execution.RouteConverted && spec.ChannelID == string(channel.OpenRouter) {
// Only provider preferences use extra-parameter passthrough. The SDK
// still prepares and serializes the typed request before merging them.
preferences, exists := request.Params.ExtraParams["provider"]
request.Params.ExtraParams = nil
if exists {
request.Params.ExtraParams = map[string]any{"provider": preferences}
}
}
return request, nil
}

Expand Down
3 changes: 3 additions & 0 deletions internal/execution/bifrost/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,9 @@ func (r *Runtime) newSDKContext(parent context.Context, spec execution.AttemptSp
spec.ClientProtocol == protocol.Anthropic {
bifrostContext.SetValue(schemas.BifrostContextKeyPassthroughExtraParams, true)
}
if spec.RouteMode == execution.RouteConverted && spec.ChannelID == string(channel.OpenRouter) {
bifrostContext.SetValue(schemas.BifrostContextKeyPassthroughExtraParams, true)
}
if spec.Operation == execution.OperationProbe &&
openAICompatibleBase(r.providerKind(spec)) {
bifrostContext.SetValue(schemas.BifrostContextKeyPassthroughExtraParams, true)
Expand Down
6 changes: 4 additions & 2 deletions internal/execution/bifrost/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,8 +855,10 @@ func TestSanitizeNativeChatBodyPreservesExplicitStreamOptions(t *testing.T) {
t.Parallel()

body, err := sanitizeNativeChatBody(
[]byte(`{"model":"client","messages":[],"stream_options":{"include_usage":false,"vendor_option":"keep"}}`),
"upstream",
execution.AttemptSpec{
Body: []byte(`{"model":"client","messages":[],"stream_options":{"include_usage":false,"vendor_option":"keep"}}`),
UpstreamModel: "upstream",
},
true,
)
if err != nil {
Expand Down
12 changes: 9 additions & 3 deletions internal/execution/bifrost/parameter_overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/maximhq/bifrost/core/providers/gemini"
"github.com/maximhq/bifrost/core/providers/openai"

"gpt-load/internal/channel"
"gpt-load/internal/execution"
"gpt-load/internal/protocol"
)
Expand Down Expand Up @@ -48,7 +49,8 @@ func sdkRequestFields(value reflect.Type) map[string]bool {
}

func convertedParameterOverrides(spec execution.AttemptSpec) (map[string]any, error) {
if spec.RouteMode != execution.RouteConverted || len(spec.ConfiguredParameters) == 0 {
allowProviderPreferences := spec.ChannelID == string(channel.OpenRouter)
if spec.RouteMode != execution.RouteConverted || len(spec.ConfiguredParameters) == 0 && !allowProviderPreferences {
return nil, nil
}
known := convertedRequestFields[spec.ClientProtocol]
Expand All @@ -60,9 +62,13 @@ func convertedParameterOverrides(spec execution.AttemptSpec) (map[string]any, er
return nil, fmt.Errorf("invalid overridden request body")
}
// 复用原生请求的控制字段清理,扩展参数不能接管凭据或 SDK 路由。
stripNativeControlFields(body)
stripNativeControlFields(body, allowProviderPreferences)
names := spec.ConfiguredParameters
if allowProviderPreferences {
names = append([]string{"provider"}, names...)
}
extras := make(map[string]any)
for _, name := range spec.ConfiguredParameters {
for _, name := range names {
lower := strings.ToLower(name)
if known[lower] {
continue
Expand Down
37 changes: 21 additions & 16 deletions internal/execution/bifrost/passthrough.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ func nativeMessageProvider(providerKind channel.ProviderKind, spec execution.Att
return "", false
}

func sanitizeNativeChatBody(body []byte, upstreamModel string, stream ...bool) ([]byte, error) {
object, err := decodeNativeJSONObject(body)
func sanitizeNativeChatBody(spec execution.AttemptSpec, stream ...bool) ([]byte, error) {
object, err := decodeNativeJSONObject(spec.Body)
if err != nil {
return nil, err
}
changed := stripNativeControlFields(object)
if !jsonStringEquals(object["model"], upstreamModel) {
if err := forceJSONModel(object, upstreamModel); err != nil {
changed := stripNativeControlFields(object, spec.ChannelID == string(channel.OpenRouter))
if !jsonStringEquals(object["model"], spec.UpstreamModel) {
if err := forceJSONModel(object, spec.UpstreamModel); err != nil {
return nil, err
}
changed = true
Expand All @@ -77,7 +77,7 @@ func sanitizeNativeChatBody(body []byte, upstreamModel string, stream ...bool) (
}
}
if !changed {
return append([]byte(nil), body...), nil
return append([]byte(nil), spec.Body...), nil
}
return encodeNativeJSONObject(object)
}
Expand All @@ -88,7 +88,7 @@ func sanitizeNativeRequestBody(spec execution.AttemptSpec, stream bool) ([]byte,
return nil, nil
case (spec.ClientProtocol == protocol.OpenAICompletions || spec.ClientProtocol == protocol.Anthropic) &&
spec.Operation == execution.OperationChatCompletion:
body, err := sanitizeNativeChatBody(spec.Body, spec.UpstreamModel, stream)
body, err := sanitizeNativeChatBody(spec, stream)
if err != nil {
return nil, err
}
Expand All @@ -100,15 +100,15 @@ func sanitizeNativeRequestBody(spec execution.AttemptSpec, stream bool) ([]byte,
}
return body, nil
case spec.ClientProtocol == protocol.Anthropic && spec.Operation == execution.OperationCountTokens:
return sanitizeNativeChatBody(spec.Body, spec.UpstreamModel)
return sanitizeNativeChatBody(spec)
case spec.ClientProtocol == protocol.Gemini && spec.Operation == execution.OperationChatCompletion:
object, err := decodeNativeJSONObject(spec.Body)
if err != nil {
// Preserve malformed/opaque provider payloads for the upstream to
// validate, while valid JSON is scrubbed below.
return append([]byte(nil), spec.Body...), nil
}
changed := stripNativeControlFields(object)
changed := stripNativeControlFields(object, false)
if !changed {
return append([]byte(nil), spec.Body...), nil
}
Expand All @@ -118,7 +118,7 @@ func sanitizeNativeRequestBody(spec execution.AttemptSpec, stream bool) ([]byte,
if err != nil {
return append([]byte(nil), spec.Body...), nil
}
if !stripNativeControlFields(object) {
if !stripNativeControlFields(object, false) {
return append([]byte(nil), spec.Body...), nil
}
return encodeNativeJSONObject(object)
Expand All @@ -128,7 +128,7 @@ func sanitizeNativeRequestBody(spec execution.AttemptSpec, stream bool) ([]byte,
if err != nil {
return nil, err
}
changed := stripNativeControlFields(object)
changed := stripNativeControlFields(object, spec.ChannelID == string(channel.OpenRouter))
if !jsonStringEquals(object["model"], spec.UpstreamModel) {
if err := forceJSONModel(object, spec.UpstreamModel); err != nil {
return nil, err
Expand All @@ -150,7 +150,7 @@ func sanitizeNativeRequestBody(spec execution.AttemptSpec, stream bool) ([]byte,
if err != nil {
return nil, err
}
changed := stripNativeControlFields(object)
changed := stripNativeControlFields(object, false)
if !jsonStringEquals(object["model"], spec.UpstreamModel) {
if err := forceJSONModel(object, spec.UpstreamModel); err != nil {
return nil, err
Expand All @@ -173,7 +173,7 @@ func sanitizeNativeRequestBody(spec execution.AttemptSpec, stream bool) ([]byte,
if err != nil {
return nil, err
}
changed := stripNativeControlFields(object)
changed := stripNativeControlFields(object, false)
if _, hasModel := object["model"]; hasModel && strings.TrimSpace(spec.UpstreamModel) != "" {
if !jsonStringEquals(object["model"], spec.UpstreamModel) {
if err := forceJSONModel(object, spec.UpstreamModel); err != nil {
Expand All @@ -194,7 +194,7 @@ func sanitizeNativeRequestBody(spec execution.AttemptSpec, stream bool) ([]byte,
if err != nil {
return nil, err
}
stripNativeControlFields(object)
stripNativeControlFields(object, false)
return encodeNativeJSONObject(object)
}
}
Expand Down Expand Up @@ -252,9 +252,14 @@ func decodeNativeJSONObject(body []byte) (map[string]json.RawMessage, error) {
return object, nil
}

func stripNativeControlFields(object map[string]json.RawMessage) bool {
func stripNativeControlFields(object map[string]json.RawMessage, allowProviderPreferences bool) bool {
changed := false
for key := range object {
for key, value := range object {
// OpenRouter's native provider object selects its upstream providers.
// Scalar gateway controls and case variants remain filtered.
if allowProviderPreferences && key == "provider" && bytes.HasPrefix(bytes.TrimSpace(value), []byte("{")) {
continue
}
normalized := strings.ReplaceAll(strings.ToLower(key), "-", "_")
switch normalized {
case "provider", "fallback", "fallbacks", "authorization", "proxy_authorization",
Expand Down
Loading