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
5 changes: 3 additions & 2 deletions backend/internal/service/openai_gateway_request_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,7 @@ func normalizeOpenAIResponsesCompatibilityBodyWithOptions(body []byte, account *
}
}
needsOrphanCleanup := account != nil && account.IsOpenAIOAuthLike() &&
gjson.GetBytes(normalized, "input").IsArray() && openAIResponsesMayNeedOrphanCleanup(normalized)
gjson.Get(openAIWSPayloadStringView(normalized), "input").IsArray() && openAIResponsesMayNeedOrphanCleanup(normalized)
if needsOrphanCleanup || openAIResponsesInputMayNeedTruncation(normalized) {
var reqBody map[string]any
if err := decodeOpenAIJSONUseNumber(normalized, &reqBody); err != nil {
Expand Down Expand Up @@ -1723,7 +1723,8 @@ func normalizeOpenAIPassthroughOAuthBody(body []byte, compact bool) ([]byte, boo
changed = true
}

if inputResult := gjson.GetBytes(normalized, "input"); inputResult.Exists() {
// Inspect the input in place so an already-normalized history array is never copied here.
if inputResult := gjson.Get(openAIWSPayloadStringView(normalized), "input"); inputResult.Exists() {
switch {
case inputResult.Type == gjson.String:
text := inputResult.String()
Expand Down
30 changes: 28 additions & 2 deletions backend/internal/service/openai_passthrough_normalization_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service

import (
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -306,11 +307,14 @@ func TestNormalizeOpenAIPassthroughOAuthBody_CompactRemovesUnsupportedUser(t *te
}

func TestNormalizeOpenAIPassthroughOAuthBody_StringInputWrappedAsArray(t *testing.T) {
body := []byte(`{"model":"gpt-5.4","input":"hello world"}`)
const original = `{"model":"gpt-5.4","input":"hello world"}`
body := []byte(original)

normalized, changed, err := normalizeOpenAIPassthroughOAuthBody(body, false)
require.NoError(t, err)
require.True(t, changed)
require.Equal(t, original, string(body))
clear(body) // The rewritten request must survive reuse of the original buffer.

input := gjson.GetBytes(normalized, "input")
require.True(t, input.IsArray(), "string input should be converted to array")
Expand All @@ -334,11 +338,14 @@ func TestNormalizeOpenAIPassthroughOAuthBody_EmptyStringInputWrappedAsEmptyArray
}

func TestNormalizeOpenAIPassthroughOAuthBody_ObjectInputWrappedAsArray(t *testing.T) {
body := []byte(`{"model":"gpt-5.4","input":{"type":"message","role":"user","content":"hi"}}`)
const original = `{"model":"gpt-5.4","input":{"type":"message","role":"user","content":"hi"}}`
body := []byte(original)

normalized, changed, err := normalizeOpenAIPassthroughOAuthBody(body, false)
require.NoError(t, err)
require.True(t, changed)
require.Equal(t, original, string(body))
clear(body)

input := gjson.GetBytes(normalized, "input")
require.True(t, input.IsArray(), "object input should be wrapped in array")
Expand All @@ -359,6 +366,25 @@ func TestNormalizeOpenAIPassthroughOAuthBody_ArrayInputUnchanged(t *testing.T) {
require.Equal(t, "message", input.Array()[0].Get("type").String())
}

func BenchmarkNormalizeOpenAIPassthroughOAuthArrayInput(b *testing.B) {
for _, tc := range []struct {
name string
size int
}{{"4KiB", 4 << 10}, {"1MiB", 1 << 20}} {
b.Run(tc.name, func(b *testing.B) {
body := []byte(`{"model":"gpt-5.5","store":false,"stream":true,"input":[{"role":"user","content":"` +
strings.Repeat("x", tc.size) + `"}]}`)
b.SetBytes(int64(len(body)))
b.ReportAllocs()
for b.Loop() {
if _, changed, err := normalizeOpenAIPassthroughOAuthBody(body, false); changed || err != nil {
b.Fatalf("changed=%v err=%v", changed, err)
}
}
})
}
}

func TestDetectOpenAIPassthroughInstructionsRejectReason(t *testing.T) {
for _, tt := range []struct {
name string
Expand Down
Loading