diff --git a/backend/internal/pkg/apicompat/responses_to_anthropic.go b/backend/internal/pkg/apicompat/responses_to_anthropic.go index 7bd8c8d788..45dcfaad38 100644 --- a/backend/internal/pkg/apicompat/responses_to_anthropic.go +++ b/backend/internal/pkg/apicompat/responses_to_anthropic.go @@ -55,7 +55,7 @@ func ResponsesToAnthropic(resp *ResponsesResponse, model string) *AnthropicRespo Input: anthropicToolUseInput(item.Type, item.Name, item.Arguments), }) case "custom_tool_call": - if !resp.RenderCustomToolCalls { + if !resp.RenderCustomToolCalls || (resp.XAIServerSideSearch && IsServerSideSearchCall(item.Type, item.CallID)) { continue } blocks = append(blocks, AnthropicContentBlock{ diff --git a/backend/internal/pkg/apicompat/responses_to_chatcompletions.go b/backend/internal/pkg/apicompat/responses_to_chatcompletions.go index c7c1c0afdb..6d1227a205 100644 --- a/backend/internal/pkg/apicompat/responses_to_chatcompletions.go +++ b/backend/internal/pkg/apicompat/responses_to_chatcompletions.go @@ -57,7 +57,7 @@ func ResponsesToChatCompletions(resp *ResponsesResponse, model string) *ChatComp }, }) case "custom_tool_call": - if !resp.RenderCustomToolCalls { + if !resp.RenderCustomToolCalls || (resp.XAIServerSideSearch && IsServerSideSearchCall(item.Type, item.CallID)) { continue } toolCalls = append(toolCalls, ChatToolCall{ diff --git a/backend/internal/pkg/apicompat/types.go b/backend/internal/pkg/apicompat/types.go index fc084f9c10..c3812ae960 100644 --- a/backend/internal/pkg/apicompat/types.go +++ b/backend/internal/pkg/apicompat/types.go @@ -389,6 +389,11 @@ type ResponsesResponse struct { // the accumulator has always handed custom_tool_call items to the converters // as tool calls. An output the upstream delivered keeps them unrendered. RenderCustomToolCalls bool `json:"-"` + + // XAIServerSideSearch is set with RenderCustomToolCalls when the upstream is + // xAI. A custom_tool_call that IsServerSideSearchCall then stays in the + // output but is not rendered as a tool call, because xAI runs it itself. + XAIServerSideSearch bool `json:"-"` } // ResponsesError describes an error in a failed response. diff --git a/backend/internal/service/openai_gateway_buffered_hosted_items.go b/backend/internal/service/openai_gateway_buffered_hosted_items.go index 772f0bc1af..69ac8f6205 100644 --- a/backend/internal/service/openai_gateway_buffered_hosted_items.go +++ b/backend/internal/service/openai_gateway_buffered_hosted_items.go @@ -80,6 +80,9 @@ type openAICompatOutputSlot struct { // so that an empty terminal output can be rebuilt from them. type openAICompatOutputSlots struct { slots []*openAICompatOutputSlot + // xaiServerSideSearch is set when the upstream is xAI, so that the rebuilt + // output keeps a server-side search call out of the client tool calls. + xaiServerSideSearch bool } func openAICompatIsToolCallType(itemType string) bool { @@ -291,6 +294,7 @@ func (o *openAICompatOutputSlots) fillTerminalOutput(eventType string, response response.Output = append(response.Output, item) if item.Type == "custom_tool_call" { response.RenderCustomToolCalls = true + response.XAIServerSideSearch = o.xaiServerSideSearch } } } diff --git a/backend/internal/service/openai_gateway_buffered_server_side_search_test.go b/backend/internal/service/openai_gateway_buffered_server_side_search_test.go new file mode 100644 index 0000000000..99be34e349 --- /dev/null +++ b/backend/internal/service/openai_gateway_buffered_server_side_search_test.go @@ -0,0 +1,65 @@ +//go:build unit + +package service + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +// A nonstreaming request rebuilds an empty terminal output from the output_item.done items and +// renders its custom_tool_call items as tool calls. On xAI an xs_ / as_ item is a search the +// server ran, so it is not a tool call but still counts as a Grok search. +func TestGrokBufferedRebuiltOutputKeepsServerSideSearchOutOfToolCalls(t *testing.T) { + account := healthyGrokOAuthGatewayTestAccount(4242, "access-token") + for _, path := range []string{"/v1/messages", "/v1/chat/completions"} { + t.Run(path+" search only", func(t *testing.T) { + sse := grokCustomToolCallStream([]string{grokXSearchItemJSON}, `[]`) + got := forwardGrokCustomToolCallStream(t, path, false, account, sse) + + require.Equal(t, 1, got.searchCount) + require.NotContains(t, got.body, "tool_use", got.body) + require.NotContains(t, got.body, "tool_calls", got.body) + require.NotContains(t, got.body, "x_keyword_search", got.body) + require.Contains(t, got.body, "found it", got.body) + if path == "/v1/messages" { + require.Contains(t, got.body, `"stop_reason":"end_turn"`, got.body) + } else { + require.Contains(t, got.body, `"finish_reason":"stop"`, got.body) + } + }) + t.Run(path+" search next to a client tool", func(t *testing.T) { + sse := grokCustomToolCallStream([]string{grokXSearchItemJSON, grokClientToolJSON}, `[]`) + got := forwardGrokCustomToolCallStream(t, path, false, account, sse) + + require.Equal(t, 1, got.searchCount) + require.NotContains(t, got.body, "x_keyword_search", got.body) + require.Contains(t, got.body, "exec", got.body) + if path == "/v1/messages" { + require.Contains(t, got.body, `"stop_reason":"tool_use"`, got.body) + } else { + require.Contains(t, got.body, `"finish_reason":"tool_calls"`, got.body) + } + }) + } +} + +// Off xAI the same call_id is an ordinary custom tool call and stays one. +func TestOpenAIBufferedRebuiltOutputKeepsXSCallIDAsToolCall(t *testing.T) { + account := rawChatCompletionsTestAccount() + for _, path := range []string{"/v1/messages", "/v1/chat/completions"} { + t.Run(path, func(t *testing.T) { + sse := grokCustomToolCallStream([]string{grokXSearchItemJSON}, `[]`) + got := forwardGrokCustomToolCallStream(t, path, false, account, sse) + + require.Equal(t, 0, got.searchCount) + require.Contains(t, got.body, "x_keyword_search", got.body) + if path == "/v1/messages" { + require.Contains(t, got.body, "tool_use", got.body) + } else { + require.Contains(t, got.body, "tool_calls", got.body) + } + }) + } +} diff --git a/backend/internal/service/openai_gateway_messages.go b/backend/internal/service/openai_gateway_messages.go index b4ba5ba25a..227fb70a9e 100644 --- a/backend/internal/service/openai_gateway_messages.go +++ b/backend/internal/service/openai_gateway_messages.go @@ -835,7 +835,7 @@ func (s *OpenAIGatewayService) readOpenAICompatBufferedTerminal( ) (*apicompat.ResponsesResponse, OpenAIUsage, *apicompat.BufferedResponseAccumulator, openAICompatPriorModelAudit, error) { acc := apicompat.NewBufferedResponseAccumulator() acc.XAIServerSideSearch = account != nil && account.IsGrok() - outputSlots := &openAICompatOutputSlots{} + outputSlots := &openAICompatOutputSlots{xaiServerSideSearch: acc.XAIServerSideSearch} var usage OpenAIUsage if resp == nil || resp.Body == nil { return nil, usage, acc, openAICompatPriorModelAudit{}, errors.New("upstream response body is nil")