From 0db932802ea3ebbc4e409ace7583c93d454dc91b Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 13 Nov 2026 22:01:34 -0800 Subject: [PATCH] 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 --- .../transformation.py | 10 +++-- ...responses_transformation_transformation.py | 41 +++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/litellm/completion_extras/litellm_responses_transformation/transformation.py b/litellm/completion_extras/litellm_responses_transformation/transformation.py index 6ccd18da6ea..c37a666071b 100644 --- a/litellm/completion_extras/litellm_responses_transformation/transformation.py +++ b/litellm/completion_extras/litellm_responses_transformation/transformation.py @@ -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}" diff --git a/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py b/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py index d0c7ba9e3bb..0cba58aa8e4 100644 --- a/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py +++ b/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py @@ -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()