From 27c828d3bc5dd10463b7567d0ea3aa627c77f7f9 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 13 Nov 2026 22:20:34 -0800 Subject: [PATCH] fix(responses): keep leading system messages in client order once one goes to input on the bridge A message-level cache_control on the first of two leading string system messages sent that message to input as a breakpoint-carrying part, while the unmarked second one still joined instructions. The Responses API reads instructions before input, so the model saw the second message first and followed the first one instead The leading run that may join instructions now stops at the first message that is not a system message with string content. A list-content system message ends the run the same way, so the system messages after it keep their place in input Port of the matching change on #51783 to lts/1.108.x --- .../transformation.py | 18 ++++++--- ...responses_transformation_transformation.py | 39 +++++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/litellm/completion_extras/litellm_responses_transformation/transformation.py b/litellm/completion_extras/litellm_responses_transformation/transformation.py index 41f0592112c..714ea86bead 100644 --- a/litellm/completion_extras/litellm_responses_transformation/transformation.py +++ b/litellm/completion_extras/litellm_responses_transformation/transformation.py @@ -146,6 +146,17 @@ def _pending_audio_breakpoint(pending: object, part: object) -> object: return pending if pending is not None else _breakpoint_of(part) +def _leading_system_string_count(messages: Sequence[Mapping[str, object]]) -> int: + return next( + ( + index + for index, msg in enumerate(messages) + if msg.get("role") != "system" or not isinstance(msg.get("content", ""), str) + ), + len(messages), + ) + + def _carried_breakpoints(content: Sequence[object]) -> tuple[object, ...]: seeded_from_the_end: Final = chain((None,), reversed(content)) pending_after_each: Final = tuple(accumulate(seeded_from_the_end, _pending_audio_breakpoint)) @@ -942,10 +953,7 @@ 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"), - len(messages), - ) + leading_system_string_count: Final = _leading_system_string_count(messages) for index, msg in enumerate(messages): role = msg.get("role") @@ -955,7 +963,7 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge): if 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 00f80081713..93a68625287 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 @@ -5879,6 +5879,45 @@ def test_transform_request_moves_a_message_level_cache_control_onto_the_last_par assert messages == sent_messages +@pytest.mark.parametrize( + ("first_system", "first_input_part"), + [ + ( + {"role": "system", "content": "Reply ALPHA.", "cache_control": _CACHE_CONTROL}, + {"type": "input_text", "text": "Reply ALPHA.", "prompt_cache_breakpoint": _EXPLICIT_BREAKPOINT}, + ), + ( + {"role": "system", "content": [{"type": "text", "text": "Reply ALPHA."}]}, + {"type": "input_text", "text": "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_input_part: 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"] == [ + {"type": "message", "role": "system", "content": [first_input_part]}, + { + "type": "message", + "role": "system", + "content": [{"type": "input_text", "text": "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()