Skip to content

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]

fix(xai/chat/transformation.py): filter out 'name' param for xai non-… (#9761)

Krish Dholakia90a4dfab3c04
diff --git a/litellm/litellm_core_utils/prompt_templates/common_utils.py b/litellm/litellm_core_utils/prompt_templates/common_utils.py
index 9ba1153c08c..8d3845969a7 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 734c6eb2e08..614509020e4 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 de4bfc907d0..3846a4f1f0e 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