From 82ac5df92ec5ae043062d8e47771c039ed7a04e4 Mon Sep 17 00:00:00 2001 From: yokomotod Date: Thu, 19 Jun 2025 21:00:07 +0900 Subject: [PATCH] fix: handle content.thinking --- src/claude_code_sdk/_internal/client.py | 8 ++++++++ src/claude_code_sdk/types.py | 8 ++++++++ tests/test_types.py | 16 +++++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/claude_code_sdk/_internal/client.py b/src/claude_code_sdk/_internal/client.py index ef1070d..15704d4 100644 --- a/src/claude_code_sdk/_internal/client.py +++ b/src/claude_code_sdk/_internal/client.py @@ -11,6 +11,7 @@ from ..types import ( ResultMessage, SystemMessage, TextBlock, + ThinkingBlock, ToolResultBlock, ToolUseBlock, UserMessage, @@ -55,6 +56,13 @@ class InternalClient: match block["type"]: case "text": content_blocks.append(TextBlock(text=block["text"])) + case "thinking": + content_blocks.append( + ThinkingBlock( + thinking=block["thinking"], + signature=block["signature"], + ) + ) case "tool_use": content_blocks.append( ToolUseBlock( diff --git a/src/claude_code_sdk/types.py b/src/claude_code_sdk/types.py index 21ae8ff..58509d7 100644 --- a/src/claude_code_sdk/types.py +++ b/src/claude_code_sdk/types.py @@ -26,6 +26,14 @@ class TextBlock: text: str +@dataclass +class ThinkingBlock: + """Thinking content block.""" + + thinking: str + signature: str + + @dataclass class ToolUseBlock: """Tool use content block.""" diff --git a/tests/test_types.py b/tests/test_types.py index 6046292..0d0308a 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -5,7 +5,13 @@ from claude_code_sdk import ( ClaudeCodeOptions, ResultMessage, ) -from claude_code_sdk.types import TextBlock, ToolResultBlock, ToolUseBlock, UserMessage +from claude_code_sdk.types import ( + TextBlock, + ThinkingBlock, + ToolResultBlock, + ToolUseBlock, + UserMessage, +) class TestMessageTypes: @@ -23,6 +29,14 @@ class TestMessageTypes: assert len(msg.content) == 1 assert msg.content[0].text == "Hello, human!" + def test_assistant_message_with_thinking(self): + """Test creating an AssistantMessage with thinking content.""" + thinking_block = ThinkingBlock(thinking="I'm thinking...", signature="sig-123") + msg = AssistantMessage(content=[thinking_block]) + assert len(msg.content) == 1 + assert msg.content[0].thinking == "I'm thinking..." + assert msg.content[0].signature == "sig-123" + def test_tool_use_block(self): """Test creating a ToolUseBlock.""" block = ToolUseBlock(