3 Commits
Author SHA1 Message Date
m1saka f988c47fec Fix silent data loss in function_call arguments/output non-string JSON parsing
build / build (push) Successful in 2m29s
responsesInputToMessages unmarshaled function_call.arguments and
function_call_output.output as bare strings, silently dropping the
value when it arrived as an object or content-parts array. This
caused the model to lose tool-call context in multi-turn
conversations, increasing the likelihood of malformed tool-call JSON.

Add rawJSONToString (re-encodes non-string values as JSON strings)
and outputToString (extracts text from content-parts arrays, re-encodes
other non-string values). Add 3 regression tests covering object
arguments, array output, and object output.
2026-08-23 14:15:01 +08:00
m1saka f51ec09a09 gofmt: remove trailing blank lines in client.go
build / build (push) Successful in 2m31s
2026-08-21 15:25:06 +08:00
m1saka b9a3f8d5cd Mirror Zhanlu client: GLM tool_stream, shared v4 UUID request id, drop chat state:ERROR
build / build (push) Successful in 2m26s
2026-08-21 14:27:37 +08:00
6 changed files with 170 additions and 24 deletions
+55 -6
View File
@@ -3,6 +3,7 @@ package openai
import (
"encoding/json"
"errors"
"strings"
)
// --- Responses API request parsing & conversion ---
@@ -194,9 +195,7 @@ func responsesInputToMessages(input json.RawMessage) ([]map[string]any, error) {
inner["name"] = name
}
if v, ok := item["arguments"]; ok {
var args string
_ = json.Unmarshal(v, &args)
inner["arguments"] = args
inner["arguments"] = rawJSONToString(v)
}
if v, ok := item["call_id"]; ok {
var id string
@@ -215,9 +214,7 @@ func responsesInputToMessages(input json.RawMessage) ([]map[string]any, error) {
msg["tool_call_id"] = id
}
if v, ok := item["output"]; ok {
var out string
_ = json.Unmarshal(v, &out)
msg["content"] = out
msg["content"] = outputToString(v)
}
messages = append(messages, msg)
default:
@@ -279,6 +276,58 @@ func convertContentParts(raw json.RawMessage) any {
return result
}
// rawJSONToString converts a json.RawMessage to a string. If the value is
// already a JSON string, it is used directly. Any other JSON value (object,
// array, number, bool) is re-encoded as a JSON string so it can populate
// fields that require a string, such as tool_calls[].function.arguments.
// This prevents silent data loss when a field arrives as a non-string type.
func rawJSONToString(v json.RawMessage) string {
var s string
if json.Unmarshal(v, &s) == nil {
return s
}
var anyValue any
if json.Unmarshal(v, &anyValue) == nil {
if b, err := json.Marshal(anyValue); err == nil {
return string(b)
}
}
return string(v) // last resort: raw bytes
}
// outputToString converts a function_call_output "output" value to a string
// for the Chat Completions tool message content. The output may be:
// - a plain string (used directly)
// - an array of content parts (text extracted and concatenated)
// - any other JSON value (re-encoded as a JSON string)
func outputToString(v json.RawMessage) string {
var s string
if json.Unmarshal(v, &s) == nil {
return s
}
// Try array of content parts — extract text from each part.
var parts []map[string]any
if json.Unmarshal(v, &parts) == nil {
var sb strings.Builder
for _, p := range parts {
if t, _ := p["text"].(string); t != "" {
sb.WriteString(t)
}
}
if sb.Len() > 0 {
return sb.String()
}
}
// Fallback: re-encode as a JSON string.
var anyValue any
if json.Unmarshal(v, &anyValue) == nil {
if b, err := json.Marshal(anyValue); err == nil {
return string(b)
}
}
return string(v)
}
// translateResponsesTools converts tools from the Responses API flat format
// to the Chat Completions nested {type:"function",function:{…}} format.
func translateResponsesTools(raw json.RawMessage) (json.RawMessage, error) {
+84
View File
@@ -191,6 +191,90 @@ func TestParseResponsesRequest_FunctionCallInput(t *testing.T) {
}
}
// TestParseResponsesRequest_FunctionCallObjectArguments verifies that when
// function_call.arguments arrives as a JSON object (not a string), it is
// re-encoded as a JSON string instead of being silently dropped.
func TestParseResponsesRequest_FunctionCallObjectArguments(t *testing.T) {
body := `{"model":"GLM-4.7","input":[
{"type":"function_call","call_id":"call_456","name":"task","arguments":{"operation":"create","summary":"test"}}
]}`
req, err := ParseResponsesRequest([]byte(body))
if err != nil {
t.Fatal(err)
}
if len(req.Messages) != 1 {
t.Fatalf("messages = %d items", len(req.Messages))
}
tc, _ := req.Messages[0]["tool_calls"].([]any)
if len(tc) != 1 {
t.Fatalf("tool_calls = %v", req.Messages[0]["tool_calls"])
}
fn, _ := tc[0].(map[string]any)["function"].(map[string]any)
args, _ := fn["arguments"].(string)
if args == "" {
t.Fatalf("arguments was dropped (empty)")
}
// The re-encoded string must be valid JSON containing the original fields.
var parsed map[string]any
if err := json.Unmarshal([]byte(args), &parsed); err != nil {
t.Fatalf("arguments not valid JSON: %v", err)
}
if parsed["operation"] != "create" {
t.Fatalf("operation = %v", parsed["operation"])
}
}
// TestParseResponsesRequest_FunctionCallOutputArray verifies that when
// function_call_output.output arrives as an array of content parts, the
// text is extracted instead of being silently dropped.
func TestParseResponsesRequest_FunctionCallOutputArray(t *testing.T) {
body := `{"model":"GLM-4.7","input":[
{"type":"function_call","call_id":"call_789","name":"get_weather","arguments":"{\"city\":\"NYC\"}"},
{"type":"function_call_output","call_id":"call_789","output":[{"type":"output_text","text":"sunny 72F"}]}
]}`
req, err := ParseResponsesRequest([]byte(body))
if err != nil {
t.Fatal(err)
}
if len(req.Messages) != 2 {
t.Fatalf("messages = %d items", len(req.Messages))
}
toolMsg := req.Messages[1]
if toolMsg["role"] != "tool" {
t.Fatalf("msg[1] role = %v", toolMsg["role"])
}
content, _ := toolMsg["content"].(string)
if content != "sunny 72F" {
t.Fatalf("content = %q, want %q", content, "sunny 72F")
}
}
// TestParseResponsesRequest_FunctionCallOutputObject verifies that when
// function_call_output.output is a bare JSON object, it is re-encoded as a
// JSON string instead of being silently dropped.
func TestParseResponsesRequest_FunctionCallOutputObject(t *testing.T) {
body := `{"model":"GLM-4.7","input":[
{"type":"function_call","call_id":"call_obj","name":"run","arguments":"{}"},
{"type":"function_call_output","call_id":"call_obj","output":{"result":"success","code":200}}
]}`
req, err := ParseResponsesRequest([]byte(body))
if err != nil {
t.Fatal(err)
}
toolMsg := req.Messages[1]
content, _ := toolMsg["content"].(string)
if content == "" {
t.Fatal("content was dropped (empty)")
}
var parsed map[string]any
if err := json.Unmarshal([]byte(content), &parsed); err != nil {
t.Fatalf("content not valid JSON: %v", err)
}
if parsed["result"] != "success" {
t.Fatalf("result = %v", parsed["result"])
}
}
func TestUsageToResponses(t *testing.T) {
usage := map[string]any{
"prompt_tokens": 10,
+9 -1
View File
@@ -1,6 +1,9 @@
package openai
import "encoding/json"
import (
"encoding/json"
"strings"
)
type ChatCompletionRequest struct {
Model string `json:"model"`
@@ -40,6 +43,11 @@ func (r ChatCompletionRequest) MarshalForUpstream() ([]byte, error) {
m[k] = anyValue
}
}
// Mirror the Zhanlu plugin: GLM models require {tool_stream:true} to
// stream tool calls back to the client (matches /glm/i.test(model)).
if strings.Contains(strings.ToLower(r.Model), "glm") {
m["tool_stream"] = true
}
return json.Marshal(m)
}
+1 -5
View File
@@ -440,7 +440,7 @@ func (s *Server) postPhoneAPI(endpoint string, payload map[string]string, out *p
req.Header.Set("Content-Type", "application/json")
req.Header.Set("plugin_type", "zhanlu_ide")
req.Header.Set("plugin_version", s.cfg.PluginVersion)
req.Header.Set("request", randomRequestID())
req.Header.Set("request", util.RandomRequestID())
resp, err := s.upstreamHTTPClient().Do(req)
if err != nil {
return err
@@ -1093,10 +1093,6 @@ func forEachSSEChunk(r io.Reader, fn func([]byte) error) error {
if payload == "" || payload == "[DONE]" {
continue
}
var upstreamError map[string]any
if json.Unmarshal([]byte(payload), &upstreamError) == nil && upstreamError["state"] == "ERROR" {
return fmt.Errorf("zhanlu upstream error: %v", upstreamError["errorMessage"])
}
if err := fn([]byte(payload)); err != nil {
return err
}
+20 -1
View File
@@ -2,7 +2,12 @@
// avoid divergent same-named copies.
package util
import "strings"
import (
"crypto/rand"
"fmt"
"strings"
"time"
)
// FirstNonEmpty returns the first trimmed-non-empty value, or "" when none of
// the values are non-empty. Callers that need a specific fallback for the
@@ -15,3 +20,17 @@ func FirstNonEmpty(values ...string) string {
}
return ""
}
// RandomRequestID returns a random v4 UUID string, matching the format the
// Zhanlu plugin sends in the `request` header of every gateway call
// (crypto.randomUUID()). Extracted so the login and phone-code paths share
// one implementation.
func RandomRequestID() string {
b := make([]byte, 16)
if _, err := rand.Read(b); err != nil {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
b[6] = (b[6] & 0x0f) | 0x40
b[8] = (b[8] & 0x3f) | 0x80
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
}
+1 -11
View File
@@ -210,7 +210,7 @@ func (c *Client) setPluginHeaders(req *http.Request) {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("plugin_type", "zhanlu_ide")
req.Header.Set("plugin_version", c.PluginVersion)
req.Header.Set("request", randomRequestID())
req.Header.Set("request", util.RandomRequestID())
}
func decodeJSON(resp *http.Response, out any) error {
@@ -248,13 +248,3 @@ func randomAlnum(n int) string {
}
return string(b)
}
func randomRequestID() string {
b := make([]byte, 16)
if _, err := rand.Read(b); err != nil {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
b[6] = (b[6] & 0x0f) | 0x40
b[8] = (b[8] & 0x3f) | 0x80
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
}