litellm/tests/unit/responses/test_instructions_length.py
ryan-crabbe-berri 511afdc8d6
fix(responses): send bridged system prompts too long for instructions as input (#52964)
* test(responses): cover bridged system prompts too long for instructions

Black-box wire tests for chat completions and /v1/messages on a Responses
bridged OpenAI model. A system prompt over the instructions length limit
must reach the upstream in input on truncation auto and on a follow-up
whose stored chain lacks it, while short prompts and prompts the chain
already holds keep today's shape

* fix(responses): send bridged system prompts too long for instructions as input

OpenAI and Azure reject an instructions string over 1,048,576 code points.
The chat completions and /v1/messages bridges still sent long system
prompts there on truncation auto and on follow-ups whose stored chain did
not hold the prompt, so those requests failed with a 400. After the stored
chain check, instructions over the limit now go out as a leading system
input item, and a truncation auto follow-up skips a prompt its stored chain
already holds instead of sending it again

* fix(responses): keep a long system prompt when the caller overrides input in extra_body

A caller's extra_body input replaced the input that carried the moved
system prompt, so the prompt was dropped. The moved prompt now goes into
whichever input the request sends

* ci(unit): run the new instructions length tests in the responses shard
2026-11-20 11:56:47 -08:00

45 lines
1.9 KiB
Python

from typing import Final
import pytest
from litellm.responses.instructions_length import RESPONSES_INSTRUCTIONS_MAX_LENGTH, with_long_instructions_in_input
_OVER_LIMIT: Final = "a" * (RESPONSES_INSTRUCTIONS_MAX_LENGTH + 1)
_USER_HI: Final = {"type": "message", "role": "user", "content": [{"type": "input_text", "text": "hi"}]}
_SYSTEM_ITEM: Final = {"type": "message", "role": "system", "content": [{"type": "input_text", "text": _OVER_LIMIT}]}
@pytest.mark.parametrize(
("request_body", "expected"),
[
(
{"instructions": _OVER_LIMIT, "input": [_USER_HI], "truncation": "auto"},
{"input": [_SYSTEM_ITEM, _USER_HI], "truncation": "auto"},
),
(
{"input": [_USER_HI], "extra_body": {"instructions": _OVER_LIMIT, "truncation": "auto"}},
{"input": [_SYSTEM_ITEM, _USER_HI], "extra_body": {"truncation": "auto"}},
),
(
{"instructions": _OVER_LIMIT, "input": [_USER_HI], "extra_body": {"input": [_USER_HI]}},
{"input": [_SYSTEM_ITEM, _USER_HI], "extra_body": {"input": [_SYSTEM_ITEM, _USER_HI]}},
),
],
ids=["top_level_instructions", "extra_body_instructions", "caller_input_override_in_extra_body"],
)
def test_instructions_too_long_for_the_responses_api_go_first_in_the_input_the_request_sends(
request_body: dict[str, object], expected: dict[str, object]
) -> None:
assert with_long_instructions_in_input(request_body) == expected
@pytest.mark.parametrize(
"request_body",
[
{"instructions": "a" * RESPONSES_INSTRUCTIONS_MAX_LENGTH, "input": [_USER_HI]},
{"instructions": _OVER_LIMIT, "input": "hi"},
],
ids=["at_limit", "string_input_is_left_for_the_provider_to_reject"],
)
def test_requests_the_limit_does_not_apply_to_are_sent_unchanged(request_body: dict[str, object]) -> None:
assert with_long_instructions_in_input(request_body) is request_body