fix: default empty input_json_delta arguments to {} for tool call parsing
This commit is contained in:
@@ -451,11 +451,17 @@ func StreamResponseClaude2OpenAI(claudeResponse *dto.ClaudeResponse) *dto.ChatCo
|
|||||||
choice.Delta.Content = claudeResponse.Delta.Text
|
choice.Delta.Content = claudeResponse.Delta.Text
|
||||||
switch claudeResponse.Delta.Type {
|
switch claudeResponse.Delta.Type {
|
||||||
case "input_json_delta":
|
case "input_json_delta":
|
||||||
|
arguments := "{}"
|
||||||
|
if claudeResponse.Delta.PartialJson != nil {
|
||||||
|
if partial := strings.TrimSpace(*claudeResponse.Delta.PartialJson); partial != "" {
|
||||||
|
arguments = partial
|
||||||
|
}
|
||||||
|
}
|
||||||
tools = append(tools, dto.ToolCallResponse{
|
tools = append(tools, dto.ToolCallResponse{
|
||||||
Type: "function",
|
Type: "function",
|
||||||
Index: common.GetPointer(fcIdx),
|
Index: common.GetPointer(fcIdx),
|
||||||
Function: dto.FunctionResponse{
|
Function: dto.FunctionResponse{
|
||||||
Arguments: *claudeResponse.Delta.PartialJson,
|
Arguments: arguments,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
case "signature_delta":
|
case "signature_delta":
|
||||||
|
|||||||
@@ -215,3 +215,41 @@ func TestRequestOpenAI2ClaudeMessage_AssistantToolCallWithMalformedArguments(t *
|
|||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
assert.Empty(t, inputObj)
|
assert.Empty(t, inputObj)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStreamResponseClaude2OpenAI_EmptyInputJSONDeltaFallback(t *testing.T) {
|
||||||
|
empty := ""
|
||||||
|
resp := &dto.ClaudeResponse{
|
||||||
|
Type: "content_block_delta",
|
||||||
|
Index: func() *int { v := 1; return &v }(),
|
||||||
|
Delta: &dto.ClaudeMediaMessage{
|
||||||
|
Type: "input_json_delta",
|
||||||
|
PartialJson: &empty,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
chunk := StreamResponseClaude2OpenAI(resp)
|
||||||
|
require.NotNil(t, chunk)
|
||||||
|
require.Len(t, chunk.Choices, 1)
|
||||||
|
require.NotNil(t, chunk.Choices[0].Delta.ToolCalls)
|
||||||
|
require.Len(t, chunk.Choices[0].Delta.ToolCalls, 1)
|
||||||
|
assert.Equal(t, "{}", chunk.Choices[0].Delta.ToolCalls[0].Function.Arguments)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStreamResponseClaude2OpenAI_NonEmptyInputJSONDeltaPreserved(t *testing.T) {
|
||||||
|
partial := `{"timezone":"Asia/Shanghai"}`
|
||||||
|
resp := &dto.ClaudeResponse{
|
||||||
|
Type: "content_block_delta",
|
||||||
|
Index: func() *int { v := 1; return &v }(),
|
||||||
|
Delta: &dto.ClaudeMediaMessage{
|
||||||
|
Type: "input_json_delta",
|
||||||
|
PartialJson: &partial,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
chunk := StreamResponseClaude2OpenAI(resp)
|
||||||
|
require.NotNil(t, chunk)
|
||||||
|
require.Len(t, chunk.Choices, 1)
|
||||||
|
require.NotNil(t, chunk.Choices[0].Delta.ToolCalls)
|
||||||
|
require.Len(t, chunk.Choices[0].Delta.ToolCalls, 1)
|
||||||
|
assert.Equal(t, partial, chunk.Choices[0].Delta.ToolCalls[0].Function.Arguments)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user