diff --git a/src/pipecat/services/anthropic.py b/src/pipecat/services/anthropic.py index f0c033375..d8f485296 100644 --- a/src/pipecat/services/anthropic.py +++ b/src/pipecat/services/anthropic.py @@ -378,6 +378,26 @@ class AnthropicLLMContext(OpenAILLMContext): # convert a message in Anthropic format into one or more messages in OpenAI format def to_standard_messages(self, obj): + """Convert Anthropic message format to standard structured format. + + Handles text content and function calls for both user and assistant messages. + + Args: + obj: Message in Anthropic format: + { + "role": "user/assistant", + "content": str | [{"type": "text/tool_use/tool_result", ...}] + } + + Returns: + List of messages in standard format: + [ + { + "role": "user/assistant/tool", + "content": [{"type": "text", "text": str}] + } + ] + """ # todo: image format (?) # tool_use role = obj.get("role") @@ -432,6 +452,30 @@ class AnthropicLLMContext(OpenAILLMContext): return messages def from_standard_message(self, message): + """Convert standard format message to Anthropic format. + + Handles conversion of text content, tool calls, and tool results. + Empty text content is converted to "(empty)". + + Args: + message: Message in standard format: + { + "role": "user/assistant/tool", + "content": str | [{"type": "text", ...}], + "tool_calls": [{"id": str, "function": {"name": str, "arguments": str}}] + } + + Returns: + Message in Anthropic format: + { + "role": "user/assistant", + "content": str | [ + {"type": "text", "text": str} | + {"type": "tool_use", "id": str, "name": str, "input": dict} | + {"type": "tool_result", "tool_use_id": str, "content": str} + ] + } + """ # todo: image messages (?) if message["role"] == "tool": return { diff --git a/src/pipecat/services/google.py b/src/pipecat/services/google.py index 5442ee91c..383dde624 100644 --- a/src/pipecat/services/google.py +++ b/src/pipecat/services/google.py @@ -412,6 +412,25 @@ class GoogleLLMContext(OpenAILLMContext): # self.add_message(message) def from_standard_message(self, message): + """Convert standard format message to Google Content object. + + Handles conversion of text, images, and function calls to Google's format. + System messages are stored separately and return None. + + Args: + message: Message in standard format: + { + "role": "user/assistant/system/tool", + "content": str | [{"type": "text/image_url", ...}] | None, + "tool_calls": [{"function": {"name": str, "arguments": str}}] + } + + Returns: + glm.Content object with: + - role: "user" or "model" (converted from "assistant") + - parts: List[Part] containing text, inline_data, or function calls + Returns None for system messages. + """ role = message["role"] content = message.get("content", []) if role == "system": @@ -461,6 +480,27 @@ class GoogleLLMContext(OpenAILLMContext): return message def to_standard_messages(self, obj) -> list: + """Convert Google Content object to standard structured format. + + Handles text, images, and function calls from Google's Content/Part objects. + + Args: + obj: Google Content object with: + - role: "model" (converted to "assistant") or "user" + - parts: List[Part] containing text, inline_data, or function calls + + Returns: + List of messages in standard format: + [ + { + "role": "user/assistant/tool", + "content": [ + {"type": "text", "text": str} | + {"type": "image_url", "image_url": {"url": str}} + ] + } + ] + """ msg = {"role": obj.role, "content": []} if msg["role"] == "model": msg["role"] = "assistant"