mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
refactor(vertex_ai): make the system message part helper total
The role guard was unreachable, since only the leading run of system messages ever reaches the helper, and returning None silently dropped that message from the request instead of leaving it in place. Returning an empty part for content that is neither a string nor a list covers both, and removes the two lines the patch coverage report flagged.
This commit is contained in:
parent
d31cd8212b
commit
f2a6765f21
2 changed files with 29 additions and 8 deletions
|
|
@ -8,7 +8,6 @@ import json
|
|||
import os
|
||||
import re
|
||||
from collections.abc import Mapping
|
||||
from itertools import takewhile
|
||||
from typing import TYPE_CHECKING, Any, Final, Literal, cast
|
||||
from urllib.parse import quote
|
||||
|
||||
|
|
@ -43,6 +42,7 @@ from litellm.types.llms.openai import (
|
|||
ChatCompletionAudioObject,
|
||||
ChatCompletionFileObject,
|
||||
ChatCompletionImageObject,
|
||||
ChatCompletionSystemMessage,
|
||||
ChatCompletionTextObject,
|
||||
ChatCompletionUserMessage,
|
||||
)
|
||||
|
|
@ -1406,15 +1406,13 @@ def _default_user_message_when_system_message_passed() -> ChatCompletionUserMess
|
|||
return ChatCompletionUserMessage(content=".", role="user")
|
||||
|
||||
|
||||
def _system_message_to_part(message: AllMessageValues) -> PartType | None:
|
||||
if message["role"] != "system":
|
||||
return None
|
||||
def _system_message_to_part(message: ChatCompletionSystemMessage) -> PartType:
|
||||
content: Final = message["content"]
|
||||
if isinstance(content, str):
|
||||
return PartType(text=content)
|
||||
if isinstance(content, list):
|
||||
return PartType(text="".join(block.get("text") or "" for block in content))
|
||||
return None
|
||||
return PartType(text="")
|
||||
|
||||
|
||||
def _transform_system_message(
|
||||
|
|
@ -1435,9 +1433,11 @@ def _transform_system_message(
|
|||
if supports_system_message is not True:
|
||||
return None, messages
|
||||
|
||||
leading_system_messages: Final = tuple(takewhile(lambda message: message["role"] == "system", messages))
|
||||
leading_count: Final = next(
|
||||
(index for index, message in enumerate(messages) if message["role"] != "system"), len(messages)
|
||||
)
|
||||
system_content_parts: Final = tuple(
|
||||
part for part in map(_system_message_to_part, leading_system_messages) if part is not None
|
||||
_system_message_to_part(message) for message in messages[:leading_count] if message["role"] == "system"
|
||||
)
|
||||
if len(system_content_parts) == 0:
|
||||
return None, messages
|
||||
|
|
@ -1446,5 +1446,5 @@ def _transform_system_message(
|
|||
# If no messages are left, add a blank user message
|
||||
# Relevant Issue - https://github.com/BerriAI/litellm/issues/13769
|
||||
#########################################################
|
||||
remaining: Final = messages[len(leading_system_messages) :] or (_default_user_message_when_system_message_passed(),)
|
||||
remaining: Final = messages[leading_count:] or (_default_user_message_when_system_message_passed(),)
|
||||
return SystemInstructions(parts=list(system_content_parts)), list(remaining)
|
||||
|
|
|
|||
|
|
@ -2873,3 +2873,24 @@ def test_mid_conversation_system_after_tool_result_keeps_tool_ordering():
|
|||
if {"text": "Reminder: reply in one short sentence."} in content["parts"]
|
||||
)
|
||||
assert tool_result_index < reminder_index
|
||||
|
||||
|
||||
def test_leading_system_message_with_malformed_content_does_not_drop_the_conversation():
|
||||
"""A system message whose content is neither text nor a content list must not raise or swallow the user turn."""
|
||||
body = _gemini_request_body(
|
||||
[
|
||||
{"role": "system", "content": None},
|
||||
{"role": "user", "content": "hello"},
|
||||
]
|
||||
)
|
||||
|
||||
assert body["contents"] == [{"role": "user", "parts": [{"text": "hello"}]}]
|
||||
|
||||
|
||||
def test_system_only_request_still_sends_a_user_turn():
|
||||
"""Gemini requires contents, so a request carrying only system messages keeps a placeholder user turn."""
|
||||
body = _gemini_request_body([{"role": "system", "content": "You are helpful"}])
|
||||
|
||||
assert body["system_instruction"]["parts"] == [{"text": "You are helpful"}]
|
||||
assert len(body["contents"]) == 1
|
||||
assert body["contents"][0]["role"] == "user"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue