From c0336d3f40deb3d3017e143ade99047f1c1980ae Mon Sep 17 00:00:00 2001 From: Emir Ayar Date: Sun, 31 Mar 2024 22:43:30 +0200 Subject: [PATCH 1/2] add a third condition: list of text-content dictionaries --- litellm/llms/prompt_templates/factory.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/litellm/llms/prompt_templates/factory.py b/litellm/llms/prompt_templates/factory.py index 4492423f45d..30964f7329c 100644 --- a/litellm/llms/prompt_templates/factory.py +++ b/litellm/llms/prompt_templates/factory.py @@ -684,9 +684,15 @@ def anthropic_messages_pt(messages: list): assistant_content = [] ## 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 + # Handle assistant messages as string, none, or list of text-content dictionaries. + if isinstance(messages[msg_i].get("content"), list): + assistant_text = '' + for content in messages[msg_i]["content"]: + if content.get("type") == "text": + assistant_text += content["text"] + else: + assistant_text = messages[msg_i].get("content") or "" + if messages[msg_i].get( "tool_calls", [] ): # support assistant tool invoke convertion From 2ecbf6663a70dbee13520fda133f74142a9df017 Mon Sep 17 00:00:00 2001 From: Emir Ayar Date: Sat, 27 Apr 2024 12:27:12 +0200 Subject: [PATCH 2/2] Add test for completion with text content dictionaries --- litellm/tests/test_completion.py | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/litellm/tests/test_completion.py b/litellm/tests/test_completion.py index d2c004a0ab1..e2c635a5cb7 100644 --- a/litellm/tests/test_completion.py +++ b/litellm/tests/test_completion.py @@ -230,6 +230,48 @@ def test_completion_claude_3_function_call(): except Exception as e: pytest.fail(f"Error occurred: {e}") +def test_completion_claude_3_with_text_content_dictionaries(): + litellm.set_verbose = True + messages = [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Hello" + } + ] + }, + { + "role": "assistant", + "content": [ + { + "type": "text", + "text": "Hello! How can I assist you today?" + } + ] + }, + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Hello again!" + } + ] + } + ] + + try: + # test without max tokens + response = completion( + model="anthropic/claude-3-opus-20240229", + messages=messages, + ) + # Add any assertions, here to check response args + print(response) + except Exception as e: + pytest.fail(f"Error occurred: {e}") def test_parse_xml_params(): from litellm.llms.prompt_templates.factory import parse_xml_params