From 30152d1b16fb2fbc5d3d58a8ca71d2bd6fea8a89 Mon Sep 17 00:00:00 2001 From: Anankke Date: Sat, 14 Nov 2026 07:09:38 -0800 Subject: [PATCH] fix(apicompat): tolerate non-string action.url and skip unfinished web_search calls WebSearchAction.URL was a strict string, so a web_search_call whose action.url was not a string failed decoding of the whole Responses event and the Messages stream lost its terminal event. It is now raw like Sources and read as a string only for open_page. The non-streaming converter also filled results for web_search_call items that had not completed, while the streaming one only handles completed items. Both now emit blocks for completed calls only. --- .../pkg/apicompat/responses_to_anthropic.go | 3 +++ .../apicompat/responses_web_search_sources.go | 13 ++++++---- .../responses_web_search_sources_test.go | 24 +++++++++++++++++-- backend/internal/pkg/apicompat/types.go | 6 ++--- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/backend/internal/pkg/apicompat/responses_to_anthropic.go b/backend/internal/pkg/apicompat/responses_to_anthropic.go index a1c6485164..7ac3ffe0c7 100644 --- a/backend/internal/pkg/apicompat/responses_to_anthropic.go +++ b/backend/internal/pkg/apicompat/responses_to_anthropic.go @@ -55,6 +55,9 @@ func ResponsesToAnthropic(resp *ResponsesResponse, model string) *AnthropicRespo Input: sanitizeAnthropicToolUseInput(item.Name, item.Arguments), }) case "web_search_call": + if item.Status != "completed" { + continue + } toolUseID := "srvtoolu_" + item.ID query := "" if item.Action != nil { diff --git a/backend/internal/pkg/apicompat/responses_web_search_sources.go b/backend/internal/pkg/apicompat/responses_web_search_sources.go index 39efc7370d..f6f0bf16a7 100644 --- a/backend/internal/pkg/apicompat/responses_web_search_sources.go +++ b/backend/internal/pkg/apicompat/responses_web_search_sources.go @@ -27,9 +27,9 @@ type webSearchResult struct { } // webSearchCallResults is the web_search_tool_result content for one call: a -// search's action.sources, or the single page a completed open_page opened. -// Sources are decoded entry by entry, so one malformed entry cannot fail the -// terminal event that carries the call. +// search's action.sources, or the single page an open_page opened. Callers +// pass completed calls only. Sources and the page URL are decoded leniently, so +// a malformed value cannot fail the terminal event that carries the call. func webSearchCallResults(item *ResponsesOutput) json.RawMessage { results := []webSearchResult{} if action := item.Action; action != nil { @@ -42,8 +42,11 @@ func webSearchCallResults(item *ResponsesOutput) json.RawMessage { results = append(results, newWebSearchResult(rawURL)) } } - } else if action.Type == "open_page" && item.Status == "completed" && action.URL != "" { - results = append(results, newWebSearchResult(action.URL)) + } else if action.Type == "open_page" { + var pageURL string + if json.Unmarshal(action.URL, &pageURL) == nil && pageURL != "" { + results = append(results, newWebSearchResult(pageURL)) + } } } out, _ := json.Marshal(results) diff --git a/backend/internal/pkg/apicompat/responses_web_search_sources_test.go b/backend/internal/pkg/apicompat/responses_web_search_sources_test.go index 07742de86c..3cf9c04f3e 100644 --- a/backend/internal/pkg/apicompat/responses_web_search_sources_test.go +++ b/backend/internal/pkg/apicompat/responses_web_search_sources_test.go @@ -100,11 +100,31 @@ func TestWebSearchOpenPageReturnsSinglePage(t *testing.T) { func TestWebSearchOpenPageRequiresCompletedURL(t *testing.T) { for name, item := range map[string]string{ - "in progress": `{"type":"web_search_call","id":"ws_1","status":"in_progress","action":{"type":"open_page","url":"https://docs.example/page"}}`, - "empty url": `{"type":"web_search_call","id":"ws_1","status":"completed","action":{"type":"open_page","url":""}}`, + "empty url": `{"type":"web_search_call","id":"ws_1","status":"completed","action":{"type":"open_page","url":""}}`, } { t.Run(name, func(t *testing.T) { require.JSONEq(t, `[]`, nonStreamingWebSearchResults(t, item)["srvtoolu_ws_1"]) }) } } + +func TestWebSearchNonStringActionURLKeepsEvent(t *testing.T) { + for name, url := range map[string]string{"number": `123`, "object": `{"a":1}`, "array": `["a"]`} { + t.Run(name, func(t *testing.T) { + raw := `{"type":"web_search_call","id":"ws_1","status":"completed","action":{"type":"open_page","url":` + url + `}}` + got := nonStreamingWebSearchResults(t, raw) + require.JSONEq(t, `[]`, got["srvtoolu_ws_1"]) + require.JSONEq(t, `[]`, streamingWebSearchResults(t, raw)["srvtoolu_ws_1"]) + }) + } +} + +func TestWebSearchCallsThatDidNotCompleteProduceNoBlocks(t *testing.T) { + for _, status := range []string{"failed", "in_progress"} { + t.Run(status, func(t *testing.T) { + raw := `{"type":"web_search_call","id":"ws_1","status":"` + status + `","action":{"type":"search","query":"q","sources":[{"url":"https://a.example/1"}]}}` + require.Empty(t, nonStreamingWebSearchResults(t, raw)) + require.Empty(t, streamingWebSearchResults(t, raw)) + }) + } +} diff --git a/backend/internal/pkg/apicompat/types.go b/backend/internal/pkg/apicompat/types.go index a603d64147..d421feef78 100644 --- a/backend/internal/pkg/apicompat/types.go +++ b/backend/internal/pkg/apicompat/types.go @@ -492,9 +492,9 @@ func (o *ResponsesOutput) UnmarshalJSON(data []byte) error { type WebSearchAction struct { Type string `json:"type,omitempty"` // "search" Query string `json:"query,omitempty"` // primary search query - URL string `json:"url,omitempty"` // open_page / find_in_page target - // Sources stays raw: a strictly typed entry of an unexpected shape would - // fail decoding the whole terminal event that carries this call. + // URL and Sources stay raw: a strictly typed value of an unexpected shape + // would fail decoding the whole terminal event that carries this call. + URL json.RawMessage `json:"url,omitempty"` // open_page / find_in_page target Sources json.RawMessage `json:"sources,omitempty"` }