Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/claude_code_sdk/_internal/message_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ def parse_message(data: dict[str, Any]) -> Message:
user_content_blocks.append(
TextBlock(text=block["text"])
)
case "thinking":
user_content_blocks.append(
ThinkingBlock(
thinking=block["thinking"],
signature=block["signature"],
)
)
case "tool_use":
user_content_blocks.append(
ToolUseBlock(
Expand Down Expand Up @@ -91,6 +84,13 @@ def parse_message(data: dict[str, Any]) -> Message:
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(
Expand Down
26 changes: 26 additions & 0 deletions tests/test_message_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
ResultMessage,
SystemMessage,
TextBlock,
ThinkingBlock,
ToolResultBlock,
ToolUseBlock,
UserMessage,
Expand Down Expand Up @@ -152,6 +153,31 @@ def test_parse_valid_assistant_message(self):
assert isinstance(message.content[0], TextBlock)
assert isinstance(message.content[1], ToolUseBlock)

def test_parse_assistant_message_with_thinking(self):
"""Test parsing an assistant message with thinking block."""
data = {
"type": "assistant",
"message": {
"content": [
{
"type": "thinking",
"thinking": "I'm thinking about the answer...",
"signature": "sig-123",
},
{"type": "text", "text": "Here's my response"},
],
"model": "claude-opus-4-1-20250805",
},
}
message = parse_message(data)
assert isinstance(message, AssistantMessage)
assert len(message.content) == 2
assert isinstance(message.content[0], ThinkingBlock)
assert message.content[0].thinking == "I'm thinking about the answer..."
assert message.content[0].signature == "sig-123"
assert isinstance(message.content[1], TextBlock)
assert message.content[1].text == "Here's my response"

def test_parse_valid_system_message(self):
"""Test parsing a valid system message."""
data = {"type": "system", "subtype": "start"}
Expand Down