Skip to content

Commit afb61cb

Browse files
committed
fix: prevent panic in extractSystemBlocks on empty system message with CacheControl
Assisted-By: docker-agent
1 parent b4894c8 commit afb61cb

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

pkg/model/provider/anthropic/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ func extractSystemBlocks(messages []chat.Message) []anthropic.TextBlockParam {
671671
})
672672
}
673673

674-
if msg.CacheControl {
674+
if msg.CacheControl && len(systemBlocks) > 0 {
675675
systemBlocks[len(systemBlocks)-1].CacheControl = anthropic.NewCacheControlEphemeralParam()
676676
}
677677
}

pkg/model/provider/anthropic/client_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,3 +523,20 @@ func TestExtractSystemBlocksCacheControl(t *testing.T) {
523523
assert.Equal(t, "ephemeral", string(blocks[3].CacheControl.Type))
524524
assert.Empty(t, string(blocks[3].CacheControl.TTL))
525525
}
526+
527+
func TestExtractSystemBlocks_EmptyContentWithCacheControl(t *testing.T) {
528+
t.Parallel()
529+
530+
// An empty system message with CacheControl must not panic.
531+
msgs := []chat.Message{
532+
{
533+
Role: chat.MessageRoleSystem,
534+
Content: "",
535+
CacheControl: true,
536+
},
537+
}
538+
539+
// Before the fix this panicked with index out of range [-1].
540+
blocks := extractSystemBlocks(msgs)
541+
assert.Empty(t, blocks)
542+
}

0 commit comments

Comments
 (0)