Skip to content

Commit 7eb4070

Browse files
ochafikclaude
andcommitted
Add tests for full coverage of tool validation branches
- Add test cases for valid create_message calls that pass validation (covering branches 261->266, 266->272, 269->272 in session.py) - Add test for CreateMessageResult.content_as_list with single content (covering line 1245 else branch in types.py) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent b1d944a commit 7eb4070

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

tests/server/test_session.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,29 @@ async def test_create_message_tool_result_validation():
360360
tools=[tool],
361361
)
362362

363+
# Case 5: text-only message with tools (no tool_results) - passes validation
364+
# This covers branch 261->266 (has_tool_results=False) and 266->272
365+
# We use move_on_after since send_request will block waiting for response
366+
with anyio.move_on_after(0.01):
367+
await session.create_message(
368+
messages=[types.SamplingMessage(role="user", content=text)],
369+
max_tokens=100,
370+
tools=[tool],
371+
)
372+
373+
# Case 6: valid matching tool_result/tool_use IDs - passes validation
374+
# This covers branch 269->272 (IDs match, no error raised)
375+
with anyio.move_on_after(0.01):
376+
await session.create_message(
377+
messages=[
378+
types.SamplingMessage(role="user", content=text),
379+
types.SamplingMessage(role="assistant", content=tool_use),
380+
types.SamplingMessage(role="user", content=tool_result),
381+
],
382+
max_tokens=100,
383+
tools=[tool],
384+
)
385+
363386

364387
@pytest.mark.anyio
365388
async def test_other_requests_blocked_before_initialization():

tests/shared/test_streamable_http.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,10 @@ async def handle_call_tool(name: str, args: dict[str, Any]) -> list[TextContent]
210210
)
211211

212212
# Return the sampling result in the tool response
213-
if isinstance(sampling_result.content, list):
214-
response = (
215-
sampling_result.content[0].text
216-
if sampling_result.content and sampling_result.content[0].type == "text"
217-
else None
218-
)
219-
elif sampling_result.content.type == "text":
220-
response = sampling_result.content.text
213+
if all(c.type == "text" for c in sampling_result.content_as_list):
214+
response = "\n".join(c.text for c in sampling_result.content_as_list if c.type == "text")
221215
else:
222-
response = None
216+
response = str(sampling_result.content)
223217
return [
224218
TextContent(
225219
type="text",
@@ -1248,13 +1242,8 @@ async def sampling_callback(
12481242
nonlocal sampling_callback_invoked, captured_message_params
12491243
sampling_callback_invoked = True
12501244
captured_message_params = params
1251-
msg_content = params.messages[0].content
1252-
if isinstance(msg_content, list):
1253-
message_received = msg_content[0].text if msg_content and msg_content[0].type == "text" else None
1254-
elif msg_content.type == "text":
1255-
message_received = msg_content.text
1256-
else:
1257-
message_received = None
1245+
msg_content = params.messages[0].content_as_list[0]
1246+
message_received = msg_content.text if msg_content.type == "text" else None
12581247

12591248
return types.CreateMessageResult(
12601249
role="assistant",

tests/test_types.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ async def test_create_message_result_with_tool_use():
253253
assert result.stopReason == "toolUse"
254254
assert result.model == "claude-3"
255255

256+
# Test content_as_list with single content (covers else branch)
257+
content_list = result.content_as_list
258+
assert len(content_list) == 1
259+
assert content_list[0] == result.content
260+
256261

257262
@pytest.mark.anyio
258263
async def test_client_capabilities_with_sampling_tools():

0 commit comments

Comments
 (0)