Move thinking block parsing from user to assistant messages (#119)
Some checks failed
Lint / lint (push) Has been cancelled
Test / test (3.10) (push) Has been cancelled
Test / test (3.11) (push) Has been cancelled
Test / test (3.12) (push) Has been cancelled
Test / test (3.13) (push) Has been cancelled

Related to https://github.com/anthropics/claude-code-sdk-python/pull/28
cc @dicksontsai
This commit is contained in:
Michael Gendy 2025-08-19 23:36:12 +03:00 committed by GitHub
parent bc01cd7e9a
commit 30df222bfc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 7 deletions

View file

@ -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(
@ -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(

View file

@ -9,6 +9,7 @@ from claude_code_sdk.types import (
ResultMessage,
SystemMessage,
TextBlock,
ThinkingBlock,
ToolResultBlock,
ToolUseBlock,
UserMessage,
@ -152,6 +153,31 @@ class TestMessageParser:
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"}