fix(openai): preserve empty tool completion turns

This commit is contained in:
CJACK.
2026-03-22 01:19:17 +08:00
parent db89744055
commit f8936887d0
2 changed files with 28 additions and 1 deletions

View File

@@ -29,7 +29,7 @@ func normalizeOpenAIMessagesForPrompt(raw []any, traceID string) []map[string]an
case "tool", "function":
content := normalizeOpenAIContentForPrompt(msg["content"])
if content == "" {
continue
content = "null"
}
out = append(out, map[string]any{
"role": "user",

View File

@@ -117,6 +117,33 @@ func TestNormalizeOpenAIMessagesForPrompt_FunctionRoleCompatible(t *testing.T) {
}
}
func TestNormalizeOpenAIMessagesForPrompt_EmptyToolContentPreservedAsNull(t *testing.T) {
raw := []any{
map[string]any{
"role": "tool",
"tool_call_id": "call_5",
"name": "noop_tool",
"content": "",
},
map[string]any{
"role": "assistant",
"content": "done",
},
}
normalized := normalizeOpenAIMessagesForPrompt(raw, "")
if len(normalized) != 2 {
t.Fatalf("expected tool completion turn to be preserved, got %#v", normalized)
}
if normalized[0]["role"] != "user" {
t.Fatalf("expected tool role mapped to user, got %#v", normalized[0]["role"])
}
got, _ := normalized[0]["content"].(string)
if got != "null" {
t.Fatalf("expected empty tool content to be preserved as null placeholder, got %q", got)
}
}
func TestNormalizeOpenAIMessagesForPrompt_AssistantMultipleToolCallsRemainSeparated(t *testing.T) {
raw := []any{
map[string]any{