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