fix(translator): handle empty tool function names in OpenAI Claude responses
- Added check to prevent processing of empty `function.name` values, ensuring valid data is handled. Fixed: #2557
This commit is contained in:
@@ -236,7 +236,7 @@ func convertOpenAIStreamingChunkToAnthropic(rawJSON []byte, param *ConvertOpenAI
|
|||||||
|
|
||||||
// Handle function name
|
// Handle function name
|
||||||
if function := toolCall.Get("function"); function.Exists() {
|
if function := toolCall.Get("function"); function.Exists() {
|
||||||
if name := function.Get("name"); name.Exists() {
|
if name := function.Get("name"); name.Exists() && name.String() != "" {
|
||||||
accumulator.Name = util.MapToolName(param.ToolNameMap, name.String())
|
accumulator.Name = util.MapToolName(param.ToolNameMap, name.String())
|
||||||
|
|
||||||
stopThinkingContentBlock(param, &results)
|
stopThinkingContentBlock(param, &results)
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package claude
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestConvertOpenAIResponseToClaude_StreamIgnoresNullToolNameDelta(t *testing.T) {
|
||||||
|
originalRequest := []byte(`{"stream":true}`)
|
||||||
|
var param any
|
||||||
|
|
||||||
|
firstChunks := ConvertOpenAIResponseToClaude(
|
||||||
|
context.Background(),
|
||||||
|
"test-model",
|
||||||
|
originalRequest,
|
||||||
|
nil,
|
||||||
|
[]byte(`data: {"id":"chatcmpl_1","model":"test-model","created":1,"choices":[{"index":0,"delta":{"role":"assistant","tool_calls":[{"index":0,"id":"call_1","type":"function","function":{"name":"read_file","arguments":""}}]},"finish_reason":null}]}`),
|
||||||
|
¶m,
|
||||||
|
)
|
||||||
|
firstOutput := bytes.Join(firstChunks, nil)
|
||||||
|
if !bytes.Contains(firstOutput, []byte(`"name":"read_file"`)) {
|
||||||
|
t.Fatalf("expected first chunk to start read_file tool block, got %s", string(firstOutput))
|
||||||
|
}
|
||||||
|
|
||||||
|
secondChunks := ConvertOpenAIResponseToClaude(
|
||||||
|
context.Background(),
|
||||||
|
"test-model",
|
||||||
|
originalRequest,
|
||||||
|
nil,
|
||||||
|
[]byte(`data: {"id":"chatcmpl_1","model":"test-model","created":1,"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"name":null,"arguments":"{\"path\":\"/tmp/a\"}"}}]},"finish_reason":null}]}`),
|
||||||
|
¶m,
|
||||||
|
)
|
||||||
|
secondOutput := bytes.Join(secondChunks, nil)
|
||||||
|
if bytes.Contains(secondOutput, []byte(`content_block_start`)) {
|
||||||
|
t.Fatalf("did not expect null tool name delta to start a new content block, got %s", string(secondOutput))
|
||||||
|
}
|
||||||
|
if bytes.Contains(secondOutput, []byte(`"name":""`)) {
|
||||||
|
t.Fatalf("did not expect null tool name delta to emit an empty tool name, got %s", string(secondOutput))
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user