From 77aeda36eba644bd4471f98a0fd0d11ba94fbf2f Mon Sep 17 00:00:00 2001 From: Mark Backman Date: Sat, 14 Dec 2024 09:23:43 -0500 Subject: [PATCH] Update OpenAI's from_standard_message to convert back to OpenAI's simple format --- .../aggregators/openai_llm_context.py | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/pipecat/processors/aggregators/openai_llm_context.py b/src/pipecat/processors/aggregators/openai_llm_context.py index e84584053..4adf76de0 100644 --- a/src/pipecat/processors/aggregators/openai_llm_context.py +++ b/src/pipecat/processors/aggregators/openai_llm_context.py @@ -112,7 +112,38 @@ class OpenAILLMContext: msgs.append(msg) return json.dumps(msgs) - def from_standard_message(self, message): + def from_standard_message(self, message) -> dict: + """Convert standard format message to OpenAI format. + + Converts structured content back to OpenAI's simple string format. + + Args: + message: Message in standard format: + { + "role": "user/assistant", + "content": [{"type": "text", "text": str}] + } + + Returns: + Message in OpenAI format: + { + "role": "user/assistant", + "content": str + } + """ + # If content is already a string, return as-is + if isinstance(message.get("content"), str): + return message + + # Convert structured content to string + if isinstance(message.get("content"), list): + text_parts = [] + for part in message["content"]: + if part.get("type") == "text": + text_parts.append(part["text"]) + + return {"role": message["role"], "content": " ".join(text_parts) if text_parts else ""} + return message def to_standard_messages(self, obj) -> list: