Skip to content

Commit 30df222

Browse files
authored
Move thinking block parsing from user to assistant messages (#119)
Related to #28 cc @dicksontsai
1 parent bc01cd7 commit 30df222

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/claude_code_sdk/_internal/message_parser.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ def parse_message(data: dict[str, Any]) -> Message:
5454
user_content_blocks.append(
5555
TextBlock(text=block["text"])
5656
)
57-
case "thinking":
58-
user_content_blocks.append(
59-
ThinkingBlock(
60-
thinking=block["thinking"],
61-
signature=block["signature"],
62-
)
63-
)
6457
case "tool_use":
6558
user_content_blocks.append(
6659
ToolUseBlock(
@@ -91,6 +84,13 @@ def parse_message(data: dict[str, Any]) -> Message:
9184
match block["type"]:
9285
case "text":
9386
content_blocks.append(TextBlock(text=block["text"]))
87+
case "thinking":
88+
content_blocks.append(
89+
ThinkingBlock(
90+
thinking=block["thinking"],
91+
signature=block["signature"],
92+
)
93+
)
9494
case "tool_use":
9595
content_blocks.append(
9696
ToolUseBlock(

tests/test_message_parser.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
ResultMessage,
1010
SystemMessage,
1111
TextBlock,
12+
ThinkingBlock,
1213
ToolResultBlock,
1314
ToolUseBlock,
1415
UserMessage,
@@ -152,6 +153,31 @@ def test_parse_valid_assistant_message(self):
152153
assert isinstance(message.content[0], TextBlock)
153154
assert isinstance(message.content[1], ToolUseBlock)
154155

156+
def test_parse_assistant_message_with_thinking(self):
157+
"""Test parsing an assistant message with thinking block."""
158+
data = {
159+
"type": "assistant",
160+
"message": {
161+
"content": [
162+
{
163+
"type": "thinking",
164+
"thinking": "I'm thinking about the answer...",
165+
"signature": "sig-123",
166+
},
167+
{"type": "text", "text": "Here's my response"},
168+
],
169+
"model": "claude-opus-4-1-20250805",
170+
},
171+
}
172+
message = parse_message(data)
173+
assert isinstance(message, AssistantMessage)
174+
assert len(message.content) == 2
175+
assert isinstance(message.content[0], ThinkingBlock)
176+
assert message.content[0].thinking == "I'm thinking about the answer..."
177+
assert message.content[0].signature == "sig-123"
178+
assert isinstance(message.content[1], TextBlock)
179+
assert message.content[1].text == "Here's my response"
180+
155181
def test_parse_valid_system_message(self):
156182
"""Test parsing a valid system message."""
157183
data = {"type": "system", "subtype": "start"}

0 commit comments

Comments
 (0)