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
This commit is contained in:
mateo-berri 2026-11-13 22:20:34 -08:00
parent d3e02ad9b3
commit 27c828d3bc
2 changed files with 52 additions and 5 deletions

View file

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

View file

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