fix(anthropic): keep system a string when marked parts are empty

This commit is contained in:
2026-11-26 01:53:51 +00:00
parent 1618934cf7
commit 86870668ea
4 changed files with 77 additions and 7 deletions
+18 -4
View File
@@ -84,21 +84,35 @@ func markBlock(block any) (any, bool) {
return cp, true
}
// markLastBlock returns a copy of blocks with the last entry marked.
// markLastBlock returns a copy of blocks with the last non-empty entry marked;
// the API rejects a cache marker on an empty text block.
func markLastBlock(blocks []any) ([]any, bool) {
if len(blocks) == 0 {
idx := len(blocks) - 1
for idx >= 0 && isEmptyTextBlock(blocks[idx]) {
idx--
}
if idx < 0 {
return blocks, false
}
marked, ok := markBlock(blocks[len(blocks)-1])
marked, ok := markBlock(blocks[idx])
if !ok {
return blocks, false
}
cp := make([]any, len(blocks))
copy(cp, blocks)
cp[len(cp)-1] = marked
cp[idx] = marked
return cp, true
}
func isEmptyTextBlock(block any) bool {
m, ok := block.(map[string]any)
if !ok {
return false
}
text, isStr := m["text"].(string)
return m["type"] == "text" && isStr && text == ""
}
// markContent returns content (string or block list) with its last block marked.
func markContent(content any) (any, bool) {
switch c := content.(type) {
+20
View File
@@ -220,3 +220,23 @@ func TestMessagesPassthroughInjects(t *testing.T) {
t.Fatalf("enabled body = %s", on)
}
}
func TestInjectCacheControlSkipsEmptyTextBlock(t *testing.T) {
b := map[string]any{
"model": "m",
"messages": []any{
map[string]any{"role": "user", "content": []any{
map[string]any{"type": "text", "text": "real"},
map[string]any{"type": "text", "text": ""},
}},
},
}
out, _ := injectCacheControl(b)
blocks := out["messages"].([]any)[0].(map[string]any)["content"].([]any)
if _, ok := blocks[0].(map[string]any)["cache_control"]; !ok {
t.Fatalf("non-empty block not marked: %#v", blocks)
}
if _, ok := blocks[1].(map[string]any)["cache_control"]; ok {
t.Fatal("empty block marked")
}
}
+3 -3
View File
@@ -459,10 +459,10 @@ func anthropicRequestFromOpenAI(req map[string]any) (map[string]any, error) {
for _, part := range c {
pm, _ := part.(map[string]any)
block, marked := systemBlockFromPart(pm)
if marked {
systemMarked = true
}
if block["text"] != "" {
if marked {
systemMarked = true
}
systemBlocks = append(systemBlocks, block)
}
}
+36
View File
@@ -50,3 +50,39 @@ func TestTranslateSystemNoMarkersUnchanged(t *testing.T) {
t.Fatalf("system = %#v", out["system"])
}
}
func systemReq(parts ...any) map[string]any {
return map[string]any{
"model": "claude-x",
"messages": []any{
map[string]any{"role": "system", "content": parts},
map[string]any{"role": "user", "content": "hi"},
},
}
}
func TestTranslateSystemEmptyMarkedPartKeepsString(t *testing.T) {
cc := map[string]any{"type": "ephemeral"}
out, err := anthropicRequestFromOpenAI(systemReq(
map[string]any{"type": "text", "text": "", "cache_control": cc}))
if err != nil {
t.Fatal(err)
}
if out["system"] != "" {
t.Fatalf("system = %#v, want empty string", out["system"])
}
}
func TestTranslateSystemEmptyMarkedPartWithOthers(t *testing.T) {
cc := map[string]any{"type": "ephemeral"}
out, err := anthropicRequestFromOpenAI(systemReq(
map[string]any{"type": "text", "text": "A"},
map[string]any{"type": "text", "text": "", "cache_control": cc},
map[string]any{"type": "text", "text": "B"}))
if err != nil {
t.Fatal(err)
}
if out["system"] != "A\n\nB" {
t.Fatalf("system = %#v", out["system"])
}
}