From 90a4dfab3c044a7795642ad8cc28bbc04fcfc276 Mon Sep 17 00:00:00 2001 From: Krish Dholakia Date: Fri, 4 Apr 2025 20:37:08 -0700 Subject: [PATCH] =?UTF-8?q?fix(xai/chat/transformation.py):=20filter=20out?= =?UTF-8?q?=20'name'=20param=20for=20xai=20non-=E2=80=A6=20(#9761)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(xai/chat/transformation.py): filter out 'name' param for xai non-user roles Fixes https://github.com/BerriAI/litellm/issues/9720 * test fix test_hf_chat_template --------- Co-authored-by: Ishaan Jaff --- .../prompt_templates/common_utils.py | 4 ++-- litellm/llms/xai/chat/transformation.py | 24 ++++++++++++++++++- tests/llm_translation/test_xai.py | 18 ++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/litellm/litellm_core_utils/prompt_templates/common_utils.py b/litellm/litellm_core_utils/prompt_templates/common_utils.py index 9ba1153c08..8d3845969a 100644 --- a/litellm/litellm_core_utils/prompt_templates/common_utils.py +++ b/litellm/litellm_core_utils/prompt_templates/common_utils.py @@ -35,7 +35,7 @@ def handle_messages_with_content_list_to_str_conversion( def strip_name_from_messages( - messages: List[AllMessageValues], + messages: List[AllMessageValues], allowed_name_roles: List[str] = ["user"] ) -> List[AllMessageValues]: """ Removes 'name' from messages @@ -44,7 +44,7 @@ def strip_name_from_messages( for message in messages: msg_role = message.get("role") msg_copy = message.copy() - if msg_role == "user": + if msg_role not in allowed_name_roles: msg_copy.pop("name", None) # type: ignore new_messages.append(msg_copy) return new_messages diff --git a/litellm/llms/xai/chat/transformation.py b/litellm/llms/xai/chat/transformation.py index 734c6eb2e0..614509020e 100644 --- a/litellm/llms/xai/chat/transformation.py +++ b/litellm/llms/xai/chat/transformation.py @@ -1,6 +1,10 @@ -from typing import Optional, Tuple +from typing import List, Optional, Tuple +from litellm.litellm_core_utils.prompt_templates.common_utils import ( + strip_name_from_messages, +) from litellm.secret_managers.main import get_secret_str +from litellm.types.llms.openai import AllMessageValues from ...openai.chat.gpt_transformation import OpenAIGPTConfig @@ -51,3 +55,21 @@ class XAIChatConfig(OpenAIGPTConfig): if value is not None: optional_params[param] = value return optional_params + + def transform_request( + self, + model: str, + messages: List[AllMessageValues], + optional_params: dict, + litellm_params: dict, + headers: dict, + ) -> dict: + """ + Handle https://github.com/BerriAI/litellm/issues/9720 + + Filter out 'name' from messages + """ + messages = strip_name_from_messages(messages) + return super().transform_request( + model, messages, optional_params, litellm_params, headers + ) diff --git a/tests/llm_translation/test_xai.py b/tests/llm_translation/test_xai.py index de4bfc907d..3846a4f1f0 100644 --- a/tests/llm_translation/test_xai.py +++ b/tests/llm_translation/test_xai.py @@ -142,3 +142,21 @@ def test_completion_xai(stream): assert response.choices[0].message.content is not None except Exception as e: pytest.fail(f"Error occurred: {e}") + + +def test_xai_message_name_filtering(): + messages = [ + { + "role": "system", + "content": "*I press the green button*", + "name": "example_user" + }, + {"role": "user", "content": "Hello", "name": "John"}, + {"role": "assistant", "content": "Hello", "name": "Jane"}, + ] + response = completion( + model="xai/grok-beta", + messages=messages, + ) + assert response is not None + assert response.choices[0].message.content is not None