added functionality to accept list content handling in bedrock cohere models

This commit is contained in:
shivam 2025-12-18 02:18:08 -08:00
parent 202f71c13a
commit c04b921f6f
2 changed files with 52 additions and 1 deletions

View file

@ -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

View file

@ -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 == []