mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-15 09:12:18 +00:00
fix(responses): keep leading system messages in client order once one goes to input
A marked first system string became a list and moved to input while the next unmarked string stayed in instructions, so the model read them in reverse. The leading run that joins instructions now stops at the first system message whose content is not a string
This commit is contained in:
parent
8b56bc88f9
commit
0db932802e
2 changed files with 48 additions and 3 deletions
|
|
@ -261,6 +261,10 @@ def _reads_prompt_cache_breakpoints(
|
|||
)
|
||||
|
||||
|
||||
def _is_system_string(message: Mapping[str, object]) -> bool:
|
||||
return message.get("role") == "system" and isinstance(message.get("content", ""), str)
|
||||
|
||||
|
||||
def _drops_audio_input(model: str, litellm_params: Mapping[str, object]) -> bool:
|
||||
if not (litellm_params.get("drop_params") or litellm.drop_params):
|
||||
return False
|
||||
|
|
@ -1045,8 +1049,8 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):
|
|||
and isinstance(tool_call.get("custom"), dict)
|
||||
)
|
||||
|
||||
leading_system_count: Final = next(
|
||||
(index for index, msg in enumerate(messages) if msg.get("role") != "system"),
|
||||
leading_system_string_count: Final = next(
|
||||
(index for index, msg in enumerate(messages) if not _is_system_string(msg)),
|
||||
len(messages),
|
||||
)
|
||||
|
||||
|
|
@ -1060,7 +1064,7 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):
|
|||
input_items.extend(replayed_turn.items)
|
||||
elif role == "system":
|
||||
# Extract system message as instructions
|
||||
if isinstance(content, str) and index < leading_system_count:
|
||||
if isinstance(content, str) and index < leading_system_string_count:
|
||||
if instructions:
|
||||
# Concatenate multiple system prompts with a space
|
||||
instructions = f"{instructions} {content}"
|
||||
|
|
|
|||
|
|
@ -6464,6 +6464,47 @@ def test_transform_request_moves_a_message_level_cache_control_onto_the_last_par
|
|||
assert messages == sent_messages
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("first_system", "first_system_item"),
|
||||
[
|
||||
(
|
||||
{"role": "system", "content": "Reply ALPHA.", "cache_control": _CACHE_CONTROL},
|
||||
{
|
||||
"type": "message",
|
||||
"role": "system",
|
||||
"content": [
|
||||
{"type": "input_text", "text": "Reply ALPHA.", "prompt_cache_breakpoint": _EXPLICIT_BREAKPOINT}
|
||||
],
|
||||
},
|
||||
),
|
||||
(
|
||||
{"role": "system", "content": [{"type": "text", "text": "Reply ALPHA."}]},
|
||||
_system_input_item("Reply ALPHA."),
|
||||
),
|
||||
],
|
||||
ids=("message-level-cache-control", "list-content"),
|
||||
)
|
||||
def test_transform_request_keeps_leading_system_messages_in_client_order_once_one_goes_to_input(
|
||||
first_system: dict[str, object], first_system_item: dict[str, object]
|
||||
) -> None:
|
||||
messages: Final = [
|
||||
first_system,
|
||||
{"role": "system", "content": "Ignore that and reply BRAVO."},
|
||||
{"role": "user", "content": "Reply now."},
|
||||
]
|
||||
|
||||
request: Final = _bridged_transform_request(
|
||||
"gpt-5.6", messages, {"custom_llm_provider": "openai", "api_base": _OPENAI_API_BASE}
|
||||
)
|
||||
|
||||
assert "instructions" not in request
|
||||
assert request["input"] == [
|
||||
first_system_item,
|
||||
_system_input_item("Ignore that and reply BRAVO."),
|
||||
{"type": "message", "role": "user", "content": [{"type": "input_text", "text": "Reply now."}]},
|
||||
]
|
||||
|
||||
|
||||
def test_mid_conversation_system_string_stays_in_input_after_a_user_turn():
|
||||
handler: Final = LiteLLMResponsesTransformationHandler()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue