The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(factory.py): support assistant messages as a list of dictionaries - cohere messages api
Krrish Dholakia9b46ec05b02d
diff --git a/litellm/llms/prompt_templates/factory.py b/litellm/llms/prompt_templates/factory.pyindex 7c3c7e80fbc..f39273c1a2f 100644--- a/litellm/llms/prompt_templates/factory.py+++ b/litellm/llms/prompt_templates/factory.py@@ -1701,12 +1701,12 @@ def cohere_messages_pt_v2(assistant_tool_calls: List[ToolCallObject] = []## MERGE CONSECUTIVE ASSISTANT CONTENT ##while msg_i < len(messages) and messages[msg_i]["role"] == "assistant":- assistant_text = (- messages[msg_i].get("content") or ""- ) # either string or none- if assistant_text:- assistant_content += assistant_text-+ if isinstance(messages[msg_i]["content"], list):+ for m in messages[msg_i]["content"]:+ if m.get("type", "") == "text":+ assistant_content += m["text"]+ else:+ assistant_content += messages[msg_i]["content"]if messages[msg_i].get("tool_calls", []): # support assistant tool invoke conversiondiff --git a/litellm/tests/test_completion.py b/litellm/tests/test_completion.pyindex 4ea9ee3b0f3..83031aba085 100644--- a/litellm/tests/test_completion.py+++ b/litellm/tests/test_completion.py@@ -3705,19 +3705,21 @@ def test_completion_anyscale_api():# test_completion_anyscale_api()[email protected](reason="flaky test, times out frequently")+# @pytest.mark.skip(reason="flaky test, times out frequently")def test_completion_cohere():try:# litellm.set_verbose=Truemessages = [{"role": "system", "content": "You're a good bot"},+ {"role": "assistant", "content": [{"text": "2", "type": "text"}]},+ {"role": "assistant", "content": [{"text": "3", "type": "text"}]},{"role": "user","content": "Hey",},]response = completion(- model="command-nightly",+ model="command-r",messages=messages,)print(response)