mirror of
https://github.com/anthropics/claude-code-sdk-python.git
synced 2025-12-23 09:19:52 +00:00
Move thinking block parsing from user to assistant messages (#119)
Related to https://github.com/anthropics/claude-code-sdk-python/pull/28 cc @dicksontsai
This commit is contained in:
parent
bc01cd7e9a
commit
30df222bfc
2 changed files with 33 additions and 7 deletions
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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"}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue