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]

address greptile review feedback (greploop iteration 1)

Sameer Kankute67f5ce9c7c23
diff --git a/litellm/litellm_core_utils/prompt_templates/common_utils.py b/litellm/litellm_core_utils/prompt_templates/common_utils.py
index d7c1cc708b0..8713d0283ed 100644
--- a/litellm/litellm_core_utils/prompt_templates/common_utils.py
+++ b/litellm/litellm_core_utils/prompt_templates/common_utils.py
@@ -287,11 +287,9 @@ def _insert_user_continue_message(
result_messages = messages.copy() # Don't modify the input list
continue_message = user_continue_message or DEFAULT_USER_CONTINUE_MESSAGE
- # Handle first counted message if it's an assistant message
- if (
- result_messages[0]["role"] == "assistant"
- and _counts_for_alternation(result_messages[0])
- ):
+ # Handle first message if it's an assistant message — always prepend
+ # user_continue regardless of tool_calls, to preserve backward compatibility.
+ if result_messages[0]["role"] == "assistant":
result_messages.insert(0, continue_message)
# Handle consecutive assistant messages in the counted sequence
diff --git a/tests/llm_translation/test_prompt_factory.py b/tests/llm_translation/test_prompt_factory.py
index b02ed3ebea1..3a9f267e6a0 100644
--- a/tests/llm_translation/test_prompt_factory.py
+++ b/tests/llm_translation/test_prompt_factory.py
@@ -929,12 +929,25 @@ def test_ensure_alternating_roles_trailing_tool_call_assistant():
ensure_alternating_roles=True,
)
- # Backward compat: trailing assistant (even with tool_calls) gets user_continue
- # appended, then assistant_continue bridges the user→user gap.
- assert transformed_messages[-1] == {"role": "user", "content": "Please continue."}
- assert transformed_messages[0] == {"role": "user", "content": "What's the weather?"}
- assert transformed_messages[1]["role"] == "assistant"
- assert transformed_messages[1].get("tool_calls") is not None
+ assert transformed_messages == [
+ {"role": "user", "content": "What's the weather?"},
+ {
+ "role": "assistant",
+ "content": None,
+ "tool_calls": [
+ {
+ "id": "call_abc",
+ "type": "function",
+ "function": {
+ "name": "get_weather",
+ "arguments": '{"location": "NYC"}',
+ },
+ }
+ ],
+ },
+ {"role": "assistant", "content": "Please continue."},
+ {"role": "user", "content": "Please continue."},
+ ]
def test_alternating_roles_e2e():