xonecas.io
6312
Ga

go-opencode-ai-zen-sdk / commit b006ac8

fix: tool history in normalized api

xonecas2026-02-19 b006ac888fd772b14e33347b3404c02136ccbe54 parent 60d8777

8 files changed

cmd/test_models/main.go+65−0
@@ -53,6 +53,8 @@ func main() {
5353 {"UnifiedStream", testUnifiedStream},
5454 {"UnifiedCreateNormalized", testUnifiedCreateNormalized},
5555 {"UnifiedStreamNormalized", testUnifiedStreamNormalized},
56+ {"ToolHistory (normalized)", testToolHistoryNormalized},
57+ {"ToolHistory (stream)", testToolHistoryNormalizedStream},
5658 }
5759
5860 for _, s := range sections {
@@ -236,6 +238,69 @@ func testUnifiedStreamNormalized(ctx context.Context, client *zen.Client, modelI
236238 return drainUnifiedStream(modelID, "auto", string(reqBody), eventCh, errCh, err, start)
237239 }
238240
241+// toolHistory returns a pre-built two-turn tool-use conversation:
242+//
243+// user → assistant+tool_call → tool_result → user follow-up
244+//
245+// The model is expected to incorporate the tool result in its reply.
246+func toolHistory() []zen.NormalizedMessage {
247+ return []zen.NormalizedMessage{
248+ {Role: "user", Content: "What is 3 + 4?"},
249+ {
250+ Role: "assistant",
251+ ToolCalls: []zen.NormalizedToolCall{
252+ {ID: "call_1", Name: "add", Arguments: json.RawMessage(`{"a":3,"b":4}`)},
253+ },
254+ },
255+ {Role: "tool", Content: "7", ToolCallID: "call_1"},
256+ {Role: "user", Content: "Thanks, what is the result doubled?"},
257+ }
258+}
259+
260+func testToolHistoryNormalized(ctx context.Context, client *zen.Client, modelID string) testResult {
261+ req := zen.NormalizedRequest{
262+ Model: modelID,
263+ System: "You are a calculator assistant. When given a tool result, use it to answer the user.",
264+ Messages: toolHistory(),
265+ Tools: []zen.NormalizedTool{
266+ {
267+ Name: "add",
268+ Description: "Adds two numbers",
269+ Parameters: json.RawMessage(`{"type":"object","properties":{"a":{"type":"number"},"b":{"type":"number"}},"required":["a","b"]}`),
270+ },
271+ },
272+ }
273+ start := time.Now()
274+ reqBody, _ := json.Marshal(req)
275+
276+ resp, err := client.UnifiedCreateNormalized(ctx, req)
277+ if err != nil {
278+ return makeResult(modelID, "auto", false, string(reqBody), "", err, time.Since(start))
279+ }
280+ return makeResult(modelID, string(resp.Endpoint), false, string(reqBody), truncate(string(resp.Body), 100), nil, time.Since(start))
281+}
282+
283+func testToolHistoryNormalizedStream(ctx context.Context, client *zen.Client, modelID string) testResult {
284+ req := zen.NormalizedRequest{
285+ Model: modelID,
286+ System: "You are a calculator assistant. When given a tool result, use it to answer the user.",
287+ Messages: toolHistory(),
288+ Tools: []zen.NormalizedTool{
289+ {
290+ Name: "add",
291+ Description: "Adds two numbers",
292+ Parameters: json.RawMessage(`{"type":"object","properties":{"a":{"type":"number"},"b":{"type":"number"}},"required":["a","b"]}`),
293+ },
294+ },
295+ Stream: true,
296+ }
297+ start := time.Now()
298+ reqBody, _ := json.Marshal(req)
299+
300+ eventCh, errCh, err := client.UnifiedStreamNormalized(ctx, req)
301+ return drainUnifiedStream(modelID, "auto", string(reqBody), eventCh, errCh, err, start)
302+}
303+
239304 // drainStream consumes a StreamEvent channel and builds a testResult.
240305 func drainStream(modelID, endpoint, reqBody string, eventCh <-chan zen.StreamEvent, errCh <-chan error, initErr error, start time.Time) testResult {
241306 if initErr != nil {
normalized.go+138−8
@@ -31,9 +31,18 @@ type NormalizedReasoning struct {
3131 BudgetTokens int
3232 }
3333
34+type NormalizedToolCall struct {
35+ ID string
36+ Name string
37+ Arguments json.RawMessage
38+}
39+
3440 type NormalizedMessage struct {
35- Role string
36- Content string
41+ Role string
42+ Content string
43+ ToolCalls []NormalizedToolCall // set on assistant messages that invoked tools
44+ ToolCallID string // set on tool-result messages (role "tool")
45+ FunctionName string // set on tool-result messages (role "tool"): name of the called function; required by Gemini
3746 }
3847
3948 type NormalizedRequest struct {
@@ -63,11 +72,35 @@ func (r NormalizedRequest) ToResponsesRequest() (*ResponsesRequest, error) {
6372 if len(r.Messages) == 0 {
6473 req.Input = ""
6574 } else {
66- messages := make([]ResponsesInputMessage, 0, len(r.Messages))
75+ items := make([]any, 0, len(r.Messages))
6776 for _, m := range r.Messages {
68- messages = append(messages, ResponsesInputMessage(m))
77+ // Tool result message → function_call_output item.
78+ if strings.ToLower(strings.TrimSpace(m.Role)) == "tool" {
79+ items = append(items, ResponsesFunctionCallOutput{
80+ Type: "function_call_output",
81+ CallID: m.ToolCallID,
82+ Output: m.Content,
83+ })
84+ continue
85+ }
86+ // Assistant message with tool calls → optional text item + function_call items.
87+ if strings.ToLower(strings.TrimSpace(m.Role)) == "assistant" && len(m.ToolCalls) > 0 {
88+ if strings.TrimSpace(m.Content) != "" {
89+ items = append(items, ResponsesInputMessage{Role: m.Role, Content: m.Content})
90+ }
91+ for _, tc := range m.ToolCalls {
92+ items = append(items, ResponsesFunctionCall{
93+ Type: "function_call",
94+ CallID: tc.ID,
95+ Name: tc.Name,
96+ Arguments: string(tc.Arguments),
97+ })
98+ }
99+ continue
100+ }
101+ items = append(items, ResponsesInputMessage{Role: m.Role, Content: m.Content})
69102 }
70- req.Input = messages
103+ req.Input = items
71104 }
72105
73106 if r.Reasoning != nil && r.Reasoning.Effort != "" {
@@ -78,8 +111,10 @@ func (r NormalizedRequest) ToResponsesRequest() (*ResponsesRequest, error) {
78111 req.Tools = make([]ResponsesTool, 0, len(r.Tools))
79112 for _, t := range r.Tools {
80113 req.Tools = append(req.Tools, ResponsesTool{
81- Type: "function",
82- Function: ResponsesToolFunction(t),
114+ Type: "function",
115+ Name: t.Name,
116+ Description: t.Description,
117+ Parameters: t.Parameters,
83118 })
84119 }
85120 }
@@ -101,7 +136,21 @@ func (r NormalizedRequest) ToChatCompletionsRequest() (*ChatCompletionsRequest,
101136 messages = append(messages, ChatMessage{Role: "system", Content: r.System})
102137 }
103138 for _, m := range r.Messages {
104- messages = append(messages, ChatMessage(m))
139+ cm := ChatMessage{Role: m.Role, Content: m.Content, ToolCallID: m.ToolCallID}
140+ if len(m.ToolCalls) > 0 {
141+ cm.ToolCalls = make([]ChatMessageToolCall, 0, len(m.ToolCalls))
142+ for _, tc := range m.ToolCalls {
143+ cm.ToolCalls = append(cm.ToolCalls, ChatMessageToolCall{
144+ ID: tc.ID,
145+ Type: "function",
146+ Function: ChatMessageToolCallFunc{
147+ Name: tc.Name,
148+ Arguments: string(tc.Arguments),
149+ },
150+ })
151+ }
152+ }
153+ messages = append(messages, cm)
105154 }
106155
107156 req := &ChatCompletionsRequest{
@@ -196,12 +245,64 @@ func (r NormalizedRequest) ToMessagesRequest() (*MessagesRequest, error) {
196245 }
197246
198247 func (r NormalizedRequest) ToGeminiRequest() (*GeminiRequest, error) {
248+ // Build a call-id → function-name index from all assistant tool calls so
249+ // that tool-result messages can have their FunctionName derived
250+ // automatically when the caller did not set it explicitly.
251+ callIDToName := make(map[string]string)
252+ for _, m := range r.Messages {
253+ if strings.ToLower(strings.TrimSpace(m.Role)) == "assistant" {
254+ for _, tc := range m.ToolCalls {
255+ if tc.ID != "" && tc.Name != "" {
256+ callIDToName[tc.ID] = tc.Name
257+ }
258+ }
259+ }
260+ }
261+
199262 contents := make([]GeminiContent, 0, len(r.Messages))
200263 for _, m := range r.Messages {
201264 role := strings.ToLower(strings.TrimSpace(m.Role))
265+
266+ // Tool result: role "tool" → user turn with functionResponse parts.
267+ if role == "tool" {
268+ name := m.FunctionName
269+ if name == "" {
270+ name = callIDToName[m.ToolCallID]
271+ }
272+ if name == "" {
273+ return nil, errors.New("zen: tool result message is missing FunctionName (required by Gemini)")
274+ }
275+ contents = append(contents, GeminiContent{
276+ Role: "user",
277+ Parts: []GeminiPart{{
278+ FunctionResponse: &GeminiFunctionResponse{
279+ Name: name,
280+ Response: GeminiFunctionResponseBody{Output: m.Content},
281+ },
282+ }},
283+ })
284+ continue
285+ }
286+
202287 if role == "assistant" {
203288 role = "model"
204289 }
290+
291+ // Assistant message with tool calls → model turn with functionCall parts.
292+ if role == "model" && len(m.ToolCalls) > 0 {
293+ parts := make([]GeminiPart, 0, len(m.ToolCalls))
294+ for _, tc := range m.ToolCalls {
295+ parts = append(parts, GeminiPart{
296+ FunctionCall: &GeminiFunctionCall{
297+ Name: tc.Name,
298+ Args: tc.Arguments,
299+ },
300+ })
301+ }
302+ contents = append(contents, GeminiContent{Role: role, Parts: parts})
303+ continue
304+ }
305+
205306 contents = append(contents, GeminiContent{
206307 Role: role,
207308 Parts: []GeminiPart{{Text: m.Content}},
@@ -315,6 +416,35 @@ func normalizeAnthropicMessages(system string, msgs []NormalizedMessage) (string
315416 continue
316417 }
317418
419+ // Tool result: role "tool" maps to a "user" message with a tool_result block.
420+ if role == "tool" {
421+ out = append(out, AnthropicMessage{
422+ Role: "user",
423+ Content: []AnthropicContentBlock{
424+ {Type: "tool_result", ToolUseID: m.ToolCallID, Content: m.Content},
425+ },
426+ })
427+ continue
428+ }
429+
430+ // Assistant message with tool calls: emit content blocks of type "tool_use".
431+ if role == "assistant" && len(m.ToolCalls) > 0 {
432+ blocks := make([]AnthropicContentBlock, 0, len(m.ToolCalls)+1)
433+ if strings.TrimSpace(m.Content) != "" {
434+ blocks = append(blocks, AnthropicContentBlock{Type: "text", Text: m.Content})
435+ }
436+ for _, tc := range m.ToolCalls {
437+ blocks = append(blocks, AnthropicContentBlock{
438+ Type: "tool_use",
439+ ID: tc.ID,
440+ Name: tc.Name,
441+ Input: tc.Arguments,
442+ })
443+ }
444+ out = append(out, AnthropicMessage{Role: "assistant", Content: blocks})
445+ continue
446+ }
447+
318448 out = append(out, AnthropicMessage{
319449 Role: role,
320450 Content: m.Content,
normalized_test.go+277−1
@@ -5,6 +5,282 @@ import (
55 "testing"
66 )
77
8+// toolHistory is a two-turn tool-use history shared across all endpoint tests:
9+//
10+// user → assistant+tool_calls → tool result → assistant
11+var toolHistory = []NormalizedMessage{
12+ {Role: "user", Content: "What's the weather in Paris?"},
13+ {
14+ Role: "assistant",
15+ Content: "",
16+ ToolCalls: []NormalizedToolCall{
17+ {ID: "call_1", Name: "get_weather", Arguments: json.RawMessage(`{"city":"Paris"}`)},
18+ },
19+ },
20+ // FunctionName is intentionally omitted: ToGeminiRequest derives it from
21+ // the preceding assistant message's ToolCalls via ToolCallID.
22+ {Role: "tool", Content: "Sunny, 22°C", ToolCallID: "call_1"},
23+ {Role: "assistant", Content: "The weather in Paris is sunny and 22°C."},
24+}
25+
26+func TestNormalizedToResponsesToolHistory(t *testing.T) {
27+ req := NormalizedRequest{
28+ Model: "gpt-5.2-codex",
29+ Messages: toolHistory,
30+ }
31+
32+ resp, err := req.ToResponsesRequest()
33+ if err != nil {
34+ t.Fatalf("ToResponsesRequest error: %v", err)
35+ }
36+
37+ items, ok := resp.Input.([]any)
38+ if !ok {
39+ t.Fatalf("Input is not []any, got %T", resp.Input)
40+ }
41+ if len(items) != 4 {
42+ t.Fatalf("expected 4 input items, got %d", len(items))
43+ }
44+
45+ // [0] user message
46+ userMsg, ok := items[0].(ResponsesInputMessage)
47+ if !ok || userMsg.Role != "user" {
48+ t.Fatalf("item[0] should be user ResponsesInputMessage, got %T %+v", items[0], items[0])
49+ }
50+
51+ // [1] function_call item for assistant tool call
52+ fc, ok := items[1].(ResponsesFunctionCall)
53+ if !ok {
54+ t.Fatalf("item[1] should be ResponsesFunctionCall, got %T %+v", items[1], items[1])
55+ }
56+ if fc.Type != "function_call" {
57+ t.Fatalf("item[1] type: want function_call, got %q", fc.Type)
58+ }
59+ if fc.CallID != "call_1" {
60+ t.Fatalf("item[1] call_id: want call_1, got %q", fc.CallID)
61+ }
62+ if fc.Name != "get_weather" {
63+ t.Fatalf("item[1] name: want get_weather, got %q", fc.Name)
64+ }
65+
66+ // [2] function_call_output item for tool result
67+ fco, ok := items[2].(ResponsesFunctionCallOutput)
68+ if !ok {
69+ t.Fatalf("item[2] should be ResponsesFunctionCallOutput, got %T %+v", items[2], items[2])
70+ }
71+ if fco.Type != "function_call_output" {
72+ t.Fatalf("item[2] type: want function_call_output, got %q", fco.Type)
73+ }
74+ if fco.CallID != "call_1" {
75+ t.Fatalf("item[2] call_id: want call_1, got %q", fco.CallID)
76+ }
77+ if fco.Output != "Sunny, 22°C" {
78+ t.Fatalf("item[2] output: want %q, got %q", "Sunny, 22°C", fco.Output)
79+ }
80+
81+ // [3] final assistant message
82+ assistantMsg, ok := items[3].(ResponsesInputMessage)
83+ if !ok || assistantMsg.Role != "assistant" {
84+ t.Fatalf("item[3] should be assistant ResponsesInputMessage, got %T %+v", items[3], items[3])
85+ }
86+}
87+
88+func TestNormalizedToChatCompletionsToolHistory(t *testing.T) {
89+ req := NormalizedRequest{
90+ Model: "gpt-5.2",
91+ Messages: toolHistory,
92+ }
93+
94+ chat, err := req.ToChatCompletionsRequest()
95+ if err != nil {
96+ t.Fatalf("ToChatCompletionsRequest error: %v", err)
97+ }
98+
99+ // No system message injected (System is empty), so 4 messages.
100+ if len(chat.Messages) != 4 {
101+ t.Fatalf("expected 4 messages, got %d", len(chat.Messages))
102+ }
103+
104+ // [0] user
105+ if chat.Messages[0].Role != "user" {
106+ t.Fatalf("msg[0] role: want user, got %q", chat.Messages[0].Role)
107+ }
108+
109+ // [1] assistant with tool_calls
110+ asst := chat.Messages[1]
111+ if asst.Role != "assistant" {
112+ t.Fatalf("msg[1] role: want assistant, got %q", asst.Role)
113+ }
114+ if len(asst.ToolCalls) != 1 {
115+ t.Fatalf("msg[1] tool_calls length: want 1, got %d", len(asst.ToolCalls))
116+ }
117+ if asst.ToolCalls[0].ID != "call_1" {
118+ t.Fatalf("msg[1] tool_calls[0].ID: want call_1, got %q", asst.ToolCalls[0].ID)
119+ }
120+ if asst.ToolCalls[0].Type != "function" {
121+ t.Fatalf("msg[1] tool_calls[0].Type: want function, got %q", asst.ToolCalls[0].Type)
122+ }
123+ if asst.ToolCalls[0].Function.Name != "get_weather" {
124+ t.Fatalf("msg[1] tool_calls[0].Function.Name: want get_weather, got %q", asst.ToolCalls[0].Function.Name)
125+ }
126+ if asst.ToolCalls[0].Function.Arguments != `{"city":"Paris"}` {
127+ t.Fatalf("msg[1] tool_calls[0].Function.Arguments: want %q, got %q", `{"city":"Paris"}`, asst.ToolCalls[0].Function.Arguments)
128+ }
129+
130+ // [2] tool result
131+ toolMsg := chat.Messages[2]
132+ if toolMsg.Role != "tool" {
133+ t.Fatalf("msg[2] role: want tool, got %q", toolMsg.Role)
134+ }
135+ if toolMsg.ToolCallID != "call_1" {
136+ t.Fatalf("msg[2] tool_call_id: want call_1, got %q", toolMsg.ToolCallID)
137+ }
138+ if toolMsg.Content != "Sunny, 22°C" {
139+ t.Fatalf("msg[2] content: want %q, got %q", "Sunny, 22°C", toolMsg.Content)
140+ }
141+
142+ // [3] final assistant
143+ if chat.Messages[3].Role != "assistant" {
144+ t.Fatalf("msg[3] role: want assistant, got %q", chat.Messages[3].Role)
145+ }
146+}
147+
148+func TestNormalizedToMessagesToolHistory(t *testing.T) {
149+ req := NormalizedRequest{
150+ Model: "claude-sonnet-4-6",
151+ Messages: toolHistory,
152+ }
153+
154+ msg, err := req.ToMessagesRequest()
155+ if err != nil {
156+ t.Fatalf("ToMessagesRequest error: %v", err)
157+ }
158+
159+ if len(msg.Messages) != 4 {
160+ t.Fatalf("expected 4 messages, got %d", len(msg.Messages))
161+ }
162+
163+ // [0] user plain text
164+ m0 := msg.Messages[0]
165+ if m0.Role != "user" {
166+ t.Fatalf("msg[0] role: want user, got %q", m0.Role)
167+ }
168+ if m0.Content.(string) != "What's the weather in Paris?" {
169+ t.Fatalf("msg[0] content mismatch")
170+ }
171+
172+ // [1] assistant with tool_use block
173+ m1 := msg.Messages[1]
174+ if m1.Role != "assistant" {
175+ t.Fatalf("msg[1] role: want assistant, got %q", m1.Role)
176+ }
177+ blocks1, ok := m1.Content.([]AnthropicContentBlock)
178+ if !ok {
179+ t.Fatalf("msg[1] content should be []AnthropicContentBlock, got %T", m1.Content)
180+ }
181+ if len(blocks1) != 1 {
182+ t.Fatalf("msg[1] content blocks: want 1, got %d", len(blocks1))
183+ }
184+ if blocks1[0].Type != "tool_use" {
185+ t.Fatalf("msg[1] block[0].Type: want tool_use, got %q", blocks1[0].Type)
186+ }
187+ if blocks1[0].ID != "call_1" {
188+ t.Fatalf("msg[1] block[0].ID: want call_1, got %q", blocks1[0].ID)
189+ }
190+ if blocks1[0].Name != "get_weather" {
191+ t.Fatalf("msg[1] block[0].Name: want get_weather, got %q", blocks1[0].Name)
192+ }
193+
194+ // [2] user with tool_result block (tool role → user message)
195+ m2 := msg.Messages[2]
196+ if m2.Role != "user" {
197+ t.Fatalf("msg[2] role: want user (tool_result), got %q", m2.Role)
198+ }
199+ blocks2, ok := m2.Content.([]AnthropicContentBlock)
200+ if !ok {
201+ t.Fatalf("msg[2] content should be []AnthropicContentBlock, got %T", m2.Content)
202+ }
203+ if len(blocks2) != 1 || blocks2[0].Type != "tool_result" {
204+ t.Fatalf("msg[2] should have 1 tool_result block, got %+v", blocks2)
205+ }
206+ if blocks2[0].ToolUseID != "call_1" {
207+ t.Fatalf("msg[2] block[0].ToolUseID: want call_1, got %q", blocks2[0].ToolUseID)
208+ }
209+ if blocks2[0].Content != "Sunny, 22°C" {
210+ t.Fatalf("msg[2] block[0].Content: want %q, got %q", "Sunny, 22°C", blocks2[0].Content)
211+ }
212+
213+ // [3] final assistant plain text
214+ m3 := msg.Messages[3]
215+ if m3.Role != "assistant" {
216+ t.Fatalf("msg[3] role: want assistant, got %q", m3.Role)
217+ }
218+ if m3.Content.(string) != "The weather in Paris is sunny and 22°C." {
219+ t.Fatalf("msg[3] content mismatch")
220+ }
221+}
222+
223+func TestNormalizedToGeminiToolHistory(t *testing.T) {
224+ req := NormalizedRequest{
225+ Model: "gemini-3-pro",
226+ Messages: toolHistory,
227+ }
228+
229+ gem, err := req.ToGeminiRequest()
230+ if err != nil {
231+ t.Fatalf("ToGeminiRequest error: %v", err)
232+ }
233+
234+ if len(gem.Contents) != 4 {
235+ t.Fatalf("expected 4 contents, got %d", len(gem.Contents))
236+ }
237+
238+ // [0] user plain text
239+ c0 := gem.Contents[0]
240+ if c0.Role != "user" {
241+ t.Fatalf("content[0] role: want user, got %q", c0.Role)
242+ }
243+ if len(c0.Parts) != 1 || c0.Parts[0].Text != "What's the weather in Paris?" {
244+ t.Fatalf("content[0] parts mismatch: %+v", c0.Parts)
245+ }
246+
247+ // [1] model with functionCall part
248+ c1 := gem.Contents[1]
249+ if c1.Role != "model" {
250+ t.Fatalf("content[1] role: want model, got %q", c1.Role)
251+ }
252+ if len(c1.Parts) != 1 || c1.Parts[0].FunctionCall == nil {
253+ t.Fatalf("content[1] should have 1 functionCall part, got %+v", c1.Parts)
254+ }
255+ if c1.Parts[0].FunctionCall.Name != "get_weather" {
256+ t.Fatalf("content[1] functionCall.Name: want get_weather, got %q", c1.Parts[0].FunctionCall.Name)
257+ }
258+
259+ // [2] user with functionResponse part
260+ c2 := gem.Contents[2]
261+ if c2.Role != "user" {
262+ t.Fatalf("content[2] role: want user, got %q", c2.Role)
263+ }
264+ if len(c2.Parts) != 1 || c2.Parts[0].FunctionResponse == nil {
265+ t.Fatalf("content[2] should have 1 functionResponse part, got %+v", c2.Parts)
266+ }
267+ if c2.Parts[0].FunctionResponse.Name != "get_weather" {
268+ t.Fatalf("content[2] functionResponse.Name: want get_weather, got %q", c2.Parts[0].FunctionResponse.Name)
269+ }
270+ if c2.Parts[0].FunctionResponse.Response.Output != "Sunny, 22°C" {
271+ t.Fatalf("content[2] functionResponse.Response.Output: want %q, got %q", "Sunny, 22°C", c2.Parts[0].FunctionResponse.Response.Output)
272+ }
273+
274+ // [3] final model plain text
275+ c3 := gem.Contents[3]
276+ if c3.Role != "model" {
277+ t.Fatalf("content[3] role: want model, got %q", c3.Role)
278+ }
279+ if len(c3.Parts) != 1 || c3.Parts[0].Text != "The weather in Paris is sunny and 22°C." {
280+ t.Fatalf("content[3] parts mismatch: %+v", c3.Parts)
281+ }
282+}
283+
8284 func TestNormalizedToResponses(t *testing.T) {
9285 req := NormalizedRequest{
10286 Model: "gpt-5.2-codex",
@@ -32,7 +308,7 @@ func TestNormalizedToResponses(t *testing.T) {
32308 if resp.Reasoning == nil || resp.Reasoning.Effort != "low" {
33309 t.Fatalf("reasoning not mapped")
34310 }
35- if len(resp.Tools) != 1 || resp.Tools[0].Function.Name != "tool" {
311+ if len(resp.Tools) != 1 || resp.Tools[0].Name != "tool" {
36312 t.Fatalf("tools not mapped")
37313 }
38314 choice, ok := resp.ToolChoice.(map[string]any)
types_chat.go+15−2
@@ -14,9 +14,22 @@ type ChatCompletionsRequest struct {
1414 Extra map[string]any
1515 }
1616
17+type ChatMessageToolCall struct {
18+ ID string `json:"id"`
19+ Type string `json:"type"`
20+ Function ChatMessageToolCallFunc `json:"function"`
21+}
22+
23+type ChatMessageToolCallFunc struct {
24+ Name string `json:"name"`
25+ Arguments string `json:"arguments"`
26+}
27+
1728 type ChatMessage struct {
18- Role string `json:"role"`
19- Content string `json:"content"`
29+ Role string `json:"role"`
30+ Content string `json:"content,omitempty"`
31+ ToolCalls []ChatMessageToolCall `json:"tool_calls,omitempty"`
32+ ToolCallID string `json:"tool_call_id,omitempty"`
2033 }
2134
2235 type ChatReasoning struct {
types_gemini.go+31−1
@@ -17,8 +17,38 @@ type GeminiContent struct {
1717 Parts []GeminiPart `json:"parts"`
1818 }
1919
20+type GeminiFunctionCall struct {
21+ Name string `json:"name"`
22+ Args json.RawMessage `json:"args,omitempty"`
23+}
24+
25+type GeminiFunctionResponse struct {
26+ Name string `json:"name"`
27+ Response GeminiFunctionResponseBody `json:"response"`
28+}
29+
30+// GeminiFunctionResponseBody is the value of functionResponse.response sent to
31+// Gemini. When Content is non-nil it is used verbatim (allowing callers to
32+// provide structured JSON). Otherwise a plain {"output": <Output>} object is
33+// marshalled, which is the simplest form accepted by the API.
34+type GeminiFunctionResponseBody struct {
35+ Output string `json:"output,omitempty"`
36+ Content json.RawMessage `json:"-"` // if set, marshalled as the entire response object
37+}
38+
39+func (b GeminiFunctionResponseBody) MarshalJSON() ([]byte, error) {
40+ if len(b.Content) > 0 {
41+ return b.Content, nil
42+ }
43+ return json.Marshal(struct {
44+ Output string `json:"output"`
45+ }{Output: b.Output})
46+}
47+
2048 type GeminiPart struct {
21- Text string `json:"text,omitempty"`
49+ Text string `json:"text,omitempty"`
50+ FunctionCall *GeminiFunctionCall `json:"functionCall,omitempty"`
51+ FunctionResponse *GeminiFunctionResponse `json:"functionResponse,omitempty"`
2252 }
2353
2454 type GeminiTool struct {
types_messages.go+11−1
@@ -15,9 +15,19 @@ type MessagesRequest struct {
1515 Extra map[string]any
1616 }
1717
18+type AnthropicContentBlock struct {
19+ Type string `json:"type"`
20+ Text string `json:"text,omitempty"`
21+ ID string `json:"id,omitempty"`
22+ Name string `json:"name,omitempty"`
23+ Input json.RawMessage `json:"input,omitempty"`
24+ ToolUseID string `json:"tool_use_id,omitempty"`
25+ Content string `json:"content,omitempty"`
26+}
27+
1828 type AnthropicMessage struct {
1929 Role string `json:"role"`
20- Content string `json:"content"`
30+ Content any `json:"content"` // string or []AnthropicContentBlock
2131 }
2232
2333 type AnthropicTool struct {
types_responses.go+21−5
@@ -19,12 +19,11 @@ type ResponsesReasoning struct {
1919 Effort string `json:"effort,omitempty"`
2020 }
2121
22+// ResponsesTool defines a function tool in the Responses API flat format:
23+// {"type":"function","name":...,"description":...,"parameters":...}
24+// Note: this differs from Chat Completions which nests these under a "function" key.
2225 type ResponsesTool struct {
23- Type string `json:"type"`
24- Function ResponsesToolFunction `json:"function"`
25-}
26-
27-type ResponsesToolFunction struct {
26+ Type string `json:"type"`
2827 Name string `json:"name"`
2928 Description string `json:"description,omitempty"`
3029 Parameters json.RawMessage `json:"parameters,omitempty"`
@@ -35,6 +34,23 @@ type ResponsesInputMessage struct {
3534 Content string `json:"content"`
3635 }
3736
37+// ResponsesFunctionCall represents an assistant function-call output item in
38+// the Responses API input array.
39+type ResponsesFunctionCall struct {
40+ Type string `json:"type"`
41+ CallID string `json:"call_id"`
42+ Name string `json:"name"`
43+ Arguments string `json:"arguments"`
44+}
45+
46+// ResponsesFunctionCallOutput represents a tool result item in the Responses
47+// API input array.
48+type ResponsesFunctionCallOutput struct {
49+ Type string `json:"type"`
50+ CallID string `json:"call_id"`
51+ Output string `json:"output"`
52+}
53+
3854 func (r ResponsesRequest) MarshalJSON() ([]byte, error) {
3955 base := map[string]any{
4056 "model": r.Model,
unified_test.go+10−2
@@ -6,6 +6,7 @@ import (
66 "io"
77 "net/http"
88 "net/http/httptest"
9+ "strings"
910 "sync"
1011 "testing"
1112 )
@@ -29,6 +30,13 @@ func TestUnifiedCreateNormalizedRouting(t *testing.T) {
2930 captures = append(captures, requestCapture{path: r.URL.Path, body: body})
3031 mu.Unlock()
3132
33+ // Gemini requests go through startStream (SSE), so respond with a
34+ // minimal SSE frame; all other endpoints return plain JSON.
35+ if strings.HasPrefix(r.URL.Path, "/models/") {
36+ w.Header().Set("Content-Type", "text/event-stream")
37+ _, _ = w.Write([]byte("data: {\"ok\":true}\n\n"))
38+ return
39+ }
3240 w.Header().Set("Content-Type", "application/json")
3341 _, _ = w.Write([]byte(`{"ok":true}`))
3442 }))
@@ -106,8 +114,8 @@ func TestUnifiedCreateNormalizedRouting(t *testing.T) {
106114 t.Fatalf("messages payload missing messages")
107115 }
108116
109- if captures[2].path != "/models/gemini-3-pro" {
110- t.Fatalf("expected /models/gemini-3-pro, got %s", captures[2].path)
117+ if !strings.HasPrefix(captures[2].path, "/models/gemini-3-pro") {
118+ t.Fatalf("expected /models/gemini-3-pro path, got %s", captures[2].path)
111119 }
112120 if _, ok := captures[2].body["contents"]; !ok {
113121 t.Fatalf("gemini payload missing contents")