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)
This commit is contained in:
@@ -79,6 +79,16 @@ import (
|
||||
// is; a name of another kind on a later fragment is left out, the first
|
||||
// one's kept.
|
||||
//
|
||||
// And a legacy function_call that carries nothing — a name and arguments
|
||||
// both empty — is left out too (#4659): WorkBuddy's stream ends a reply
|
||||
// of plain text on a frame with "function_call":{"name":"","arguments":""}
|
||||
// where the contract has null, and a client that checks the shape of
|
||||
// every frame reads the empty object for a legacy call and refuses the
|
||||
// whole turn, so every text reply a tools-carrying request got was a
|
||||
// turn lost (jinccc-ctrl). An empty one runs nowhere, as a nameless call
|
||||
// is no call; a null goes as it came, and a call whose name or arguments
|
||||
// hold something is one a client may still be reading.
|
||||
//
|
||||
// Lines pass whole, as they came, unless one has any of them.
|
||||
type chatTidy struct {
|
||||
buf []byte
|
||||
@@ -98,6 +108,7 @@ var (
|
||||
contentParts = []byte(`"content":[`)
|
||||
contentPartsSpace = []byte(`"content": [`)
|
||||
toolCallsField = []byte(`"tool_calls"`)
|
||||
functionCallField = []byte(`"function_call":{`)
|
||||
)
|
||||
|
||||
// write takes what was read and gives back what to send on: every line
|
||||
@@ -146,7 +157,8 @@ func needsTidy(line []byte) bool {
|
||||
bytes.Contains(line, emptyToolCalls) ||
|
||||
bytes.Contains(line, contentParts) ||
|
||||
bytes.Contains(line, contentPartsSpace) ||
|
||||
bytes.Contains(line, toolCallsField)
|
||||
bytes.Contains(line, toolCallsField) ||
|
||||
bytes.Contains(line, functionCallField)
|
||||
}
|
||||
|
||||
// needs is needsTidy, a finish_reason once a choice made a call, and a
|
||||
@@ -279,7 +291,12 @@ func (t *chatTidy) line(line []byte) []byte {
|
||||
if calls, ok := delta["tool_calls"]; ok && string(calls) == "[]" {
|
||||
delete(delta, "tool_calls")
|
||||
inDelta = true
|
||||
} else if ok {
|
||||
}
|
||||
if fc, ok := delta["function_call"]; ok && emptyFunctionCall(fc) {
|
||||
delete(delta, "function_call")
|
||||
inDelta = true
|
||||
}
|
||||
if calls, ok := delta["tool_calls"]; ok {
|
||||
if t.indexes == nil {
|
||||
t.indexes = streamCallIndexes{}
|
||||
}
|
||||
@@ -351,6 +368,22 @@ func (t *chatTidy) line(line []byte) []byte {
|
||||
return append(append([]byte("data: "), nb...), end...)
|
||||
}
|
||||
|
||||
// emptyFunctionCall reports whether raw is a legacy function_call
|
||||
// carrying nothing: a name and arguments both empty strings or nulls,
|
||||
// either absent (#4659). A null is the contract's own nothing and is not
|
||||
// this; a name or arguments of another kind than a string is not read,
|
||||
// and the call goes as it came.
|
||||
func emptyFunctionCall(raw json.RawMessage) bool {
|
||||
if string(bytes.TrimSpace(raw)) == "null" {
|
||||
return false
|
||||
}
|
||||
var fc struct {
|
||||
Name string `json:"name"`
|
||||
Arguments string `json:"arguments"`
|
||||
}
|
||||
return decodeJSON(raw, &fc) == nil && fc.Name == "" && fc.Arguments == ""
|
||||
}
|
||||
|
||||
// withoutEmptyNames drops the empty "name" on a delta's tool-call
|
||||
// fragments and reports whether any was dropped. A fragment whose
|
||||
// function carries no name at all is left as it is; one with a real name
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user