File tree Expand file tree Collapse file tree
pkg/model/provider/anthropic Expand file tree Collapse file tree Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments