From c04b921f6f8192d20de600adeedf9c5b7d46c13d Mon Sep 17 00:00:00 2001 From: shivam Date: Thu, 18 Dec 2025 02:18:08 -0800 Subject: [PATCH] added functionality to accept list content handling in bedrock cohere models --- .../prompt_templates/factory.py | 21 +++++++++++- .../test_cohere_message_pt.py | 32 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 tests/test_litellm/litellm_core_utils/prompt_templates/test_cohere_message_pt.py diff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.py index 652692c7b8d..ef83f279490 100644 --- a/litellm/litellm_core_utils/prompt_templates/factory.py +++ b/litellm/litellm_core_utils/prompt_templates/factory.py @@ -2580,7 +2580,26 @@ def cohere_message_pt(messages: list): ) tool_results.append(tool_result) elif message.get("content"): - prompt += message["content"] + "\n\n" + content = message["content"] + + # Support both string content and OpenAI-style list-of-parts content + # e.g. [{"type": "text", "text": "Hi"}] + if isinstance(content, list): + text_parts: List[str] = [] + for part in content: + if isinstance(part, dict): + # Prefer explicit text field + if part.get("type") == "text" and "text" in part: + text_parts.append(str(part["text"])) + elif "text" in part: + text_parts.append(str(part["text"])) + elif isinstance(part, str): + text_parts.append(part) + + if len(text_parts) > 0: + prompt += "".join(text_parts) + "\n\n" + else: + prompt += str(content) + "\n\n" prompt = prompt.rstrip() return prompt, tool_results diff --git a/tests/test_litellm/litellm_core_utils/prompt_templates/test_cohere_message_pt.py b/tests/test_litellm/litellm_core_utils/prompt_templates/test_cohere_message_pt.py new file mode 100644 index 00000000000..b0b235050f7 --- /dev/null +++ b/tests/test_litellm/litellm_core_utils/prompt_templates/test_cohere_message_pt.py @@ -0,0 +1,32 @@ +import pytest + +from litellm.litellm_core_utils.prompt_templates.factory import cohere_message_pt + + +def test_cohere_message_pt_with_string_content(): + messages = [{"role": "user", "content": "Hi"}] + + prompt, tool_results = cohere_message_pt(messages) + + assert prompt == "Hi" + assert tool_results == [] + + +def test_cohere_message_pt_with_list_content_text_part(): + messages = [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Hi", + } + ], + } + ] + + prompt, tool_results = cohere_message_pt(messages) + + assert prompt == "Hi" + assert tool_results == [] +