From 6188e90aab0d88976ef28999c019632a45c9c750 Mon Sep 17 00:00:00 2001 From: ParthSareen Date: Mon, 5 Jan 2026 00:43:02 -0800 Subject: [PATCH] anthropic: preserve messages with only thinking content Fix edge case where messages containing only a thinking block (no text, images, or tool calls) would be dropped. Add thinking != "" to the condition that creates messages from content blocks. --- anthropic/anthropic.go | 2 +- anthropic/anthropic_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/anthropic/anthropic.go b/anthropic/anthropic.go index 3a1d28099..bd1465792 100644 --- a/anthropic/anthropic.go +++ b/anthropic/anthropic.go @@ -411,7 +411,7 @@ func convertMessage(msg MessageParam) ([]api.Message, error) { } } - if textContent.Len() > 0 || len(images) > 0 || len(toolCalls) > 0 { + if textContent.Len() > 0 || len(images) > 0 || len(toolCalls) > 0 || thinking != "" { m := api.Message{ Role: role, Content: textContent.String(), diff --git a/anthropic/anthropic_test.go b/anthropic/anthropic_test.go index 2c4f5a68a..3cebe6710 100644 --- a/anthropic/anthropic_test.go +++ b/anthropic/anthropic_test.go @@ -312,6 +312,41 @@ func TestFromMessagesRequest_WithThinking(t *testing.T) { } } +// TestFromMessagesRequest_ThinkingOnlyBlock verifies that messages containing only +// a thinking block (no text, images, or tool calls) are preserved and not dropped. +func TestFromMessagesRequest_ThinkingOnlyBlock(t *testing.T) { + req := MessagesRequest{ + Model: "test-model", + MaxTokens: 1024, + Messages: []MessageParam{ + {Role: "user", Content: "Hello"}, + { + Role: "assistant", + Content: []any{ + map[string]any{ + "type": "thinking", + "thinking": "Let me think about this...", + }, + }, + }, + }, + } + + result, err := FromMessagesRequest(req) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if len(result.Messages) != 2 { + t.Fatalf("expected 2 messages, got %d", len(result.Messages)) + } + + assistantMsg := result.Messages[1] + if assistantMsg.Thinking != "Let me think about this..." { + t.Errorf("expected thinking content, got %q", assistantMsg.Thinking) + } +} + func TestFromMessagesRequest_ToolUseMissingID(t *testing.T) { req := MessagesRequest{ Model: "test-model",