Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix tools conversion (#6289)

Shahmir Varqhac051aef2fed7
diff --git a/frontend/src/components/chat/chat-panel.tsx b/frontend/src/components/chat/chat-panel.tsx
index 1e202f8416..a678405601 100644
--- a/frontend/src/components/chat/chat-panel.tsx
+++ b/frontend/src/components/chat/chat-panel.tsx
@@ -5,11 +5,7 @@ import { useChat } from "@ai-sdk/react";
import { startCompletion } from "@codemirror/autocomplete";
import { storePrompt } from "@marimo-team/codemirror-ai";
import type { ReactCodeMirrorRef } from "@uiw/react-codemirror";
-import {
- DefaultChatTransport,
- lastAssistantMessageIsCompleteWithToolCalls,
- type ToolUIPart,
-} from "ai";
+import { DefaultChatTransport, type ToolUIPart } from "ai";
import { useAtom, useAtomValue, useSetAtom } from "jotai";
import {
AtSignIcon,
@@ -271,6 +267,10 @@ const ChatMessageDisplay: React.FC<ChatMessageProps> = memo(
/>
);
+ // These are cryptographic signatures, so we don't need to render them
+ case "data-reasoning-signature":
+ return null;
+
/* handle other part types … */
default:
if (part.type.startsWith("data-")) {
@@ -587,8 +587,41 @@ const ChatPanelBody = () => {
id: chatId,
} = useChat({
id: activeChatId,
- // Automatically submit when all tool results are available
- sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,
+ // Only automatically submit if we have tool calls but no text response yet
+ sendAutomaticallyWhen: ({ messages }) => {
+ if (messages.length === 0) {
+ return false;
+ }
+
+ const lastMessage = messages[messages.length - 1];
+ const parts = lastMessage.parts;
+
+ if (parts.length === 0) {
+ return false;
+ }
+
+ // Only auto-send if the last message is an assistant message
+ // Because assistant messages are the ones that can have tool calls
+ if (lastMessage.role !== "assistant") {
+ return false;
+ }
+
+ const toolParts = parts.filter((part) =>
+ part.type.startsWith("tool-"),
+ ) as ToolUIPart[];
+
+ const hasCompletedToolCalls = toolParts.some(
+ (part) => part.state === "output-available",
+ );
+
+ // Check if the last part has any text content
+ const lastPart = parts[parts.length - 1];
+ const hasTextContent =
+ lastPart.type === "text" && lastPart.text?.trim().length > 0;
+
+ // Only auto-send if we have completed tool calls and there is no reply yet
+ return hasCompletedToolCalls && !hasTextContent;
+ },
messages: activeChat?.messages || [], // initial messages
transport: new DefaultChatTransport({
api: runtimeManager.getAiURL("chat").toString(),
diff --git a/marimo/_ai/_convert.py b/marimo/_ai/_convert.py
index 6a8497eee9..7ec1f8f5e8 100644
--- a/marimo/_ai/_convert.py
+++ b/marimo/_ai/_convert.py
@@ -5,11 +5,13 @@ import base64
import dataclasses
import json
import uuid
-from typing import TYPE_CHECKING, Any, Literal, Optional, Union, cast
+from typing import TYPE_CHECKING, Any, Literal, Optional, Union
+from marimo import _loggers
from marimo._ai._types import (
ChatMessage,
ChatPart,
+ DataReasoningPart,
FilePart,
ReasoningPart,
TextPart,
@@ -38,8 +40,19 @@ if TYPE_CHECKING:
PartDict,
)
from openai.types.chat import ( # type: ignore[import-not-found]
+ ChatCompletionAssistantMessageParam,
+ ChatCompletionContentPartImageParam,
+ ChatCompletionContentPartParam,
+ ChatCompletionContentPartTextParam,
ChatCompletionMessageParam,
+ ChatCompletionMessageToolCallParam,
+ ChatCompletionToolMessageParam,
)
+ from openai.types.chat.chat_completion_assistant_message_param import (
+ ContentArrayOfContentPart,
+ )
+
+LOGGER = _loggers.marimo_logger()
# Message conversions
@@ -84,39 +97,85 @@ def get_openai_messages_from_parts(
def convert_to_openai_messages(
messages: list[ChatMessage],
) -> list[ChatCompletionMessageParam]:
- openai_messages: list[dict[Any, Any]] = []
+ openai_messages: list[ChatCompletionMessageParam] = []
for message in messages:
- parts: list[dict[Any, Any]] = []
if not message.parts or len(message.parts) == 0:
- parts.append({"type": "text", "text": message.content})
- else:
- for part in message.parts:
- if isinstance(part, FilePart):
- if part.media_type.startswith("image"):
- parts.append(
- {
- "type": "image_url",
- "image_url": {"url": part.url},
- }
- )
- elif part.media_type.startswith("text"):
- parts.append(
- {"type": "text", "text": _extract_text(part.url)}
- )
- else:
- raise ValueError(
- f"Unsupported content type {part.media_type}"
- )
+ content_part: ChatCompletionContentPartTextParam = {
+ "type": "text",
+ "text": message.content,
+ }
+ openai_messages.append(
+ {"role": message.role, "content": [content_part]} # type: ignore
+ )
+ continue
+
+ current_parts: list[
+ Union[
+ ChatCompletionContentPartTextParam,
+ ChatCompletionContentPartParam,
+ ContentArrayOfContentPart,
+ ]
+ ] = []
+ for part in message.parts:
+ if isinstance(part, TextPart):
+ text_part: ChatCompletionContentPartTextParam = {
+ "type": "text",
+ "text": part.text,
+ }
+ current_parts.append(text_part)
+ elif isinstance(part, FilePart):
+ media_type = part.media_type.lstrip()
+ if media_type.startswith("image"):
+ image_part: ChatCompletionContentPartImageParam = {
+ "type": "image_url",
+ "image_url": {"url": part.url},
+ }
+ current_parts.append(image_part)
+ elif media_type.startswith("text"):
+ file_text_part: ChatCompletionContentPartTextParam = {
+ "type": "text",
+ "text": _extract_text(part.url),
+ }
+ current_parts.append(file_text_part)
else:
- if dataclasses.is_dataclass(part):
- parts.append(dataclasses.asdict(part))
- else:
- parts.append(cast(dict[str, Any], part))
+ raise ValueError(f"Unsupported content type {media_type}")
+ elif isinstance(part, ToolInvocationPart):
+ # For ToolInvocationPart, we need to create separate messages for tool call and tool result
+ tool_calls: ChatCompletionMessageToolCallParam = {
+ "id": part.tool_call_id,
+ "type": "function",
+ "function": {
+ "name": part.tool_name,
+ "arguments": str(part.input) if part.input else "{}",
+ },
+ }
- openai_messages.append({"role": message.role, "content": parts})
+ tool_invocation_message: ChatCompletionAssistantMessageParam = {
+ "role": "assistant",
+ "tool_calls": [tool_calls],
+ }
+ openai_messages.append(tool_invocation_message)
- return cast("list[ChatCompletionMessageParam]", openai_messages)
+ # Create tool result message
+ tool_invocation_result: ChatCompletionToolMessageParam = {
+ "tool_call_id": part.tool_call_id,
+ "role": "tool",
+ "content": str(part.output),
+ }
+ openai_messages.append(tool_invocation_result)
+
+ # Reset parts since we've added the messages
+ current_parts = []
+ else:
+ current_parts.append(dataclasses.asdict(part)) # type: ignore
+
+ if current_parts:
+ openai_messages.append(
+ {"role": message.role, "content": current_parts} # type: ignore
+ )
+
+ return openai_messages
AnthropicParts = Union[
@@ -133,102 +192,123 @@ AnthropicParts = Union[
]
-def get_anthropic_parts_from_chat_parts(
- parts: list[ChatPart],
-) -> list[AnthropicParts]:
- anthropic_parts: list[AnthropicParts] = []
-
- for part in parts:
- if isinstance(part, TextPart):
- text_block: TextBlockParam = {
- "type": "text",
- "text": part.text,
- }
- anthropic_parts.append(text_block)
- elif isinstance(part, ReasoningPart):
- # Handle reasoning parts with proper Anthropic thinking type
- signature = ""
- if part.details and len(part.details) > 0:
- signature = part.details[0].signature or ""
-
- thinking_message: ThinkingBlockParam = {
- "type": "thinking",
- "thinking": part.text,
- "signature": signature,
- }
- anthropic_parts.append(thinking_message)
- elif isinstance(part, FilePart):
- media_type = part.media_type
- if media_type.strip() in [
- "image/jpeg",
- "image/png",
- "image/gif",
- "image/webp",
- ]:
- image_message: ImageBlockParam = {
- "type": "image",
- "source": {
- "type": "base64",
- "media_type": media_type, # type: ignore
- "data": _extract_data(part.url),
- },
- }
- anthropic_parts.append(image_message)
- elif media_type.startswith("text"):
- text_block_file: TextBlockParam = {
- "type": "text",
- "text": _extract_text(part.url),
- }
- anthropic_parts.append(text_block_file)
- else:
- raise ValueError(f"Unsupported content type {media_type}")
- elif isinstance(part, ToolInvocationPart):
- # Add tool use to current message content
- tool_use_message: ToolUseBlockParam = {
- "type": "tool_use",
- "id": part.tool_call_id,
- "name": part.tool_name,
- "input": part.input,
- }
- anthropic_parts.append(tool_use_message)
-
- # Create separate tool result message
- tool_result_message: ToolResultBlockParam = {
- "tool_use_id": part.tool_call_id,
- "type": "tool_result",
- "content": [
- {
- "type": "text",
- "text": str(part.output),
- }
- ],
- }
- anthropic_parts.append(tool_result_message)
-
- return anthropic_parts
-
-
def convert_to_anthropic_messages(
messages: list[ChatMessage],
) -> list[MessageParam]:
anthropic_messages: list[MessageParam] = []
for message in messages:
- parts: list[AnthropicParts] = []
+ anthropic_role = (
+ "assistant" if message.role == "system" else message.role
+ )
+
if not message.parts:
# Convert content to string
text_block: TextBlockParam = {
"type": "text",
"text": str(message.content),
}
- parts.append(text_block)
- else:
- parts.extend(get_anthropic_parts_from_chat_parts(message.parts))
+ anthropic_messages.append(
+ {"role": anthropic_role, "content": [text_block]}
+ )
+ continue
+
+ current_parts: list[AnthropicParts] = []
+ data_reasoning_parts = [
+ part
+ for part in message.parts
+ if isinstance(part, DataReasoningPart)
+ ]
+
+ for part in message.parts:
+ if isinstance(part, TextPart):
+ text_part: TextBlockParam = {
+ "type": "text",
+ "text": part.text,
+ }
+ current_parts.append(text_part)
+ elif isinstance(part, ReasoningPart):
+ signature = ""
+ if part.details and len(part.details) > 0:
+ signature = part.details[0].signature or ""
+
+ # Use the first data reasoning part if there is no signature
+ # And remove it from the list
+ # We can optimize this
+ if not signature and data_reasoning_parts:
+ signature = data_reasoning_parts[0].data.signature
+ data_reasoning_parts.pop(0)
+
+ thinking_message: ThinkingBlockParam = {
+ "type": "thinking",
+ "thinking": part.text,
+ "signature": signature,
+ }
+ current_parts.append(thinking_message)
+ elif isinstance(part, ToolInvocationPart):
+ # For ToolInvocationPart, we need to create separate messages for tool use and tool result
+ tool_use_block: ToolUseBlockParam = {
+ "type": "tool_use",
+ "id": part.tool_call_id,
+ "name": part.tool_name,
+ "input": part.input,
+ }
+
+ # Add the tool use to an assistant message
+ assistant_message_parts = current_parts + [tool_use_block]
+ anthropic_messages.append(
+ {"role": "assistant", "content": assistant_message_parts}
+ )
+
+ # Then create the tool result as a user message
+ tool_result_message: ToolResultBlockParam = {
+ "tool_use_id": part.tool_call_id,
+ "type": "tool_result",
+ "content": [
+ {
+ "type": "text",
+ "text": str(part.output),
+ }
+ ],
+ }
+ anthropic_messages.append(
+ {"role": "user", "content": [tool_result_message]}
+ )
+
+ # Reset parts since we've added the messages
+ current_parts = []
+ elif isinstance(part, FilePart):
+ media_type = part.media_type.lstrip()
+ # Only these types are supported by Anthropic
+ if media_type in [
+ "image/jpeg",
+ "image/png",
+ "image/gif",
+ "image/webp",
+ ]:
+ file_block: ImageBlockParam = {
+ "type": "image",
+ "source": {
+ "type": "base64",
+ "media_type": media_type, # type: ignore
+ "data": _extract_data(part.url),
+ },
+ }
+ current_parts.append(file_block)
+ elif media_type.startswith("text"):
+ file_text_part: TextBlockParam = {
+ "type": "text",
+ "text": _extract_text(part.url),
+ }
+ current_parts.append(file_text_part)
+ else:
+ raise ValueError(f"Unsupported content type {media_type}")
+
+ if current_parts:
+ anthropic_messages.append(
+ {"role": anthropic_role, "content": current_parts}
+ )
- anthropic_role = (
- "assistant" if message.role == "system" else message.role
- )
- anthropic_messages.append({"role": anthropic_role, "content": parts})
return anthropic_messages
diff --git a/marimo/_ai/_types.py b/marimo/_ai/_types.py
index ee28a0eb1b..6865cd8bce 100644
--- a/marimo/_ai/_types.py
+++ b/marimo/_ai/_types.py
@@ -17,8 +17,11 @@ from typing import (
import msgspec
+from marimo import _loggers
from marimo._utils.parse_dataclass import parse_raw
+LOGGER = _loggers.marimo_logger()
+
class ChatAttachmentDict(TypedDict):
url: str
@@ -151,12 +154,35 @@ class FilePart:
filename: Optional[str] = None
+@dataclass
+class ReasoningData:
+ signature: str
+
+
+@dataclass
+class DataReasoningPart:
+ type: Literal["data-reasoning-signature"]
+ data: ReasoningData
+
+
if TYPE_CHECKING:
- ChatPart = Union[TextPart, ReasoningPart, ToolInvocationPart, FilePart]
+ ChatPart = Union[
+ TextPart,
+ ReasoningPart,
+ ToolInvocationPart,
+ FilePart,
+ DataReasoningPart,
+ ]
else:
ChatPart = dict[str, Any]
-PART_TYPES = [TextPart, ReasoningPart, ToolInvocationPart, FilePart]
+PART_TYPES = [
+ TextPart,
+ ReasoningPart,
+ ToolInvocationPart,
+ FilePart,
+ DataReasoningPart,
+]
class ChatMessage(msgspec.Struct):
@@ -182,9 +208,13 @@ class ChatMessage(msgspec.Struct):
# Hack: msgspec only supports discriminated unions. This is a hack to just
# iterate through possible part variants and decode until one works.
if self.parts:
- self.parts = [self._convert_part(part) for part in self.parts]
+ parts = []
+ for part in self.parts:
+ if converted := self._convert_part(part):
+ parts.append(converted)
+ self.parts = parts
- def _convert_part(self, part: Any) -> ChatPart:
+ def _convert_part(self, part: Any) -> Optional[ChatPart]:
for PartType in PART_TYPES:
try:
if dataclasses.is_dataclass(part):
@@ -193,9 +223,8 @@ class ChatMessage(msgspec.Struct):
except Exception:
continue
- raise msgspec.DecodeError(
- f"Could not decode part as any of {PART_TYPES}, for part {part}"
- )
+ LOGGER.error(f"Could not decode part as {PartType}, for part {part}")
+ return None
@dataclass
diff --git a/tests/_ai/test_chat_convert.py b/tests/_ai/test_chat_convert.py
index 144bec7cd7..f22f5240f6 100644
--- a/tests/_ai/test_chat_convert.py
+++ b/tests/_ai/test_chat_convert.py
@@ -15,13 +15,14 @@ from marimo._ai._convert import (
convert_to_groq_messages,
convert_to_openai_messages,
convert_to_openai_tools,
- get_anthropic_parts_from_chat_parts,
get_google_messages_from_parts,
get_openai_messages_from_parts,
)
from marimo._ai._types import (
ChatMessage,
+ DataReasoningPart,
FilePart,
+ ReasoningData,
ReasoningDetails,
ReasoningPart,
TextPart,
@@ -125,65 +126,610 @@ def test_convert_to_openai_messages_with_parts():
]
result = convert_to_openai_messages(sample_messages)
assert result == [
+ {
+ "role": "assistant",
+ "tool_calls": [
+ {
+ "id": "call_123",
+ "type": "function",
+ "function": {
+ "name": "calculator",
+ "arguments": "{'expression': '2 + 2'}",
+ },
+ }
+ ],
+ },
+ {
+ "tool_call_id": "call_123",
+ "role": "tool",
+ "content": "{'answer': 4}",
+ },
{
"role": "user",
"content": [
- {"type": "text", "text": "Message with parts and attachments"},
{
- "type": "reasoning",
- "text": "Deep thinking process",
- "details": [
- {"type": "text", "text": "Analysis", "signature": None}
- ],
+ "type": "image_url",
+ "image_url": {"url": "data:image/png;base64,b'aGVsbG8='"},
+ }
+ ],
+ },
+ ]
+
+
+class TestAnthropic:
+ def test_convert_to_anthropic_messages(
+ self,
+ sample_messages: list[ChatMessage],
+ ):
+ result = convert_to_anthropic_messages(sample_messages)
+ assert result == [
+ # User message
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "Hello, I have a question."},
+ {
+ "type": "image",
+ "source": {
+ "type": "base64",
+ "media_type": "image/png",
+ "data": "b'aGVsbG8='",
+ },
+ },
+ {"type": "text", "text": "A\n1\n2\n3\n"},
+ ],
+ },
+ # Assistant message
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "Sure, I'd be happy to help. What's your question?",
+ }
+ ],
+ },
+ ]
+
+ def test_convert_to_anthropic_messages_with_tool_invocation(self):
+ """Test converting ToolInvocationPart to Anthropic format."""
+ parts = [
+ TextPart(type="text", text="I'll search for you"),
+ ReasoningPart(
+ type="reasoning",
+ text="The user is asking me to search for Python tutorials",
+ details=[],
+ ),
+ DataReasoningPart(
+ type="data-reasoning-signature",
+ data=ReasoningData(signature="signature_1"),
+ ),
+ TextPart(type="text", text="Searching for Python tutorials"),
+ ToolInvocationPart(
+ type="tool-search_tool",
+ tool_call_id="call_123",
+ state="output-available",
+ input={"query": "Python tutorials"},
+ output={"results": ["tutorial1", "tutorial2"]},
+ ),
+ ToolInvocationPart(
+ type="tool-search_tool",
+ tool_call_id="call_456",
+ state="output-available",
+ input={"query": "JavaScript tutorials"},
+ output={"results": ["tutorial3", "tutorial4"]},
+ ),
+ ]
+ messages = [
+ ChatMessage(
+ role="user",
+ content="",
+ parts=[
+ TextPart(type="text", text="Search for Python tutorials")
+ ],
+ ),
+ ChatMessage(role="assistant", content="", parts=parts),
+ ]
+
+ result = convert_to_anthropic_messages(messages)
+ assert result == [
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "Search for Python tutorials"}
+ ],
+ },
+ {
+ "role": "assistant",
+ "content": [
+ {"type": "text", "text": "I'll search for you"},
+ {
+ "type": "thinking",
+ "thinking": "The user is asking me to search for Python tutorials",
+ "signature": "signature_1",
+ },
+ {"type": "text", "text": "Searching for Python tutorials"},
+ {
+ "type": "tool_use",
+ "id": "call_123",
+ "name": "search_tool",
+ "input": {"query": "Python tutorials"},
+ },
+ ],
+ },
+ {
+ "role": "user",
+ "content": [
+ {
+ "tool_use_id": "call_123",
+ "type": "tool_result",
+ "content": [
+ {
+ "type": "text",
+ "text": "{'results': ['tutorial1', 'tutorial2']}",
+ }
+ ],
+ }
+ ],
+ },
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "tool_use",
+ "id": "call_456",
+ "name": "search_tool",
+ "input": {"query": "JavaScript tutorials"},
+ }
+ ],
+ },
+ {
+ "role": "user",
+ "content": [
+ {
+ "tool_use_id": "call_456",
+ "type": "tool_result",
+ "content": [
+ {
+ "type": "text",
+ "text": "{'results': ['tutorial3', 'tutorial4']}",
+ }
+ ],
+ }
+ ],
+ },
+ ]
+
+ def test_convert_to_anthropic_messages_single_tool_invocation(self):
+ """Test converting a single tool invocation to proper Anthropic format."""
+ messages = [
+ ChatMessage(
+ role="user",
+ content="Search for Python tutorials",
+ parts=[
+ TextPart(type="text", text="Search for Python tutorials")
+ ],
+ ),
+ ChatMessage(
+ role="assistant",
+ content="",
+ parts=[
+ TextPart(
+ type="text", text="I'll search for Python tutorials"
+ ),
+ ToolInvocationPart(
+ type="tool-search_tool",
+ tool_call_id="call_123",
+ state="output-available",
+ input={"query": "Python tutorials"},
+ output={"results": ["tutorial1", "tutorial2"]},
+ ),
+ ],
+ ),
+ ]
+
+ result = convert_to_anthropic_messages(messages)
+
+ # Should create: user message, assistant message with tool_use, user message with tool_result
+ assert len(result) == 3
+
+ # User message
+ assert result[0] == {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "Search for Python tutorials"}
+ ],
+ }
+
+ # Assistant message with tool_use (should be the last block)
+ assert result[1] == {
+ "role": "assistant",
+ "content": [
+ {"type": "text", "text": "I'll search for Python tutorials"},
+ {
+ "type": "tool_use",
+ "id": "call_123",
+ "name": "search_tool",
+ "input": {"query": "Python tutorials"},
},
+ ],
+ }
+
+ # User message with tool_result
+ assert result[2] == {
+ "role": "user",
+ "content": [
{
- "type": "tool-calculator",
- "tool_call_id": "call_123",
- "state": "output-available",
- "input": {"expression": "2 + 2"},
- "output": {"answer": 4},
+ "tool_use_id": "call_123",
+ "type": "tool_result",
+ "content": [
+ {
+ "type": "text",
+ "text": "{'results': ['tutorial1', 'tutorial2']}",
+ }
+ ],
+ }
+ ],
+ }
+
+ def test_convert_to_anthropic_messages_multiple_tool_invocations(self):
+ """Test converting multiple tool invocations to proper Anthropic format."""
+ messages = [
+ ChatMessage(
+ role="user",
+ content="Search for both Python and JavaScript tutorials",
+ parts=[
+ TextPart(
+ type="text",
+ text="Search for both Python and JavaScript tutorials",
+ )
+ ],
+ ),
+ ChatMessage(
+ role="assistant",
+ content="",
+ parts=[
+ TextPart(type="text", text="I'll search for both topics"),
+ ToolInvocationPart(
+ type="tool-search_tool",
+ tool_call_id="call_123",
+ state="output-available",
+ input={"query": "Python tutorials"},
+ output={"results": ["tutorial1", "tutorial2"]},
+ ),
+ ToolInvocationPart(
+ type="tool-search_tool",
+ tool_call_id="call_456",
+ state="output-available",
+ input={"query": "JavaScript tutorials"},
+ output={"results": ["tutorial3", "tutorial4"]},
+ ),
+ ],
+ ),
+ ]
+
+ result = convert_to_anthropic_messages(messages)
+
+ # Should create: user message, assistant message with first tool_use, user message with first tool_result,
+ # assistant message with second tool_use, user message with second tool_result
+ assert len(result) == 5
+
+ # User message
+ assert result[0] == {
+ "role": "user",
+ "content": [
+ {
+ "type": "text",
+ "text": "Search for both Python and JavaScript tutorials",
+ }
+ ],
+ }
+
+ # First assistant message with first tool_use
+ assert result[1] == {
+ "role": "assistant",
+ "content": [
+ {"type": "text", "text": "I'll search for both topics"},
+ {
+ "type": "tool_use",
+ "id": "call_123",
+ "name": "search_tool",
+ "input": {"query": "Python tutorials"},
},
+ ],
+ }
+
+ # First user message with first tool_result
+ assert result[2] == {
+ "role": "user",
+ "content": [
{
- "type": "image_url",
- "image_url": {"url": "data:image/png;base64,b'aGVsbG8='"},
+ "tool_use_id": "call_123",
+ "type": "tool_result",
+ "content": [
+ {
+ "type": "text",
+ "text": "{'results': ['tutorial1', 'tutorial2']}",
+ }
+ ],
+ }
+ ],
+ }
+
+ # Second assistant message with second tool_use
+ assert result[3] == {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "tool_use",
+ "id": "call_456",
+ "name": "search_tool",
+ "input": {"query": "JavaScript tutorials"},
},
],
}
- ]
+ # Second user message with second tool_result
+ assert result[4] == {
+ "role": "user",
+ "content": [
+ {
+ "tool_use_id": "call_456",
+ "type": "tool_result",
+ "content": [
+ {
+ "type": "text",
+ "text": "{'results': ['tutorial3', 'tutorial4']}",
+ }
+ ],
+ }
+ ],
+ }
+
+ def test_convert_to_anthropic_messages_tool_with_thinking(self):
+ """Test converting tool invocation with thinking block to proper Anthropic format."""
+ messages = [
+ ChatMessage(
+ role="user",
+ content="Search for Python tutorials",
+ parts=[
+ TextPart(type="text", text="Search for Python tutorials")
+ ],
+ ),
+ ChatMessage(
+ role="assistant",
+ content="",
+ parts=[
+ ReasoningPart(
+ type="reasoning",
+ text="The user wants me to search for Python tutorials. I should use the search tool.",
+ details=[],
+ ),
+ TextPart(
+ type="text", text="I'll search for Python tutorials"
+ ),
+ ToolInvocationPart(
+ type="tool-search_tool",
+ tool_call_id="call_123",
+ state="output-available",
+ input={"query": "Python tutorials"},
+ output={"results": ["tutorial1", "tutorial2"]},
+ ),
+ ],
+ ),
+ ]
+
+ result = convert_to_anthropic_messages(messages)
+
+ # Should create: user message, assistant message with thinking + text + tool_use, user message with tool_result
+ assert len(result) == 3
-def test_convert_to_anthropic_messages(
- sample_messages: list[ChatMessage],
-):
- result = convert_to_anthropic_messages(sample_messages)
- assert result == [
# User message
- {
+ assert result[0] == {
"role": "user",
"content": [
- {"type": "text", "text": "Hello, I have a question."},
+ {"type": "text", "text": "Search for Python tutorials"}
+ ],
+ }
+
+ # Assistant message with thinking first, then text, then tool_use
+ assert result[1] == {
+ "role": "assistant",
+ "content": [
{
- "type": "image",
- "source": {
- "type": "base64",
- "media_type": "image/png",
- "data": "b'aGVsbG8='",
- },
+ "type": "thinking",
+ "thinking": "The user wants me to search for Python tutorials. I should use the search tool.",
+ "signature": "",
+ },
+ {"type": "text", "text": "I'll search for Python tutorials"},
+ {
+ "type": "tool_use",
+ "id": "call_123",
+ "name": "search_tool",
+ "input": {"query": "Python tutorials"},
},
- {"type": "text", "text": "A\n1\n2\n3\n"},
],
- },
- # Assistant message
- {
+ }
+
+ # User message with tool_result
+ assert result[2] == {
+ "role": "user",
+ "content": [
+ {
+ "tool_use_id": "call_123",
+ "type": "tool_result",
+ "content": [
+ {
+ "type": "text",
+ "text": "{'results': ['tutorial1', 'tutorial2']}",
+ }
+ ],
+ }
+ ],
+ }
+
+ def test_convert_to_anthropic_messages_tool_with_content_after(self):
+ """Test that content after tool invocation is handled properly."""
+ messages = [
+ ChatMessage(
+ role="assistant",
+ content="",
+ parts=[
+ TextPart(type="text", text="I'll search for you"),
+ ToolInvocationPart(
+ type="tool-search_tool",
+ tool_call_id="call_123",
+ state="output-available",
+ input={"query": "Python tutorials"},
+ output={"results": ["tutorial1", "tutorial2"]},
+ ),
+ TextPart(type="text", text="Here are the results I found"),
+ ],
+ ),
+ ]
+
+ result = convert_to_anthropic_messages(messages)
+
+ # Should create: assistant message with text + tool_use, user message with tool_result,
+ # assistant message with remaining text
+ assert len(result) == 3
+
+ # First assistant message with text and tool_use
+ assert result[0] == {
"role": "assistant",
"content": [
+ {"type": "text", "text": "I'll search for you"},
{
- "type": "text",
- "text": "Sure, I'd be happy to help. What's your question?",
+ "type": "tool_use",
+ "id": "call_123",
+ "name": "search_tool",
+ "input": {"query": "Python tutorials"},
+ },
+ ],
+ }
+
+ # User message with tool_result
+ assert result[1] == {
+ "role": "user",
+ "content": [
+ {
+ "tool_use_id": "call_123",
+ "type": "tool_result",
+ "content": [
+ {
+ "type": "text",
+ "text": "{'results': ['tutorial1', 'tutorial2']}",
+ }
+ ],
}
],
- },
- ]
+ }
+
+ # Second assistant message with remaining text
+ assert result[2] == {
+ "role": "assistant",
+ "content": [
+ {"type": "text", "text": "Here are the results I found"},
+ ],
+ }
+
+ def test_convert_to_anthropic_messages_no_tool_invocation(self):
+ """Test converting messages without tool invocations."""
+ messages = [
+ ChatMessage(
+ role="user",
+ content="Hello",
+ parts=[TextPart(type="text", text="Hello")],
+ ),
+ ChatMessage(
+ role="assistant",
+ content="Hi there!",
+ parts=[TextPart(type="text", text="Hi there!")],
+ ),
+ ]
+
+ result = convert_to_anthropic_messages(messages)
+
+ # Should create: user message, assistant message
+ assert len(result) == 2
+
+ assert result[0] == {
+ "role": "user",
+ "content": [{"type": "text", "text": "Hello"}],
+ }
+
+ assert result[1] == {
+ "role": "assistant",
+ "content": [{"type": "text", "text": "Hi there!"}],
+ }
+
+ def test_convert_to_anthropic_messages_empty_parts(self):
+ """Test converting messages with empty parts."""
+ messages = [
+ ChatMessage(
+ role="user",
+ content="Hello",
+ parts=None,
+ ),
+ ChatMessage(
+ role="assistant",
+ content="Hi there!",
+ parts=[],
+ ),
+ ]
+
+ result = convert_to_anthropic_messages(messages)
+
+ # Should create: user message, assistant message
+ assert len(result) == 2
+
+ assert result[0] == {
+ "role": "user",
+ "content": [{"type": "text", "text": "Hello"}],
+ }
+
+ assert result[1] == {
+ "role": "assistant",
+ "content": [{"type": "text", "text": "Hi there!"}],
+ }
+
+ def test_convert_to_anthropic_messages_file_part(self):
+ """Test converting messages with file parts."""
+ messages = [
+ ChatMessage(
+ role="user",
+ content="Hello",
+ parts=[
+ FilePart(
+ type="file",
+ media_type="text/plain",
+ url="data:text/plain;base64,R29vZGJ5ZQ==",
+ ),
+ FilePart(
+ type="file",
+ media_type="image/png",
+ url="data:image/png;base64,R29vZGJ5ZQ==",
+ ),
+ ],
+ ),
+ ]
+ result = convert_to_anthropic_messages(messages)
+ assert result == [
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "Goodbye"},
+ {
+ "type": "image",
+ "source": {
+ "type": "base64",
+ "media_type": "image/png",
+ "data": "R29vZGJ5ZQ==",
+ },
+ },
+ ],
+ }
+ ]
def test_convert_to_google_messages(
@@ -630,138 +1176,6 @@ def test_get_openai_messages_from_parts_empty():
assert result == []
-def test_get_anthropic_parts_from_chat_parts_text_only():
- """Test converting TextPart to Anthropic format."""
- parts = [
- TextPart(type="text", text="Hello"),
- TextPart(type="text", text="World"),
- ]
-
- result = get_anthropic_parts_from_chat_parts(parts)
- assert result == [
- {"type": "text", "text": "Hello"},
- {"type": "text", "text": "World"},
- ]
-
-
-def test_get_anthropic_parts_from_chat_parts_with_reasoning():
- """Test converting ReasoningPart to Anthropic thinking format."""
- reasoning_details = [
- ReasoningDetails(
- type="text", text="Step 1: Analyze", signature="sig123"
- )
- ]
-
- parts = [
- TextPart(type="text", text="Let me think about this"),
- ReasoningPart(
- type="reasoning",
- text="My thinking process here",
- details=reasoning_details,
- ),
- ]
-
- result = get_anthropic_parts_from_chat_parts(parts)
- assert result == [
- {"type": "text", "text": "Let me think about this"},
- {
- "type": "thinking",
- "thinking": "My thinking process here",
- "signature": "sig123",
- },
- ]
-
-
-def test_get_anthropic_parts_from_chat_parts_reasoning_no_signature():
- """Test converting ReasoningPart without signature to Anthropic format."""
- reasoning_details = [
- ReasoningDetails(type="text", text="Step 1", signature=None)
- ]
-
- parts = [
- ReasoningPart(
- type="reasoning",
- text="Basic reasoning",
- details=reasoning_details,
- ),
- ]
-
- result = get_anthropic_parts_from_chat_parts(parts)
- assert result == [
- {
- "type": "thinking",
- "thinking": "Basic reasoning",
- "signature": "", # Should be empty string when no signature
- }
- ]
-
-
-def test_get_anthropic_parts_from_empty_reasoning_details_chat_part():
- """Test converting ReasoningPart with empty details list."""
- parts = [
- ReasoningPart(
- type="reasoning",
- text="Some reasoning",
- details=[], # Empty details
- ),
- ]
-
- result = get_anthropic_parts_from_chat_parts(parts)
- assert result == [
- {
- "type": "thinking",
- "thinking": "Some reasoning",
- "signature": "", # Should be empty when no details
- }
- ]
-
-
-def test_get_anthropic_parts_from_chat_parts_with_tool_invocation():
- """Test converting ToolInvocationPart to Anthropic format."""
- parts = [
- TextPart(type="text", text="I'll search for you"),
- ToolInvocationPart(
- type="tool-search_tool",
- tool_call_id="call_123",
- state="output-available",
- input={"query": "Python tutorials"},
- output={"results": ["tutorial1", "tutorial2"]},
- ),
- ]
-
- result = get_anthropic_parts_from_chat_parts(parts)
- assert result == [
- # Text message
- {"type": "text", "text": "I'll search for you"},
- # Tool use message
- {
- "type": "tool_use",
- "id": "call_123",
- "name": "search_tool",
- "input": {"query": "Python tutorials"},
- },
- # Tool result message
- {
- "tool_use_id": "call_123",
- "type": "tool_result",
- "content": [
- {
- "type": "text",
- "text": "{'results': ['tutorial1', 'tutorial2']}",
- }
- ],
- },
- ]
-
-
-def test_get_anthropic_parts_from_chat_parts_single_text():
- """Test converting single text part."""
- parts = [TextPart(type="text", text="Single message")]
-
- result = get_anthropic_parts_from_chat_parts(parts)
- assert result == [{"type": "text", "text": "Single message"}]
-
-
def test_get_google_messages_from_parts_text_only():
"""Test converting TextPart to Google format."""
parts = [