fix(responses): preserve prompt cache breakpoints in chat bridge

This commit is contained in:
Simon Sorg 2026-09-21 21:06:52 +02:00
parent 22b36cbcf6
commit f57f994629
No known key found for this signature in database
2 changed files with 44 additions and 1 deletions

View file

@ -26,6 +26,7 @@ from litellm import ModelResponse
from litellm._logging import verbose_logger
from litellm.litellm_core_utils.prompt_templates.common_utils import (
responses_reasoning_items_from_thinking_blocks,
with_prompt_cache_breakpoint,
)
from litellm.llms.base_llm.base_model_iterator import BaseModelResponseIterator
from litellm.llms.base_llm.bridges.completion_transformation import (
@ -1060,7 +1061,10 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):
# Handle multimodal content
original_type = item.get("type")
if original_type == "text":
converted = self._convert_content_str_to_input_text(item.get("text", ""), role)
converted = with_prompt_cache_breakpoint(
self._convert_content_str_to_input_text(item.get("text", ""), role),
item.get("prompt_cache_breakpoint"),
)
result.append(converted)
verbose_logger.debug("Chat provider: text -> %s", converted)
elif original_type == "image_url":

View file

@ -4185,6 +4185,45 @@ def _system_input_item(text: str) -> dict[str, object]:
return {"type": "message", "role": "system", "content": [{"type": "input_text", "text": text}]}
def test_prompt_cache_breakpoint_survives_chat_to_responses_conversion() -> None:
handler: Final = LiteLLMResponsesTransformationHandler()
cache_breakpoint: Final = {"mode": "explicit"}
request: Final = handler.transform_request(
model="gpt-5.6-sol",
messages=[
{
"role": "system",
"content": [
{
"type": "text",
"text": "Stable prefix",
"prompt_cache_breakpoint": cache_breakpoint,
}
],
},
{"role": "user", "content": "Use a tool"},
],
optional_params={"prompt_cache_options": cache_breakpoint},
litellm_params={},
headers={},
litellm_logging_obj=Mock(),
)
assert request["input"][0] == {
"type": "message",
"role": "system",
"content": [
{
"type": "input_text",
"text": "Stable prefix",
"prompt_cache_breakpoint": cache_breakpoint,
}
],
}
assert request["prompt_cache_options"] == cache_breakpoint
def test_mid_conversation_system_string_stays_in_input_after_a_user_turn():
handler: Final = LiteLLMResponsesTransformationHandler()