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:
mateo-berri 2026-11-13 22:01:34 -08:00
parent 8b56bc88f9
commit 0db932802e
2 changed files with 48 additions and 3 deletions

View file

@ -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}"

View file

@ -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()