fix(responses): strip oversized input item IDs

This commit is contained in:
wucm667
2026-09-22 19:47:12 +08:00
parent 1716e99151
commit a5d8db2445
2 changed files with 18 additions and 1 deletions
@@ -50,7 +50,7 @@ func shouldStripOpenAIResponsesInputItemID(itemType, id string) bool {
if !constrained {
return false
}
return id == "" || !strings.HasPrefix(id, prefix)
return id == "" || len(id) > 64 || !strings.HasPrefix(id, prefix)
}
func shouldStripOpenAIResponsesNonPairCallID(itemType string) bool {
@@ -2,6 +2,7 @@ package service
import (
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/require"
@@ -29,6 +30,9 @@ func TestOpenAIResponsesInputItemIDPrefixUsesObservedOutputContracts(t *testing.
{itemType: "tool_search_call", id: "fc_123", strip: true},
{itemType: "web_search_call", id: "ws_123", strip: false},
{itemType: "web_search_call", id: "item_123", strip: true},
{itemType: "web_search_call", id: "ws_" + strings.Repeat("x", 60), strip: false},
{itemType: "web_search_call", id: "ws_" + strings.Repeat("x", 61), strip: false},
{itemType: "web_search_call", id: "ws_" + strings.Repeat("x", 62), strip: true},
{itemType: "custom_tool_call_output", id: "fc_123", strip: false},
{itemType: "custom_tool_call_output", id: "ctco_123", strip: true},
// Do not impose an inferred contract on output types for which there is
@@ -132,6 +136,19 @@ func TestSanitizeOpenAIResponsesInputItemIDsStripsEmptyKnownIDsOnly(t *testing.T
require.True(t, gjson.GetBytes(sanitized, "input.1.id").Exists())
}
func TestSanitizeOpenAIResponsesInputItemIDsStripsOversizedWebSearchID(t *testing.T) {
validID := "ws_" + strings.Repeat("x", 61)
oversizedID := "ws_" + strings.Repeat("x", 62)
body := []byte(`{"input":[{"type":"web_search_call","id":"` + validID + `"},{"type":"web_search_call","id":"` + oversizedID + `"}]}`)
sanitized, changed, err := sanitizeOpenAIResponsesInputItemIDs(body)
require.NoError(t, err)
require.True(t, changed)
require.Equal(t, validID, gjson.GetBytes(sanitized, "input.0.id").String())
require.False(t, gjson.GetBytes(sanitized, "input.1.id").Exists())
}
func TestSanitizeOpenAIResponsesInputItemIDsStripsOnlyNonPairCallIDs(t *testing.T) {
body := []byte(`{"input":[
{"type":"message","call_id":"remove_message","content":"hi"},