Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions changelog/unreleased/tool-call-arguments-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### English

- Tool calls: stop dropping or failing on argument text that is only formatting-damaged. Trailing commas, bare quotes inside a value, missing closing brackets, and prose appended after the object are now repaired before a call is validated, on `/v1/responses`, `/v1/messages`, and the OpenAI compatibility paths.
- Streaming assembly no longer mangles arguments when an upstream resends the whole argument buffer on each chunk, omits `index`, reports `index` 0 for every call, or sends a JSON object where the schema expects a string. Those cases previously produced `{"a":1}{"a":1}` or silently lost fragments, which clients report as "arguments are invalid JSON".
- Arguments genuinely cut mid-string (output-token limit) are still rejected instead of being padded into a valid-looking command, and the error now says the model hit its token limit.

### 中文

- 工具调用:参数文本只是格式损坏时不再丢弃或直接报错。结尾多余逗号、值内部裸引号、缺少闭合括号、对象后面跟了一段解释文字,都会先修复再校验,覆盖 `/v1/responses`、`/v1/messages` 和 OpenAI 兼容路径。
- 流式拼接不再被上游的坏习惯整坏:上游每帧重发完整参数缓冲、省略 `index`、所有调用都写 `index:0`、或者该给字符串却给了 JSON 对象,以前会拼出 `{"a":1}{"a":1}` 或静默丢片段,客户端那边就报「arguments are invalid JSON」。
- 真被输出 token 上限截断在字符串中间的参数仍然报错(不会补成看起来合法、实际却不是模型本意的命令),并在错误里说明是触到 token 上限。
9 changes: 9 additions & 0 deletions changelog/unreleased/tool-call-arguments-stream-normalize.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### English

- Streaming tool calls now deliver arguments as one already-normalized string instead of relaying upstream fragments. `/v1/responses` sends the repaired arguments once in `response.function_call_arguments.delta` and repeats them in `.done`, `/v1/messages` sends them at block close, and `/v1/chat/completions` withholds tool frames until the call is complete. Clients that concatenate deltas and clients that read the done event therefore receive identical, valid JSON.
- Arguments truncated mid-string are no longer padded. The unfinished trailing member is dropped, so the call still parses and the tool or model can recover, instead of the round failing with "arguments are invalid JSON".

### 中文

- 流式工具调用不再把上游碎片直接转发,而是一次性下发已经归一化好的参数:`/v1/responses` 在 `response.function_call_arguments.delta` 给出修复后的完整参数并在 `.done` 重复一次,`/v1/messages` 在 block 收尾时发,`/v1/chat/completions` 把工具帧拦到调用结束再发。因此「拼接 delta 的客户端」和「读 done 事件的客户端」拿到的是同一份合法 JSON。
- 截断在字符串中间的参数不再补齐:只丢弃尾部未完成的那个成员,调用仍然能解析,交给工具或模型自己纠正,而不是整轮报「arguments are invalid JSON」。
3 changes: 2 additions & 1 deletion internal/gateway/anthropic.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ func anthropicMessageResponse(requestID, model, content, reasoning string, toolC
blocks = append(blocks, map[string]any{"type": "text", "text": content})
}
for _, call := range toolCalls {
input := json.RawMessage(call.Arguments)
arguments, _ := translate.NormalizeToolCallArguments(call.Arguments)
input := json.RawMessage(arguments)
if !json.Valid(input) {
input = json.RawMessage(`{}`)
}
Expand Down
25 changes: 14 additions & 11 deletions internal/gateway/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,33 @@ func decodeOpenAIToolCalls(raw json.RawMessage) []proxyToolCall {
arguments = "{}"
}
if _, _, custom := translate.DecodeCustomToolName(call.Function.Name); !custom && !json.Valid([]byte(arguments)) {
continue
normalized, _ := translate.NormalizeToolCallArguments(arguments)
arguments = normalized
}
calls = append(calls, proxyToolCall{ID: call.ID, Name: call.Function.Name, Arguments: arguments})
}
return calls
}

func validateProxyToolCallArguments(calls []proxyToolCall) error {
// normalizeProxyToolCallArguments makes every function call carry valid JSON
// arguments. It never fails the turn: a call the model mangled or left
// unfinished keeps only the fields it completed, so the client can run the tool
// and hand the validation error back to the model instead of the client
// aborting the whole round with "arguments are invalid JSON".
func normalizeProxyToolCallArguments(calls []proxyToolCall) bool {
changed := false
for index := range calls {
call := &calls[index]
if _, _, custom := translate.DecodeCustomToolName(call.Name); custom {
continue
}
arguments := strings.TrimSpace(call.Arguments)
if arguments == "" {
call.Arguments = "{}"
continue
}
if !json.Valid([]byte(arguments)) {
return fmt.Errorf("tool call %q (%s) arguments are invalid JSON", call.ID, call.Name)
normalized, normalizedChanged := translate.NormalizeToolCallArguments(call.Arguments)
call.Arguments = normalized
if normalizedChanged {
changed = true
}
call.Arguments = arguments
}
return nil
return changed
}

func (h *Handler) PrepareCompatibilityExecution(r *http.Request, request translate.ChatRequest) (Execution, error) {
Expand Down
Loading
Loading