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(factory.py): Handle system only message to anthropic (#10678)

Krish Dholakia5325ee4382da
diff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.py
index 190fad0daa3..6ec793a11a2 100644
--- a/litellm/litellm_core_utils/prompt_templates/factory.py
+++ b/litellm/litellm_core_utils/prompt_templates/factory.py
@@ -55,6 +55,11 @@ DEFAULT_USER_CONTINUE_MESSAGE = {
"content": "Please continue.",
} # similar to autogen. Only used if `litellm.modify_params=True`.
+DEFAULT_USER_CONTINUE_MESSAGE_TYPED = ChatCompletionUserMessage(
+ role="user",
+ content="Please continue.",
+)
+
# used to interweave assistant messages, to ensure user/assistant alternating
DEFAULT_ASSISTANT_CONTINUE_MESSAGE = ChatCompletionAssistantMessage(
role="assistant",
@@ -1408,6 +1413,17 @@ def anthropic_messages_pt( # noqa: PLR0915
AnthopicMessagesAssistantMessageParam,
]
] = []
+
+ if len(messages) == 0:
+ if not litellm.modify_params:
+ raise litellm.BadRequestError(
+ message=f"Anthropic requires at least one non-system message. Either provide one, or set `litellm.modify_params = True` // `litellm_settings::modify_params: True` to add the dummy user message - {DEFAULT_USER_CONTINUE_MESSAGE_TYPED}.",
+ model=model,
+ llm_provider=llm_provider,
+ )
+ else:
+ messages.append(DEFAULT_USER_CONTINUE_MESSAGE_TYPED)
+
msg_i = 0
while msg_i < len(messages):
user_content: List[AnthropicMessagesUserMessageValues] = []
@@ -1613,7 +1629,7 @@ def anthropic_messages_pt( # noqa: PLR0915
llm_provider=llm_provider,
)
- if new_messages[-1]["role"] == "assistant":
+ if len(new_messages) > 0 and new_messages[-1]["role"] == "assistant":
if isinstance(new_messages[-1]["content"], str):
new_messages[-1]["content"] = new_messages[-1]["content"].rstrip()
elif isinstance(new_messages[-1]["content"], list):
diff --git a/tests/llm_translation/test_anthropic_completion.py b/tests/llm_translation/test_anthropic_completion.py
index 5d32b8ee0e5..3f6f17a8a45 100644
--- a/tests/llm_translation/test_anthropic_completion.py
+++ b/tests/llm_translation/test_anthropic_completion.py
@@ -1157,3 +1157,16 @@ def test_anthropic_redacted_thinking_in_assistant_message(model):
response = litellm.completion(**params)
assert response is not None
+
+
+def test_just_system_message():
+ litellm._turn_on_debug()
+ litellm.modify_params = True
+ params = {
+ "model": "anthropic/claude-3-7-sonnet-20250219",
+ "messages": [{"role": "system", "content": "You are a helpful assistant."}],
+ }
+
+ response = litellm.completion(**params)
+
+ assert response is not None