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