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
23 changes: 23 additions & 0 deletions core/providers/openai/responses.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package openai

import (
"encoding/json"
"strings"

"github.com/maximhq/bifrost/core/providers/utils"
Expand Down Expand Up @@ -444,6 +445,28 @@ type OpenAICompactionRequest struct {
// GetExtraParams implements RequestBodyWithExtraParams.
func (r *OpenAICompactionRequest) GetExtraParams() map[string]interface{} { return r.ExtraParams }

// MarshalJSON serializes the compaction request. The embedded Input is shadowed
// by a json.RawMessage so the OpenAIResponsesRequestInput union is written via
// its own MarshalJSON (a pointer-receiver method that default struct encoding of
// the value field would skip, emitting `input` as an object the endpoint
// rejects). Empty input is omitted, since a previous_response_id-only compaction
// is valid. Mirrors OpenAIResponsesRequest.MarshalJSON.
func (r *OpenAICompactionRequest) MarshalJSON() ([]byte, error) {
type Alias OpenAICompactionRequest
var input json.RawMessage
if r.Input.OpenAIResponsesRequestInputStr != nil || r.Input.OpenAIResponsesRequestInputArray != nil {
b, err := r.Input.MarshalJSON()
if err != nil {
return nil, err
}
input = json.RawMessage(b)
}
return utils.MarshalSorted(struct {
*Alias
Input json.RawMessage `json:"input,omitempty"`
}{Alias: (*Alias)(r), Input: input})
}

// ToOpenAICompactionRequest converts a BifrostCompactionRequest to the OpenAI wire format.
func ToOpenAICompactionRequest(ctx *schemas.BifrostContext, req *schemas.BifrostCompactionRequest) *OpenAICompactionRequest {
if req == nil {
Expand Down
69 changes: 69 additions & 0 deletions core/providers/openai/responses_marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -967,3 +967,72 @@ func TestOpenAIResponsesRequest_MarshalJSON_CompactionSummaryStripped(t *testing
t.Errorf("reasoning item summary should be [], got: %s", string(rawSummary))
}
}

// TestOpenAICompactionRequest_MarshalJSON_Input guards against the `input` union
// serializing as a JSON object (which /v1/responses/compact rejects with
// "Invalid type for 'input': expected a string, but got an object instead").
func TestOpenAICompactionRequest_MarshalJSON_Input(t *testing.T) {
tests := []struct {
name string
request *OpenAICompactionRequest
assert func(t *testing.T, m map[string]interface{})
}{
{
name: "empty input is omitted (previous_response_id-only compaction)",
request: &OpenAICompactionRequest{
Model: "gpt-5",
PreviousResponseID: schemas.Ptr("resp_123"),
},
assert: func(t *testing.T, m map[string]interface{}) {
if v, ok := m["input"]; ok {
t.Errorf("input should be omitted when empty, got: %#v", v)
}
},
},
{
name: "string input serializes as a bare string",
request: &OpenAICompactionRequest{
Model: "gpt-5",
Input: OpenAIResponsesRequestInput{OpenAIResponsesRequestInputStr: schemas.Ptr("hello")},
},
assert: func(t *testing.T, m map[string]interface{}) {
if s, ok := m["input"].(string); !ok || s != "hello" {
t.Errorf("input should be the string %q, got: %#v", "hello", m["input"])
}
},
},
{
name: "array input serializes as an array",
request: &OpenAICompactionRequest{
Model: "gpt-5",
Input: OpenAIResponsesRequestInput{OpenAIResponsesRequestInputArray: []schemas.ResponsesMessage{{
Role: schemas.Ptr(schemas.ResponsesInputMessageRoleUser),
Content: &schemas.ResponsesMessageContent{ContentStr: schemas.Ptr("hi")},
}}},
},
assert: func(t *testing.T, m map[string]interface{}) {
if _, ok := m["input"].([]interface{}); !ok {
t.Errorf("input should be an array, got: %#v", m["input"])
}
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
jsonBytes, err := tt.request.MarshalJSON()
if err != nil {
t.Fatalf("MarshalJSON failed: %v", err)
}
var m map[string]interface{}
if err := sonic.Unmarshal(jsonBytes, &m); err != nil {
t.Fatalf("unmarshal failed: %v", err)
}
// The regression: input must never be a JSON object.
if obj, ok := m["input"].(map[string]interface{}); ok {
t.Fatalf("input serialized as an object (the bug): %v", obj)
}
tt.assert(t, m)
})
}
}