Skip to content

Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.

openai: add client tool search support (#18223)

Parth Sareencf8b605b06f4
diff --git a/middleware/responses_tool_search_test.go b/middleware/responses_tool_search_test.go
new file mode 100644
index 000000000..b9a6aecaf
--- /dev/null
+++ b/middleware/responses_tool_search_test.go
@@ -0,0 +1,139 @@
+package middleware
+
+import (
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/gin-gonic/gin"
+
+ "github.com/ollama/ollama/api"
+)
+
+func TestResponsesMiddlewareToolSearchInput(t *testing.T) {
+ var captured *api.ChatRequest
+ router := gin.New()
+ router.Use(ResponsesMiddleware(), captureRequestMiddleware(&captured))
+ router.POST("/v1/responses", func(c *gin.Context) { c.Status(http.StatusOK) })
+
+ body := `{
+ "model":"test",
+ "tools":[{
+ "type":"tool_search",
+ "execution":"client",
+ "description":"Find tools",
+ "parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"]}
+ }],
+ "input":[
+ {"type":"tool_search_call","id":"ts_1","call_id":"call_search","execution":"client","status":"completed","arguments":{"query":"orders"}},
+ {"type":"tool_search_output","id":"tso_1","call_id":"call_search","execution":"client","status":"completed","tools":[
+ {"type":"namespace","name":"orders","tools":[
+ {"type":"function","name":"lookup_order","description":"Look up an order","parameters":{"type":"object"}}
+ ]}
+ ]}
+ ]
+ }`
+ req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(body))
+ req.Header.Set("Content-Type", "application/json")
+ resp := httptest.NewRecorder()
+ router.ServeHTTP(resp, req)
+
+ if resp.Code != http.StatusOK {
+ t.Fatalf("status = %d, body = %s", resp.Code, resp.Body.String())
+ }
+ if captured == nil {
+ t.Fatal("request was not converted")
+ }
+ if len(captured.Tools) != 1 || captured.Tools[0].Function.Name != "tool_search" {
+ t.Fatalf("native tools = %#v", captured.Tools)
+ }
+ if len(captured.Messages) != 2 {
+ t.Fatalf("messages = %#v", captured.Messages)
+ }
+ if got := captured.Messages[0].ToolCalls[0].Function.Name; got != "tool_search" {
+ t.Fatalf("search call name = %q", got)
+ }
+ if got := captured.Messages[1]; got.Role != "tool" || got.ToolName != "tool_search" || got.Content != `[{"description":"Look up an order","name":"orders.lookup_order","parameters":{"type":"object"},"type":"function"}]` {
+ t.Fatalf("search output = %#v", got)
+ }
+}
+
+func TestResponsesMiddlewareStreamsToolSearchCall(t *testing.T) {
+ router := gin.New()
+ router.Use(ResponsesMiddleware())
+ router.POST("/v1/responses", func(c *gin.Context) {
+ c.JSON(http.StatusOK, api.ChatResponse{
+ Message: api.Message{ToolCalls: []api.ToolCall{{
+ ID: "call_search",
+ Function: api.ToolCallFunction{
+ Name: "tool_search",
+ Arguments: testArgs(map[string]any{"query": "orders", "limit": 5}),
+ },
+ }}},
+ Done: true,
+ })
+ })
+
+ body := `{
+ "model":"test",
+ "stream":true,
+ "input":"look up an order",
+ "tools":[{
+ "type":"tool_search",
+ "execution":"client",
+ "description":"Find tools",
+ "parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"]}
+ }]
+ }`
+ req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(body))
+ req.Header.Set("Content-Type", "application/json")
+ resp := httptest.NewRecorder()
+ router.ServeHTTP(resp, req)
+
+ if resp.Code != http.StatusOK {
+ t.Fatalf("status = %d, body = %s", resp.Code, resp.Body.String())
+ }
+ var sawDone, sawCompleted bool
+ for _, frame := range strings.Split(resp.Body.String(), "\n\n") {
+ var data string
+ for _, line := range strings.Split(frame, "\n") {
+ if strings.HasPrefix(line, "data: ") {
+ data = strings.TrimPrefix(line, "data: ")
+ break
+ }
+ }
+ if data == "" {
+ continue
+ }
+ var event map[string]any
+ if err := json.Unmarshal([]byte(data), &event); err != nil {
+ t.Fatal(err)
+ }
+ switch event["type"] {
+ case "response.output_item.done":
+ item := event["item"].(map[string]any)
+ if item["type"] != "tool_search_call" || item["call_id"] != "call_search" || item["execution"] != "client" {
+ t.Fatalf("item = %#v", item)
+ }
+ arguments := item["arguments"].(map[string]any)
+ if arguments["query"] != "orders" || arguments["limit"] != float64(5) {
+ t.Fatalf("arguments = %#v", arguments)
+ }
+ sawDone = true
+ case "response.completed":
+ response := event["response"].(map[string]any)
+ output := response["output"].([]any)
+ if len(output) != 1 || output[0].(map[string]any)["type"] != "tool_search_call" {
+ t.Fatalf("completed output = %#v", output)
+ }
+ sawCompleted = true
+ case "response.function_call_arguments.delta", "response.function_call_arguments.done":
+ t.Fatalf("unexpected function-call event: %s", data)
+ }
+ }
+ if !sawDone || !sawCompleted {
+ t.Fatalf("saw output_item.done=%v response.completed=%v; body=%s", sawDone, sawCompleted, resp.Body.String())
+ }
+}
diff --git a/openai/responses.go b/openai/responses.go
index e32c8ac66..b5a739d2b 100644
--- a/openai/responses.go
+++ b/openai/responses.go
@@ -159,7 +159,8 @@ type ResponsesFunctionCall struct {
Type string `json:"type"` // always "function_call"
CallID string `json:"call_id"` // the tool call ID
Name string `json:"name"` // function name
- Arguments string `json:"arguments"` // JSON arguments string
+ Namespace string `json:"namespace,omitempty"`
+ Arguments string `json:"arguments"` // JSON arguments string
}
func (ResponsesFunctionCall) responsesInputItem() {}
@@ -227,6 +228,30 @@ func (o *ResponsesFunctionCallOutput) UnmarshalJSON(data []byte) error {
func (ResponsesFunctionCallOutput) responsesInputItem() {}
+// ResponsesToolSearchCall is a tool_search_call input item.
+type ResponsesToolSearchCall struct {
+ ID string `json:"id,omitempty"`
+ Type string `json:"type"` // always "tool_search_call"
+ CallID string `json:"call_id"`
+ Execution string `json:"execution"` // always "client"
+ Status string `json:"status,omitempty"`
+ Arguments api.ToolCallFunctionArguments `json:"arguments"`
+}
+
+func (ResponsesToolSearchCall) responsesInputItem() {}
+
+// ResponsesToolSearchOutput is a tool_search_output input item.
+type ResponsesToolSearchOutput struct {
+ ID string `json:"id,omitempty"`
+ Type string `json:"type"` // always "tool_search_output"
+ CallID string `json:"call_id"`
+ Execution string `json:"execution"` // always "client"
+ Status string `json:"status,omitempty"`
+ Tools []json.RawMessage `json:"tools"`
+}
+
+func (ResponsesToolSearchOutput) responsesInputItem() {}
+
// ResponsesReasoningInput represents a reasoning item passed back as input.
// This is used when the client sends previous reasoning back for context.
type ResponsesReasoningInput struct {
@@ -274,6 +299,18 @@ func unmarshalResponsesInputItem(data []byte) (ResponsesInputItem, error) {
return nil, err
}
return output, nil
+ case "tool_search_call":
+ var call ResponsesToolSearchCall
+ if err := json.Unmarshal(data, &call); err != nil {
+ return nil, err
+ }
+ return call, nil
+ case "tool_search_output":
+ var output ResponsesToolSearchOutput
+ if err := json.Unmarshal(data, &output); err != nil {
+ return nil, err
+ }
+ return output, nil
case "reasoning":
var reasoning ResponsesReasoningInput
if err := json.Unmarshal(data, &reasoning); err != nil {
@@ -353,11 +390,13 @@ type ResponsesText struct {
// ResponsesTool represents a tool in the Responses API format.
// Note: This differs from api.Tool which nests fields under "function".
type ResponsesTool struct {
- Type string `json:"type"` // "function", "namespace", or "web_search"
- Name string `json:"name"`
- Description *string `json:"description"` // nullable but required
- Strict *bool `json:"strict"` // nullable but required
- Parameters map[string]any `json:"parameters"` // nullable but required
+ Type string `json:"type"` // "function", "namespace", "tool_search", or "web_search"
+ Name string `json:"name,omitempty"`
+ Description *string `json:"description,omitempty"`
+ Strict *bool `json:"strict,omitempty"`
+ Parameters map[string]any `json:"parameters,omitempty"`
+ Execution string `json:"execution,omitempty"`
+ DeferLoading *bool `json:"defer_loading,omitempty"`
// Tools carries a "namespace" declaration's member functions. The
// Responses API groups related tools by domain under a namespace tool
@@ -415,6 +454,7 @@ type ResponsesRequest struct {
// FromResponsesRequest converts a ResponsesRequest to api.ChatRequest
func FromResponsesRequest(r ResponsesRequest) (*api.ChatRequest, error) {
var messages []api.Message
+ availableTools := responsesRequestTools(r)
// Add instructions as system message if present
if r.Instructions != "" {
@@ -493,33 +533,21 @@ func FromResponsesRequest(r ResponsesRequest) (*api.ChatRequest, error) {
return nil, fmt.Errorf("failed to parse function call arguments: %w", err)
}
}
+ namespace, name := v.Namespace, v.Name
+ if namespace == "" {
+ if matchedNamespace, matchedName := responsesToolCallName(availableTools, name); matchedNamespace != "" {
+ namespace, name = matchedNamespace, matchedName
+ }
+ }
toolCall := api.ToolCall{
ID: v.CallID,
Function: api.ToolCallFunction{
- Name: v.Name,
+ Name: qualifyNamespaceToolName(namespace, name),
Arguments: args,
},
}
- // Merge tool call into existing assistant message if it has content or tool calls
- if len(messages) > 0 && messages[len(messages)-1].Role == "assistant" {
- lastMsg := &messages[len(messages)-1]
- lastMsg.ToolCalls = append(lastMsg.ToolCalls, toolCall)
- if pendingThinking != "" {
- lastMsg.Thinking = pendingThinking
- pendingThinking = ""
- }
- } else {
- msg := api.Message{
- Role: "assistant",
- ToolCalls: []api.ToolCall{toolCall},
- }
- if pendingThinking != "" {
- msg.Thinking = pendingThinking
- pendingThinking = ""
- }
- messages = append(messages, msg)
- }
+ messages = appendResponseToolCall(messages, toolCall, &pendingThinking)
case ResponsesFunctionCallOutput:
content := v.Output
var images []api.ImageData
@@ -536,6 +564,33 @@ func FromResponsesRequest(r ResponsesRequest) (*api.ChatRequest, error) {
Images: images,
ToolCallID: v.CallID,
})
+ case ResponsesToolSearchCall:
+ messages = appendResponseToolCall(messages, api.ToolCall{
+ ID: v.CallID,
+ Function: api.ToolCallFunction{
+ Name: "tool_search",
+ Arguments: v.Arguments,
+ },
+ }, &pendingThinking)
+ case ResponsesToolSearchOutput:
+ tools := v.Tools
+ if tools == nil {
+ tools = []json.RawMessage{}
+ }
+ tools, err := modelToolSearchTools(tools)
+ if err != nil {
+ return nil, err
+ }
+ content, err := json.Marshal(tools)
+ if err != nil {
+ return nil, fmt.Errorf("failed to encode tool search output: %w", err)
+ }
+ messages = append(messages, api.Message{
+ Role: "tool",
+ Content: string(content),
+ ToolName: "tool_search",
+ ToolCallID: v.CallID,
+ })
case ResponsesWebSearchCall:
// Built-in tool calls are history metadata. The assistant message
// that follows carries the model-visible result of the prior search.
@@ -580,11 +635,23 @@ func FromResponsesRequest(r ResponsesRequest) (*api.ChatRequest, error) {
// Convert tools from Responses API format to api.Tool format
var tools []api.Tool
hasWebSearch := HasWebSearchTool(r.Tools)
+ hasToolSearch := HasToolSearchTool(r.Tools)
for _, t := range r.Tools {
if isWebSearchTool(t) {
tools = append(tools, WebSearchFunctionTool())
continue
}
+ if isToolSearchTool(t) {
+ if t.Execution != "" && t.Execution != "client" {
+ return nil, fmt.Errorf("tool_search execution %q is not supported; use client execution", t.Execution)
+ }
+ tool, err := ToolSearchFunctionTool(t)
+ if err != nil {
+ return nil, err
+ }
+ tools = append(tools, tool)
+ continue
+ }
expanded, err := convertTools(t)
if err != nil {
return nil, err
@@ -592,7 +659,8 @@ func FromResponsesRequest(r ResponsesRequest) (*api.ChatRequest, error) {
for _, tool := range expanded {
// The built-in tool owns this name. Keeping a user-declared function
// with the same name makes a model call ambiguous.
- if hasWebSearch && tool.Function.Name == "web_search" {
+ if (hasWebSearch && tool.Function.Name == "web_search") ||
+ (hasToolSearch && tool.Function.Name == "tool_search") {
continue
}
tools = append(tools, tool)
@@ -620,8 +688,29 @@ func FromResponsesRequest(r ResponsesRequest) (*api.ChatRequest, error) {
}, nil
}
+func appendResponseToolCall(messages []api.Message, toolCall api.ToolCall, pendingThinking *string) []api.Message {
+ if len(messages) > 0 && messages[len(messages)-1].Role == "assistant" {
+ last := &messages[len(messages)-1]
+ last.ToolCalls = append(last.ToolCalls, toolCall)
+ if *pendingThinking != "" {
+ last.Thinking = *pendingThinking
+ *pendingThinking = ""
+ }
+ return messages
+ }
+
+ msg := api.Message{Role: "assistant", ToolCalls: []api.ToolCall{toolCall}}
+ if *pendingThinking != "" {
+ msg.Thinking = *pendingThinking
+ *pendingThinking = ""
+ }
+ return append(messages, msg)
+}
+
func isWebSearchTool(t ResponsesTool) bool { return t.Type == "web_search" }
+func isToolSearchTool(t ResponsesTool) bool { return t.Type == "tool_search" }
+
// HasWebSearchTool reports whether a request declares the built-in Responses
// web-search tool.
func HasWebSearchTool(tools []ResponsesTool) bool {
@@ -633,9 +722,24 @@ func HasWebSearchTool(tools []ResponsesTool) bool {
return false
}
-// WebSearchFunctionTool is the private function contract passed to local
-// models. Responses clients must only ever see their original web_search
-// declaration echoed back.
+// HasToolSearchTool reports whether tools include tool_search.
+func HasToolSearchTool(tools []ResponsesTool) bool {
+ for _, tool := range tools {
+ if isToolSearchTool(tool) {
+ return true
+ }
+ }
+ return false
+}
+
+// ToolSearchFunctionTool converts a tool_search declaration to an api.Tool.
+func ToolSearchFunctionTool(t ResponsesTool) (api.Tool, error) {
+ t.Type = "function"
+ t.Name = "tool_search"
+ return convertTool(t)
+}
+
+// WebSearchFunctionTool returns the web search function declaration.
func WebSearchFunctionTool() api.Tool {
properties := api.NewToolPropertiesMap()
properties.Set("query", api.ToolProperty{Type: api.PropertyType{"string"}, Description: "The search query."})
@@ -653,12 +757,7 @@ func WebSearchFunctionTool() api.Tool {
}
}
-// convertTools converts one Responses-API tool declaration to api.Tools. A
-// "namespace" declaration groups member functions under a common name; it
-// expands to those members with namespace-qualified names, since api.Tool
-// carries only a flat function name. Dropping the members instead would
-// leave the model with one schema-less pseudo-function and make every
-// namespaced call undeclarable.
+// convertTools converts one Responses API tool declaration to api.Tools.
func convertTools(t ResponsesTool) ([]api.Tool, error) {
if t.Type != "namespace" {
tool, err := convertTool(t)
@@ -675,15 +774,107 @@ func convertTools(t ResponsesTool) ([]api.Tool, error) {
return nil, err
}
for i := range expanded {
- if prefix := t.Name + "."; t.Name != "" && !strings.HasPrefix(expanded[i].Function.Name, prefix) {
- expanded[i].Function.Name = prefix + expanded[i].Function.Name
- }
+ expanded[i].Function.Name = qualifyNamespaceToolName(t.Name, expanded[i].Function.Name)
}
tools = append(tools, expanded...)
}
return tools, nil
}
+func qualifyNamespaceToolName(namespace, member string) string {
+ if namespace == "" || member == "" {
+ return member
+ }
+ if strings.HasPrefix(member, namespace+".") || strings.HasPrefix(member, namespace+"_") {
+ return member
+ }
+ if strings.HasPrefix(member, "_") {
+ return namespace + member
+ }
+ return namespace + "." + member
+}
+
+func responsesToolCallName(tools []ResponsesTool, qualified string) (namespace, name string) {
+ for _, tool := range tools {
+ if tool.Type != "namespace" || tool.Name == "" {
+ continue
+ }
+ for _, member := range tool.Tools {
+ if member.Type == "namespace" {
+ continue
+ }
+ native := qualifyNamespaceToolName(tool.Name, member.Name)
+ dotted := tool.Name + "." + member.Name
+ colon := tool.Name + ":" + member.Name
+ if native == qualified || dotted == qualified || colon == qualified {
+ return tool.Name, member.Name
+ }
+ }
+ }
+ return "", qualified
+}
+
+// modelToolSearchTools flattens namespace members.
+func modelToolSearchTools(tools []json.RawMessage) ([]json.RawMessage, error) {
+ flattened := make([]json.RawMessage, 0, len(tools))
+ for _, raw := range tools {
+ var tool struct {
+ Type string `json:"type"`
+ Name string `json:"name"`
+ Tools []json.RawMessage `json:"tools"`
+ }
+ if err := json.Unmarshal(raw, &tool); err != nil {
+ return nil, fmt.Errorf("failed to decode tool search result: %w", err)
+ }
+ if tool.Type != "namespace" {
+ flattened = append(flattened, raw)
+ continue
+ }
+ if tool.Name == "" {
+ return nil, fmt.Errorf("tool search namespace is missing a name")
+ }
+
+ for _, rawMember := range tool.Tools {
+ var member map[string]json.RawMessage
+ if err := json.Unmarshal(rawMember, &member); err != nil {
+ return nil, fmt.Errorf("failed to decode tool search namespace %q member: %w", tool.Name, err)
+ }
+ var name string
+ if err := json.Unmarshal(member["name"], &name); err != nil || name == "" {
+ return nil, fmt.Errorf("tool search namespace %q has a member without a name", tool.Name)
+ }
+ qualifiedName, err := json.Marshal(tool.Name + "." + name)
+ if err != nil {
+ return nil, fmt.Errorf("failed to encode tool search namespace %q member name: %w", tool.Name, err)
+ }
+ member["name"] = qualifiedName
+ encoded, err := json.Marshal(member)
+ if err != nil {
+ return nil, fmt.Errorf("failed to encode tool search namespace %q member: %w", tool.Name, err)
+ }
+ flattened = append(flattened, encoded)
+ }
+ }
+ return flattened, nil
+}
+
+func responsesRequestTools(r ResponsesRequest) []ResponsesTool {
+ tools := append([]ResponsesTool(nil), r.Tools...)
+ for _, item := range r.Input.Items {
+ output, ok := item.(ResponsesToolSearchOutput)
+ if !ok {
+ continue
+ }
+ for _, raw := range output.Tools {
+ var tool ResponsesTool
+ if err := json.Unmarshal(raw, &tool); err == nil {
+ tools = append(tools, tool)
+ }
+ }
+ }
+ return tools
+}
+
func convertTool(t ResponsesTool) (api.Tool, error) {
// Convert parameters from map[string]any to api.ToolFunctionParameters
var params api.ToolFunctionParameters
@@ -815,13 +1006,15 @@ type ResponsesResponse struct {
type ResponsesOutputItem struct {
ID string `json:"id"`
- Type string `json:"type"` // "message", "function_call", or "reasoning"
+ Type string `json:"type"` // "message", "function_call", "tool_search_call", or "reasoning"
Status string `json:"status,omitempty"`
Role string `json:"role,omitempty"` // for message
Content []ResponsesOutputContent `json:"content,omitempty"` // for message
- CallID string `json:"call_id,omitempty"` // for function_call
+ CallID string `json:"call_id,omitempty"` // for function_call or tool_search_call
Name string `json:"name,omitempty"` // for function_call
- Arguments string `json:"arguments,omitempty"` // for function_call
+ Namespace string `json:"namespace,omitempty"` // for namespaced function_call
+ Execution string `json:"execution,omitempty"` // for tool_search_call
+ Arguments any `json:"arguments,omitempty"` // string for function_call, object for tool_search_call
Action *ResponsesWebSearchAction `json:"action,omitempty"` // for web_search_call
// Reasoning fields
@@ -922,13 +1115,27 @@ func ToResponse(model, responseID, itemID string, chatResponse api.ChatResponse,
if len(chatResponse.Message.ToolCalls) > 0 {
toolCalls := ToToolCalls(chatResponse.Message.ToolCalls)
+ availableTools := responsesRequestTools(request)
for i, tc := range toolCalls {
+ if HasToolSearchTool(request.Tools) && tc.Function.Name == "tool_search" {
+ output = append(output, ResponsesOutputItem{
+ ID: fmt.Sprintf("ts_%s_%d", responseID, i),
+ Type: "tool_search_call",
+ Status: "completed",
+ CallID: tc.ID,
+ Execution: "client",
+ Arguments: toolSearchArguments(tc.Function.Arguments),
+ })
+ continue
+ }
+ namespace, name := responsesToolCallName(availableTools, tc.Function.Name)
output = append(output, ResponsesOutputItem{
ID: fmt.Sprintf("fc_%s_%d", responseID, i),
Type: "function_call",
Status: "completed",
CallID: tc.ID,
- Name: tc.Function.Name,
+ Namespace: namespace,
+ Name: name,
Arguments: tc.Function.Arguments,
})
}
@@ -1026,6 +1233,13 @@ func ToResponse(model, responseID, itemID string, chatResponse api.ChatResponse,
}
}
+func toolSearchArguments(arguments string) json.RawMessage {
+ if !json.Valid([]byte(arguments)) {
+ return json.RawMessage(`{}`)
+ }
+ return json.RawMessage(arguments)
+}
+
// Streaming events: <https://platform.openai.com/docs/api-reference/responses-streaming>
// ResponsesStreamEvent represents a single Server-Sent Event for the Responses API.
@@ -1136,20 +1350,9 @@ func (c *ResponsesStreamConverter) buildResponseObject(status string, output []a
truncation = *c.request.Truncation
}
- var tools []any
- if c.request.Tools != nil {
- for _, t := range c.request.Tools {
- tools = append(tools, map[string]any{
- "type": t.Type,
- "name": t.Name,
- "description": t.Description,
- "strict": t.Strict,
- "parameters": t.Parameters,
- })
- }
- }
+ tools := c.request.Tools
if tools == nil {
- tools = []any{}
+ tools = []ResponsesTool{}
}
textFormat := map[string]any{"type": "text"}
@@ -1322,32 +1525,72 @@ func (c *ResponsesStreamConverter) processToolCalls(toolCalls []api.ToolCall) []
func (c *ResponsesStreamConverter) emitFunctionCallEvents(toolCalls []api.ToolCall) []ResponsesStreamEvent {
var events []ResponsesStreamEvent
converted := ToToolCalls(toolCalls)
+ availableTools := responsesRequestTools(c.request)
for i, tc := range converted {
outputIndex := c.outputIndex + i
+ if HasToolSearchTool(c.request.Tools) && tc.Function.Name == "tool_search" {
+ itemID := fmt.Sprintf("ts_%d_%d", rand.Intn(999999), i)
+ arguments := toolSearchArguments(tc.Function.Arguments)
+ item := map[string]any{
+ "id": itemID,
+ "type": "tool_search_call",
+ "status": "completed",
+ "call_id": tc.ID,
+ "execution": "client",
+ "arguments": arguments,
+ }
+ c.completedItems = append(c.completedItems, item)
+ events = append(events,
+ c.newEvent("response.output_item.added", map[string]any{
+ "output_index": outputIndex,
+ "item": map[string]any{
+ "id": itemID,
+ "type": "tool_search_call",
+ "status": "in_progress",
+ "call_id": tc.ID,
+ "execution": "client",
+ "arguments": json.RawMessage(`{}`),
+ },
+ }),
+ c.newEvent("response.output_item.done", map[string]any{
+ "output_index": outputIndex,
+ "item": item,
+ }),
+ )
+ continue
+ }
fcItemID := fmt.Sprintf("fc_%d_%d", rand.Intn(999999), i)
+ namespace, name := responsesToolCallName(availableTools, tc.Function.Name)
toolCallItem := map[string]any{
"id": fcItemID,
"type": "function_call",
"status": "completed",
"call_id": tc.ID,
- "name": tc.Function.Name,
+ "name": name,
"arguments": tc.Function.Arguments,
}
+ if namespace != "" {
+ toolCallItem["namespace"] = namespace
+ }
c.completedItems = append(c.completedItems, toolCallItem)
+ inProgressItem := map[string]any{
+ "id": fcItemID,
+ "type": "function_call",
+ "status": "in_progress",
+ "call_id": tc.ID,
+ "name": name,
+ "arguments": "",
+ }
+ if namespace != "" {
+ inProgressItem["namespace"] = namespace
+ }
events = append(events,
c.newEvent("response.output_item.added", map[string]any{
"output_index": outputIndex,
- "item": map[string]any{
- "id": fcItemID,
- "type": "function_call",
- "status": "in_progress",
- "call_id": tc.ID,
- "name": tc.Function.Name,
- "arguments": "",
- },
+ "item": inProgressItem,
}),
c.newEvent("response.function_call_arguments.delta", map[string]any{
"item_id": fcItemID,
diff --git a/openai/responses_test.go b/openai/responses_test.go
index b43bed29f..853b9f0b8 100644
--- a/openai/responses_test.go
+++ b/openai/responses_test.go
@@ -249,6 +249,42 @@ func TestUnmarshalResponsesInputItem(t *testing.T) {
}
})
+ t.Run("tool_search_call item", func(t *testing.T) {
+ got, err := unmarshalResponsesInputItem([]byte(`{"type":"tool_search_call","id":"ts_1","call_id":"call_search","execution":"client","status":"completed","arguments":{"query":"order lookup","limit":5}}`))
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ call, ok := got.(ResponsesToolSearchCall)
+ if !ok {
+ t.Fatalf("got type %T, want ResponsesToolSearchCall", got)
+ }
+ if call.CallID != "call_search" || call.Execution != "client" {
+ t.Fatalf("call = %#v", call)
+ }
+ if query, ok := call.Arguments.Get("query"); !ok || query != "order lookup" {
+ t.Fatalf("query = %#v, %v", query, ok)
+ }
+ })
+
+ t.Run("tool_search_output item", func(t *testing.T) {
+ got, err := unmarshalResponsesInputItem([]byte(`{"type":"tool_search_output","id":"tso_1","call_id":"call_search","execution":"client","status":"completed","tools":[{"type":"function","name":"lookup_order","x_client_field":true}]}`))
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ output, ok := got.(ResponsesToolSearchOutput)
+ if !ok {
+ t.Fatalf("got type %T, want ResponsesToolSearchOutput", got)
+ }
+ if output.CallID != "call_search" || len(output.Tools) != 1 {
+ t.Fatalf("output = %#v", output)
+ }
+ if !json.Valid(output.Tools[0]) {
+ t.Fatalf("tool is not valid JSON: %s", output.Tools[0])
+ }
+ })
+
t.Run("web_search_call item", func(t *testing.T) {
got, err := unmarshalResponsesInputItem([]byte(`{"type":"web_search_call","id":"ws_123","status":"completed","action":{"type":"search","query":"Parth Sareen"}}`))
if err != nil {
@@ -525,6 +561,212 @@ func TestFromResponsesRequest_WebSearchTool(t *testing.T) {
}
}
+func TestFromResponsesRequest_ToolSearch(t *testing.T) {
+ description := "Find the project tools needed to continue."
+ request := ResponsesRequest{
+ Tools: []ResponsesTool{
+ {
+ Type: "tool_search",
+ Execution: "client",
+ Description: &description,
+ Parameters: map[string]any{
+ "type": "object",
+ "properties": map[string]any{"query": map[string]any{"type": "string"}},
+ "required": []any{"query"},
+ },
+ },
+ {Type: "function", Name: "tool_search"},
+ {Type: "function", Name: "exec_command"},
+ },
+ }
+
+ chat, err := FromResponsesRequest(request)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(chat.Tools) != 2 {
+ t.Fatalf("tools = %#v, want tool_search and exec_command", chat.Tools)
+ }
+ if got := chat.Tools[0].Function.Name; got != "tool_search" {
+ t.Fatalf("tool name = %q", got)
+ }
+ if got := chat.Tools[0].Function.Description; got != description {
+ t.Fatalf("description = %q", got)
+ }
+ if got := chat.Tools[0].Function.Parameters.Required; len(got) != 1 || got[0] != "query" {
+ t.Fatalf("required = %#v", got)
+ }
+ if got := chat.Tools[1].Function.Name; got != "exec_command" {
+ t.Fatalf("core tool name = %q", got)
+ }
+}
+
+func TestFromResponsesRequestRejectsHostedToolSearch(t *testing.T) {
+ _, err := FromResponsesRequest(ResponsesRequest{Tools: []ResponsesTool{{Type: "tool_search", Execution: "server"}}})
+ if err == nil {
+ t.Fatal("expected hosted tool search to be rejected")
+ }
+}
+
+func TestFromResponsesRequest_ToolSearchOutputBecomesToolContent(t *testing.T) {
+ var request ResponsesRequest
+ if err := json.Unmarshal([]byte(`{
+ "model":"test",
+ "tools":[{
+ "type":"tool_search",
+ "execution":"client",
+ "description":"Find tools",
+ "parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"]}
+ }],
+ "input":[
+ {"type":"tool_search_call","id":"ts_1","call_id":"call_search","execution":"client","status":"completed","arguments":{"query":"orders","limit":5}},
+ {"type":"tool_search_output","id":"tso_1","call_id":"call_search","execution":"client","status":"completed","tools":[
+ {"type":"function","name":"lookup_order","description":"Look up an order","defer_loading":true,"x_client_field":"preserved","parameters":{"type":"object"}}
+ ]}
+ ]
+ }`), &request); err != nil {
+ t.Fatal(err)
+ }
+
+ chat, err := FromResponsesRequest(request)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(chat.Messages) != 2 {
+ t.Fatalf("messages = %#v", chat.Messages)
+ }
+ call := chat.Messages[0]
+ if call.Role != "assistant" || len(call.ToolCalls) != 1 || call.ToolCalls[0].Function.Name != "tool_search" {
+ t.Fatalf("search call message = %#v", call)
+ }
+ if query, ok := call.ToolCalls[0].Function.Arguments.Get("query"); !ok || query != "orders" {
+ t.Fatalf("query = %#v, %v", query, ok)
+ }
+ output := chat.Messages[1]
+ if output.Role != "tool" || output.ToolName != "tool_search" || output.ToolCallID != "call_search" {
+ t.Fatalf("search output message = %#v", output)
+ }
+ var content []map[string]any
+ if err := json.Unmarshal([]byte(output.Content), &content); err != nil {
+ t.Fatal(err)
+ }
+ if len(content) != 1 || content[0]["name"] != "lookup_order" || content[0]["x_client_field"] != "preserved" {
+ t.Fatalf("content = %#v", content)
+ }
+ if len(chat.Tools) != 1 || chat.Tools[0].Function.Name != "tool_search" {
+ t.Fatalf("native tools = %#v; discovered tool must remain content-only", chat.Tools)
+ }
+}
+
+func TestFromResponsesRequest_ToolSearchOutputFlattensNamespaceMembers(t *testing.T) {
+ namespace := json.RawMessage(`{
+ "type":"namespace",
+ "name":"mcp__openaiDeveloperDocs",
+ "description":"Official OpenAI documentation tools",
+ "tools":[
+ {"type":"function","name":"search_openai_docs","description":"Search docs","strict":false,"defer_loading":true,"x_client_field":true,"parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"]}}
+ ]
+ }`)
+ request := ResponsesRequest{
+ Tools: []ResponsesTool{{Type: "tool_search", Execution: "client"}},
+ Input: ResponsesInput{Items: []ResponsesInputItem{
+ ResponsesToolSearchCall{
+ Type: "tool_search_call",
+ CallID: "call_search",
+ Execution: "client",
+ },
+ ResponsesToolSearchOutput{
+ Type: "tool_search_output",
+ CallID: "call_search",
+ Execution: "client",
+ Tools: []json.RawMessage{
+ namespace,
+ json.RawMessage(`{"type":"namespace","name":"mcp__codex_apps__github","tools":[{"type":"function","name":"_search","parameters":{"type":"object"}}]}`),
+ json.RawMessage(`{"type":"function","name":"plain","x_client_field":"preserved"}`),
+ },
+ },
+ }},
+ }
+
+ chat, err := FromResponsesRequest(request)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(chat.Messages) != 2 {
+ t.Fatalf("messages = %#v", chat.Messages)
+ }
+ var content []map[string]any
+ if err := json.Unmarshal([]byte(chat.Messages[1].Content), &content); err != nil {
+ t.Fatal(err)
+ }
+ if len(content) != 3 {
+ t.Fatalf("tools = %#v", content)
+ }
+ if got := content[0]["name"]; got != "mcp__openaiDeveloperDocs.search_openai_docs" {
+ t.Fatalf("docs tool name = %#v", got)
+ }
+ if got := content[0]["description"]; got != "Search docs" {
+ t.Fatalf("docs description = %#v", got)
+ }
+ if got := content[0]["strict"]; got != false {
+ t.Fatalf("docs strict = %#v", got)
+ }
+ if got := content[0]["defer_loading"]; got != true {
+ t.Fatalf("docs defer_loading = %#v", got)
+ }
+ parameters, ok := content[0]["parameters"].(map[string]any)
+ if !ok || parameters["type"] != "object" {
+ t.Fatalf("docs parameters = %#v", content[0]["parameters"])
+ }
+ properties, ok := parameters["properties"].(map[string]any)
+ if !ok {
+ t.Fatalf("docs parameter properties = %#v", parameters["properties"])
+ }
+ query, ok := properties["query"].(map[string]any)
+ if !ok || query["type"] != "string" {
+ t.Fatalf("docs query parameter = %#v", properties["query"])
+ }
+ required, ok := parameters["required"].([]any)
+ if !ok || len(required) != 1 || required[0] != "query" {
+ t.Fatalf("docs required parameters = %#v", parameters["required"])
+ }
+ if got := content[0]["x_client_field"]; got != true {
+ t.Fatalf("docs custom field = %#v", got)
+ }
+ if got := content[1]["name"]; got != "mcp__codex_apps__github._search" {
+ t.Fatalf("app tool name = %#v", got)
+ }
+ if got := content[2]["name"]; got != "plain" {
+ t.Fatalf("plain tool name = %#v", got)
+ }
+ if got := string(request.Input.Items[1].(ResponsesToolSearchOutput).Tools[0]); got != string(namespace) {
+ t.Fatalf("Responses tool_search_output was mutated: %s", got)
+ }
+ if len(chat.Tools) != 1 || chat.Tools[0].Function.Name != "tool_search" {
+ t.Fatalf("native tools = %#v; discovered tools must remain content-only", chat.Tools)
+ }
+}
+
+func TestFromResponsesRequestRejectsIncompleteToolSearchNamespace(t *testing.T) {
+ tests := []json.RawMessage{
+ json.RawMessage(`{"type":"namespace","tools":[{"type":"function","name":"search"}]}`),
+ json.RawMessage(`{"type":"namespace","name":"docs","tools":[{"type":"function"}]}`),
+ }
+ for _, tool := range tests {
+ t.Run(string(tool), func(t *testing.T) {
+ _, err := FromResponsesRequest(ResponsesRequest{
+ Input: ResponsesInput{Items: []ResponsesInputItem{ResponsesToolSearchOutput{
+ Type: "tool_search_output",
+ Tools: []json.RawMessage{tool},
+ }}},
+ })
+ if err == nil {
+ t.Fatal("expected incomplete namespace to be rejected")
+ }
+ })
+ }
+}
+
func TestFromResponsesRequest_WebSearchIgnoresUnknownControls(t *testing.T) {
for _, declaration := range []string{
`{"type":"web_search","filters":{"allowed_domains":["example.com"]}}`,
@@ -638,6 +880,273 @@ func TestFromResponsesRequest_NamespaceTools(t *testing.T) {
}
}
+func TestConvertToolsQualifiesNamespaceMemberNames(t *testing.T) {
+ tests := []struct {
+ name string
+ namespace string
+ member string
+ want string
+ }{
+ {name: "dot member", namespace: "muse", member: "bash", want: "muse.bash"},
+ {name: "qualified dot member", namespace: "muse", member: "muse.read_file", want: "muse.read_file"},
+ {name: "Codex plugin member", namespace: "mcp__codex_apps__notion", member: "_search", want: "mcp__codex_apps__notion_search"},
+ {name: "qualified Codex plugin member", namespace: "mcp__codex_apps__notion", member: "mcp__codex_apps__notion_search", want: "mcp__codex_apps__notion_search"},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ tools, err := convertTools(ResponsesTool{
+ Type: "namespace",
+ Name: tt.namespace,
+ Tools: []ResponsesTool{{
+ Type: "function",
+ Name: tt.member,
+ }},
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(tools) != 1 {
+ t.Fatalf("tools = %#v, want one tool", tools)
+ }
+ if got := tools[0].Function.Name; got != tt.want {
+ t.Fatalf("tool name = %q, want %q", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestFromResponsesRequestPreservesFunctionCallNamespace(t *testing.T) {
+ tests := []struct {
+ name string
+ call ResponsesFunctionCall
+ want string
+ withTools bool
+ }{
+ {
+ name: "namespaced call",
+ call: ResponsesFunctionCall{
+ Type: "function_call",
+ CallID: "call_1",
+ Namespace: "mcp__codex_apps__notion",
+ Name: "_search",
+ Arguments: `{}`,
+ },
+ want: "mcp__codex_apps__notion_search",
+ },
+ {
+ name: "legacy dotted call",
+ call: ResponsesFunctionCall{
+ Type: "function_call",
+ CallID: "call_1",
+ Name: "mcp__codex_apps__notion._search",
+ Arguments: `{}`,
+ },
+ want: "mcp__codex_apps__notion_search",
+ withTools: true,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ req := ResponsesRequest{
+ Model: "test",
+ Input: ResponsesInput{Items: []ResponsesInputItem{tt.call}},
+ }
+ if tt.withTools {
+ req.Tools = []ResponsesTool{{
+ Type: "namespace",
+ Name: "mcp__codex_apps__notion",
+ Tools: []ResponsesTool{{Type: "function", Name: "_search"}},
+ }}
+ }
+
+ chatReq, err := FromResponsesRequest(req)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got := chatReq.Messages[0].ToolCalls[0].Function.Name; got != tt.want {
+ t.Fatalf("tool name = %q, want %q", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestToResponseRestoresFunctionCallNamespace(t *testing.T) {
+ request := ResponsesRequest{Tools: []ResponsesTool{{
+ Type: "namespace",
+ Name: "mcp__codex_apps__notion",
+ Tools: []ResponsesTool{{Type: "function", Name: "_search"}},
+ }}}
+ response := ToResponse("test", "resp_1", "item_1", api.ChatResponse{
+ Message: api.Message{ToolCalls: []api.ToolCall{{
+ ID: "call_1",
+ Function: api.ToolCallFunction{
+ Name: "mcp__codex_apps__notion_search",
+ Arguments: api.ToolCallFunctionArguments{},
+ },
+ }}},
+ }, request)
+
+ if len(response.Output) != 1 {
+ t.Fatalf("output = %#v, want one function call", response.Output)
+ }
+ if got := response.Output[0].Namespace; got != "mcp__codex_apps__notion" {
+ t.Fatalf("namespace = %q", got)
+ }
+ if got := response.Output[0].Name; got != "_search" {
+ t.Fatalf("name = %q", got)
+ }
+}
+
+func TestToResponseEmitsClientToolSearchCall(t *testing.T) {
+ response := ToResponse("test", "resp_1", "item_1", api.ChatResponse{
+ Message: api.Message{ToolCalls: []api.ToolCall{{
+ ID: "call_search",
+ Function: api.ToolCallFunction{
+ Name: "tool_search",
+ Arguments: testArgs(map[string]any{"query": "orders", "limit": 5}),
+ },
+ }}},
+ }, ResponsesRequest{Tools: []ResponsesTool{{Type: "tool_search", Execution: "client"}}})
+
+ if len(response.Output) != 1 {
+ t.Fatalf("output = %#v", response.Output)
+ }
+ item := response.Output[0]
+ if item.Type != "tool_search_call" || item.CallID != "call_search" || item.Execution != "client" || item.Status != "completed" {
+ t.Fatalf("item = %#v", item)
+ }
+ encoded, err := json.Marshal(item)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var wire struct {
+ Arguments map[string]any `json:"arguments"`
+ }
+ if err := json.Unmarshal(encoded, &wire); err != nil {
+ t.Fatal(err)
+ }
+ if wire.Arguments["query"] != "orders" || wire.Arguments["limit"] != float64(5) {
+ t.Fatalf("arguments = %#v; want an object, not a JSON string", wire.Arguments)
+ }
+}
+
+func TestToResponseRestoresNamespaceFromToolSearchOutput(t *testing.T) {
+ request := ResponsesRequest{
+ Tools: []ResponsesTool{{Type: "tool_search", Execution: "client"}},
+ Input: ResponsesInput{Items: []ResponsesInputItem{ResponsesToolSearchOutput{
+ Type: "tool_search_output",
+ CallID: "call_search",
+ Execution: "client",
+ Tools: []json.RawMessage{
+ json.RawMessage(`{"type":"namespace","name":"mcp__codex_apps__notion","tools":[{"type":"function","name":"_search"}]}`),
+ json.RawMessage(`{"type":"namespace","name":"mcp__openaiDeveloperDocs","tools":[{"type":"function","name":"search_openai_docs"}]}`),
+ },
+ }}},
+ }
+
+ tests := []struct {
+ callName string
+ wantNamespace string
+ wantName string
+ }{
+ {callName: "mcp__openaiDeveloperDocs.search_openai_docs", wantNamespace: "mcp__openaiDeveloperDocs", wantName: "search_openai_docs"},
+ {callName: "mcp__openaiDeveloperDocs:search_openai_docs", wantNamespace: "mcp__openaiDeveloperDocs", wantName: "search_openai_docs"},
+ {callName: "mcp__codex_apps__notion._search", wantNamespace: "mcp__codex_apps__notion", wantName: "_search"},
+ {callName: "mcp__codex_apps__notion:_search", wantNamespace: "mcp__codex_apps__notion", wantName: "_search"},
+ {callName: "mcp__codex_apps__notion_search", wantNamespace: "mcp__codex_apps__notion", wantName: "_search"},
+ {callName: "mcp__other:search", wantName: "mcp__other:search"},
+ }
+ for _, tt := range tests {
+ t.Run(tt.callName, func(t *testing.T) {
+ response := ToResponse("test", "resp_1", "item_1", api.ChatResponse{
+ Message: api.Message{ToolCalls: []api.ToolCall{{
+ ID: "call_1",
+ Function: api.ToolCallFunction{
+ Name: tt.callName,
+ Arguments: api.ToolCallFunctionArguments{},
+ },
+ }}},
+ }, request)
+
+ if len(response.Output) != 1 {
+ t.Fatalf("output = %#v", response.Output)
+ }
+ if got := response.Output[0].Namespace; got != tt.wantNamespace {
+ t.Fatalf("namespace = %q, want %q", got, tt.wantNamespace)
+ }
+ if got := response.Output[0].Name; got != tt.wantName {
+ t.Fatalf("name = %q, want %q", got, tt.wantName)
+ }
+ })
+ }
+}
+
+func TestResponsesStreamConverterRestoresFunctionCallNamespace(t *testing.T) {
+ request := ResponsesRequest{Tools: []ResponsesTool{{
+ Type: "namespace",
+ Name: "mcp__codex_apps__notion",
+ Tools: []ResponsesTool{{Type: "function", Name: "_search"}},
+ }}}
+ converter := NewResponsesStreamConverter("resp_1", "item_1", "test", request)
+ events := converter.Process(api.ChatResponse{Message: api.Message{ToolCalls: []api.ToolCall{{
+ ID: "call_1",
+ Function: api.ToolCallFunction{
+ Name: "mcp__codex_apps__notion_search",
+ Arguments: api.ToolCallFunctionArguments{},
+ },
+ }}}})
+
+ var item map[string]any
+ for _, event := range events {
+ if event.Event == "response.output_item.done" {
+ item = event.Data.(map[string]any)["item"].(map[string]any)
+ }
+ }
+ if item == nil {
+ t.Fatal("missing response.output_item.done")
+ }
+ if got := item["namespace"]; got != "mcp__codex_apps__notion" {
+ t.Fatalf("namespace = %q", got)
+ }
+ if got := item["name"]; got != "_search" {
+ t.Fatalf("name = %q", got)
+ }
+}
+
+func TestResponsesStreamConverterRestoresLegacyDottedFunctionCallNamespace(t *testing.T) {
+ request := ResponsesRequest{Tools: []ResponsesTool{{
+ Type: "namespace",
+ Name: "mcp__codex_apps__notion",
+ Tools: []ResponsesTool{{Type: "function", Name: "_search"}},
+ }}}
+ converter := NewResponsesStreamConverter("resp_1", "item_1", "test", request)
+ events := converter.Process(api.ChatResponse{Message: api.Message{ToolCalls: []api.ToolCall{{
+ ID: "call_1",
+ Function: api.ToolCallFunction{
+ Name: "mcp__codex_apps__notion._search",
+ Arguments: api.ToolCallFunctionArguments{},
+ },
+ }}}})
+
+ var item map[string]any
+ for _, event := range events {
+ if event.Event == "response.output_item.done" {
+ item = event.Data.(map[string]any)["item"].(map[string]any)
+ }
+ }
+ if item == nil {
+ t.Fatal("missing response.output_item.done")
+ }
+ if got := item["namespace"]; got != "mcp__codex_apps__notion" {
+ t.Fatalf("namespace = %q", got)
+ }
+ if got := item["name"]; got != "_search" {
+ t.Fatalf("name = %q", got)
+ }
+}
+
func TestFromResponsesRequest_ReasoningEffort(t *testing.T) {
tests := []struct {
name string
@@ -1398,6 +1907,49 @@ func TestResponsesStreamConverter_ToolCalls(t *testing.T) {
}
}
+func TestResponsesStreamConverter_ToolSearchCall(t *testing.T) {
+ request := ResponsesRequest{Tools: []ResponsesTool{{Type: "tool_search", Execution: "client"}}}
+ converter := NewResponsesStreamConverter("resp_123", "msg_456", "test", request)
+ events := converter.Process(api.ChatResponse{
+ Message: api.Message{ToolCalls: []api.ToolCall{{
+ ID: "call_search",
+ Function: api.ToolCallFunction{
+ Name: "tool_search",
+ Arguments: testArgs(map[string]any{"query": "orders", "limit": 5}),
+ },
+ }}},
+ })
+
+ if len(events) != 4 {
+ t.Fatalf("events = %#v; want created, in_progress, item added, and item done", events)
+ }
+ if events[2].Event != "response.output_item.added" || events[3].Event != "response.output_item.done" {
+ t.Fatalf("events = %#v", events)
+ }
+ item := events[3].Data.(map[string]any)["item"].(map[string]any)
+ if item["type"] != "tool_search_call" || item["call_id"] != "call_search" || item["execution"] != "client" || item["status"] != "completed" {
+ t.Fatalf("item = %#v", item)
+ }
+ encoded, err := json.Marshal(item)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var wire struct {
+ Arguments map[string]any `json:"arguments"`
+ }
+ if err := json.Unmarshal(encoded, &wire); err != nil {
+ t.Fatal(err)
+ }
+ if wire.Arguments["query"] != "orders" || wire.Arguments["limit"] != float64(5) {
+ t.Fatalf("arguments = %#v", wire.Arguments)
+ }
+ for _, event := range events {
+ if event.Event == "response.function_call_arguments.delta" || event.Event == "response.function_call_arguments.done" {
+ t.Fatalf("tool_search_call must not use function_call argument events: %#v", events)
+ }
+ }
+}
+
func TestResponsesStreamConverter_Reasoning(t *testing.T) {
converter := NewResponsesStreamConverter("resp_123", "msg_456", "gpt-oss:20b", ResponsesRequest{})