mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-08 03:08:45 +00:00
fix(logging): keep system prompt only in system_prompt field
Stop prepending string system prompts into messages when building StandardLoggingPayload. With the dedicated system_prompt field, duplicating string prompts in messages was inconsistent with list/block prompts. Removes append_system_prompt_messages and updates tests to assert both string and block system prompts stay separate from messages.
This commit is contained in:
parent
76146880dc
commit
cc4d13c270
2 changed files with 35 additions and 104 deletions
|
|
@ -4724,31 +4724,6 @@ class StandardLoggingPayloadSetup:
|
|||
return value
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def append_system_prompt_messages(kwargs: dict | None = None, messages: Any | None = None):
|
||||
"""
|
||||
Append system prompt messages to the messages
|
||||
"""
|
||||
if kwargs is not None:
|
||||
if kwargs.get("system") is not None and isinstance(kwargs.get("system"), str):
|
||||
if messages is None:
|
||||
return [{"role": "system", "content": kwargs.get("system")}]
|
||||
elif isinstance(messages, list):
|
||||
if len(messages) == 0:
|
||||
return [{"role": "system", "content": kwargs.get("system")}]
|
||||
# check for duplicates
|
||||
if messages[0].get("role") == "system" and messages[0].get("content") == kwargs.get("system"):
|
||||
return messages
|
||||
messages = [{"role": "system", "content": kwargs.get("system")}] + messages
|
||||
elif isinstance(messages, str):
|
||||
messages = [
|
||||
{"role": "system", "content": kwargs.get("system")},
|
||||
{"role": "user", "content": messages},
|
||||
]
|
||||
return messages
|
||||
|
||||
return messages
|
||||
|
||||
@staticmethod
|
||||
def merge_litellm_metadata(litellm_params: dict) -> dict:
|
||||
"""
|
||||
|
|
@ -5644,11 +5619,7 @@ def get_standard_logging_object_payload(
|
|||
model_id=_model_id,
|
||||
requester_ip_address=clean_metadata.get("requester_ip_address", None),
|
||||
user_agent=clean_metadata.get("user_agent", None),
|
||||
messages=truncate_base64_in_messages(
|
||||
StandardLoggingPayloadSetup.append_system_prompt_messages(
|
||||
kwargs=kwargs, messages=kwargs.get("messages")
|
||||
)
|
||||
),
|
||||
messages=truncate_base64_in_messages(kwargs.get("messages")),
|
||||
system_prompt=StandardLoggingPayloadSetup.get_system_prompt_from_kwargs(kwargs=kwargs),
|
||||
response=final_response_obj,
|
||||
model_parameters=ModelParamHelper.get_standard_logging_model_parameters(
|
||||
|
|
|
|||
|
|
@ -2182,65 +2182,6 @@ def test_get_usage_as_dict():
|
|||
assert result == {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0}
|
||||
|
||||
|
||||
def test_append_system_prompt_messages():
|
||||
"""
|
||||
Test append_system_prompt_messages prepends system message from kwargs to messages list.
|
||||
"""
|
||||
from litellm.litellm_core_utils.litellm_logging import StandardLoggingPayloadSetup
|
||||
|
||||
# Test case 1: system in kwargs with existing messages
|
||||
kwargs = {"system": "You are a helpful assistant"}
|
||||
messages = [{"role": "user", "content": "Hello"}]
|
||||
result = StandardLoggingPayloadSetup.append_system_prompt_messages(
|
||||
kwargs=kwargs, messages=messages
|
||||
)
|
||||
assert len(result) == 2
|
||||
assert result[0] == {"role": "system", "content": "You are a helpful assistant"}
|
||||
assert result[1] == {"role": "user", "content": "Hello"}
|
||||
|
||||
# Test case 2: system in kwargs with None messages
|
||||
kwargs = {"system": "You are a helpful assistant"}
|
||||
result = StandardLoggingPayloadSetup.append_system_prompt_messages(
|
||||
kwargs=kwargs, messages=None
|
||||
)
|
||||
assert len(result) == 1
|
||||
assert result[0] == {"role": "system", "content": "You are a helpful assistant"}
|
||||
|
||||
# Test case 3: system in kwargs with empty messages list
|
||||
kwargs = {"system": "You are a helpful assistant"}
|
||||
result = StandardLoggingPayloadSetup.append_system_prompt_messages(
|
||||
kwargs=kwargs, messages=[]
|
||||
)
|
||||
assert len(result) == 1
|
||||
assert result[0] == {"role": "system", "content": "You are a helpful assistant"}
|
||||
|
||||
# Test case 4: duplicate system message should not be added
|
||||
kwargs = {"system": "You are a helpful assistant"}
|
||||
messages = [
|
||||
{"role": "system", "content": "You are a helpful assistant"},
|
||||
{"role": "user", "content": "Hello"},
|
||||
]
|
||||
result = StandardLoggingPayloadSetup.append_system_prompt_messages(
|
||||
kwargs=kwargs, messages=messages
|
||||
)
|
||||
assert len(result) == 2
|
||||
assert result[0] == {"role": "system", "content": "You are a helpful assistant"}
|
||||
|
||||
# Test case 5: no system in kwargs returns messages unchanged
|
||||
kwargs = {}
|
||||
messages = [{"role": "user", "content": "Hello"}]
|
||||
result = StandardLoggingPayloadSetup.append_system_prompt_messages(
|
||||
kwargs=kwargs, messages=messages
|
||||
)
|
||||
assert result == messages
|
||||
|
||||
# Test case 6: None kwargs returns messages unchanged
|
||||
result = StandardLoggingPayloadSetup.append_system_prompt_messages(
|
||||
kwargs=None, messages=messages
|
||||
)
|
||||
assert result == messages
|
||||
|
||||
|
||||
def test_get_system_prompt_from_kwargs():
|
||||
from litellm.litellm_core_utils.litellm_logging import StandardLoggingPayloadSetup
|
||||
|
||||
|
|
@ -2282,21 +2223,12 @@ def test_get_system_prompt_from_kwargs():
|
|||
assert StandardLoggingPayloadSetup.get_system_prompt_from_kwargs(kwargs=None) is None
|
||||
|
||||
|
||||
def test_get_standard_logging_object_payload_includes_system_prompt_for_list_system(logging_obj):
|
||||
def test_get_standard_logging_object_payload_keeps_system_prompt_separate_from_messages(logging_obj):
|
||||
import datetime
|
||||
|
||||
from litellm.litellm_core_utils.litellm_logging import get_standard_logging_object_payload
|
||||
|
||||
system_blocks = [
|
||||
{"type": "text", "text": "SHAPE-SECRET", "cache_control": {"type": "ephemeral"}},
|
||||
]
|
||||
user_messages = [{"role": "user", "content": "hello"}]
|
||||
kwargs = {
|
||||
"model": "anthropic/claude-sonnet-4-5",
|
||||
"system": system_blocks,
|
||||
"messages": user_messages,
|
||||
"litellm_params": {},
|
||||
}
|
||||
mock_response = {
|
||||
"id": "msg_123",
|
||||
"type": "message",
|
||||
|
|
@ -2307,8 +2239,17 @@ def test_get_standard_logging_object_payload_includes_system_prompt_for_list_sys
|
|||
}
|
||||
now = datetime.datetime.now()
|
||||
|
||||
payload = get_standard_logging_object_payload(
|
||||
kwargs=kwargs,
|
||||
system_blocks = [
|
||||
{"type": "text", "text": "SHAPE-SECRET", "cache_control": {"type": "ephemeral"}},
|
||||
]
|
||||
list_kwargs = {
|
||||
"model": "anthropic/claude-sonnet-4-5",
|
||||
"system": system_blocks,
|
||||
"messages": user_messages,
|
||||
"litellm_params": {},
|
||||
}
|
||||
list_payload = get_standard_logging_object_payload(
|
||||
kwargs=list_kwargs,
|
||||
init_response_obj=mock_response,
|
||||
start_time=now,
|
||||
end_time=now,
|
||||
|
|
@ -2316,9 +2257,28 @@ def test_get_standard_logging_object_payload_includes_system_prompt_for_list_sys
|
|||
status="success",
|
||||
)
|
||||
|
||||
assert payload is not None
|
||||
assert payload["system_prompt"] == system_blocks
|
||||
assert payload["messages"] == user_messages
|
||||
assert list_payload is not None
|
||||
assert list_payload["system_prompt"] == system_blocks
|
||||
assert list_payload["messages"] == user_messages
|
||||
|
||||
string_kwargs = {
|
||||
"model": "anthropic/claude-sonnet-4-5",
|
||||
"system": "Be helpful",
|
||||
"messages": user_messages,
|
||||
"litellm_params": {},
|
||||
}
|
||||
string_payload = get_standard_logging_object_payload(
|
||||
kwargs=string_kwargs,
|
||||
init_response_obj=mock_response,
|
||||
start_time=now,
|
||||
end_time=now,
|
||||
logging_obj=logging_obj,
|
||||
status="success",
|
||||
)
|
||||
|
||||
assert string_payload is not None
|
||||
assert string_payload["system_prompt"] == "Be helpful"
|
||||
assert string_payload["messages"] == user_messages
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue