Files
magpie/internal/gateway/function_call_null_test.go
T
ggbdpq 65fc2efa44 gateway: a Chat legacy function_call carrying nothing is left out (#4659)
jinccc-ctrl on #4659: with tools declared and the model answering in text
alone, WorkBuddy's stream ended on a frame whose function_call was an
empty object where the contract has null, and OpenMausBot, which checks
every frame's shape, read the empty object for a legacy call and refused
the whole turn ("legacy function_call is unsupported; use structured
tool_calls") — so every text reply a tools-carrying request got was a
turn lost, while clients that merge silently never noticed.

An empty function_call — a name and arguments both empty — says nothing
and runs nowhere, so chatTidy leaves it out, as it leaves out an empty
reasoning_content, an empty tool_calls and a nameless call (#1475). A
null goes as it came, and a call whose name or arguments hold something
is one a client may still be reading: both untouched, as is a name or
arguments of another kind than a string.

Run (darwin/arm64, go test ./internal/gateway/ -tags nogui):

  internal/gateway/function_call_null_test.go:32: an empty function_call
  object went out where the contract has null (#4659) — before the fix;
  after, TestStreamedEmptyFunctionCallLeftOut and
  TestStreamedNamedFunctionCallKept pass, the full gateway package
  passes, and gofmt, go vet, -race on the targeted tests and
  linux/windows/darwin builds are clean.

Generated-by: GLM-5.3-Flash (ZCode)
2026-11-01 22:23:51 +08:00

62 lines
3.8 KiB
Go

package gateway
import (
"strings"
"testing"
)
// jinccc-ctrl on #4659: with tools declared and the model answering in
// text alone, WorkBuddy's stream ends on a frame whose function_call is
// an empty object where OpenAI's contract has null. A client that checks
// every frame's shape reads the empty object for a legacy call and
// refuses the whole turn ("legacy function_call is unsupported"), so a
// reply of plain text is a turn lost whenever the client offered tools.
// An empty function_call — a name and arguments both empty — says
// nothing, so it is left out; a null goes as it came.
func TestStreamedEmptyFunctionCallLeftOut(t *testing.T) {
fresh(t)
wire := sse(
`data: {"choices":[{"delta":{"content":"","extra_fields":null,"function_call":null,"refusal":"","role":"assistant","tool_calls":[]},"finish_reason":"","index":0,"logprobs":null}],"created":1793498714,"id":"bda44076918cd5aad55494cabc17cf51","model":"deepseek-v4.1-flash","object":"chat.completion.chunk","usage":null}`,
`data: {"choices":[{"delta":{"content":"你好!","extra_fields":null,"function_call":null,"refusal":"","role":"assistant","tool_calls":[]},"finish_reason":"","index":0,"logprobs":null}],"created":1793498714,"id":"bda44076918cd5aad55494cabc17cf51","model":"deepseek-v4.1-flash","object":"chat.completion.chunk","usage":null}`,
`data: {"choices":[{"delta":{"content":"","extra_fields":null,"function_call":{"name":"","arguments":""},"refusal":"","role":"assistant"},"finish_reason":"stop","index":0,"logprobs":null}],"created":0,"id":"bda44076918cd5aad55494cabc17cf51","model":"deepseek-v4.1-flash","object":"chat.completion.chunk","usage":null}`,
`data: [DONE]`)
a := &scripted{replies: []reply{{200, "text/event-stream", wire}}}
serveOn(t, "a", "ka", []string{"m"}, a)
s := New()
ask := `{"model":"m","stream":true,"messages":[{"role":"user","content":"说声你好,别调工具"}],"tools":[{"type":"function","function":{"name":"get_weather","description":"查天气","parameters":{"type":"object","properties":{"city":{"type":"string"}}}}}],"max_tokens":100}`
code, body := postAs(t, s, "", ask)
if code != 200 || !strings.Contains(body, "你好!") {
t.Fatalf("%d %s", code, body)
}
if strings.Contains(body, `"function_call":{"name":"","arguments":""}`) {
t.Fatalf("an empty function_call object went out where the contract has null (#4659): %s", body)
}
if !strings.Contains(body, `"function_call":null`) {
t.Fatalf("a function_call null did not go as it came: %s", body)
}
}
// A legacy function_call that names a function is a call a client may
// still be reading, its arguments a later fragment's to finish: both go
// as they came, and so does the finish that names one.
func TestStreamedNamedFunctionCallKept(t *testing.T) {
fresh(t)
wire := sse(
`data: {"choices":[{"delta":{"function_call":{"name":"get_weather","arguments":""}},"finish_reason":null,"index":0}]}`,
`data: {"choices":[{"delta":{"function_call":{"name":"","arguments":"{\"city\":\"北京\"}"}},"finish_reason":null,"index":0}]}`,
`data: {"choices":[{"delta":{},"finish_reason":"function_call","index":0}]}`,
`data: [DONE]`)
a := &scripted{replies: []reply{{200, "text/event-stream", wire}}}
serveOn(t, "a", "ka", []string{"m"}, a)
s := New()
ask := `{"model":"m","stream":true,"messages":[{"role":"user","content":"weather?"}],"tools":[{"type":"function","function":{"name":"get_weather","parameters":{"type":"object","properties":{"city":{"type":"string"}}}}}]}`
code, body := postAs(t, s, "", ask)
if code != 200 {
t.Fatalf("%d %s", code, body)
}
if !strings.Contains(body, `"function_call":{"name":"get_weather","arguments":""}`) ||
!strings.Contains(body, `"function_call":{"name":"","arguments":"{\"city\":\"北京\"}"}`) {
t.Fatalf("a legacy call naming a function did not go as it came: %s", body)
}
}