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
21 changes: 20 additions & 1 deletion src/claude_agent_sdk/_internal/message_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ def parse_message(data: dict[str, Any]) -> Message | None:
if isinstance(data["message"]["content"], list):
user_content_blocks: list[ContentBlock] = []
for block in data["message"]["content"]:
if not isinstance(block, dict):
raise MessageParseError(
f"Invalid content block (expected dict, got "
f"{type(block).__name__})",
data,
)
match block["type"]:
case "text":
user_content_blocks.append(
Expand Down Expand Up @@ -126,8 +132,21 @@ def parse_message(data: dict[str, Any]) -> Message | None:

case "assistant":
try:
raw_content = data["message"]["content"]
if not isinstance(raw_content, list):
raise MessageParseError(
f"Invalid assistant content (expected list, got "
f"{type(raw_content).__name__})",
data,
)
content_blocks: list[ContentBlock] = []
for block in data["message"]["content"]:
for block in raw_content:
if not isinstance(block, dict):
raise MessageParseError(
f"Invalid content block (expected dict, got "
f"{type(block).__name__})",
data,
)
match block["type"]:
case "text":
content_blocks.append(TextBlock(text=block["text"]))
Expand Down
16 changes: 16 additions & 0 deletions tests/test_message_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,22 @@ def test_parse_assistant_message_missing_fields(self):
parse_message({"type": "assistant"})
assert "Missing required field in assistant message" in str(exc_info.value)

def test_parse_assistant_string_content_raises(self):
"""Assistant content as a bare string raises MessageParseError, not a raw TypeError."""
with pytest.raises(MessageParseError):
parse_message(
{"type": "assistant", "message": {"model": "m", "content": "hi"}}
)

@pytest.mark.parametrize("role", ["assistant", "user"])
def test_non_dict_content_block_raises_documented_error(self, role: str) -> None:
"""A non-dict block raises MessageParseError, never a raw TypeError."""
message: dict[str, object] = {"content": ["oops"]}
if role == "assistant":
message["model"] = "m"
with pytest.raises(MessageParseError):
parse_message({"type": role, "message": message})

def test_parse_system_message_missing_fields(self):
"""Test that system message with missing fields raises MessageParseError."""
with pytest.raises(MessageParseError) as exc_info:
Expand Down
Loading