Repository navigation
fix(responses): propagate message cache_control through Responses->Chat transform (LIT-2594) #29166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
oss-agent-shin
wants to merge
5
commits into
BerriAI:litellm_oss_agent_shin_daily_branch
from
oss-agent-shin:fix/lit-2594-responses-cache-control-propagation
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d9c1e32
fix(responses): propagate message cache_control through Responses->Ch…
oss-agent-shin c9ad152
test(responses): regression tests for LIT-2594 cache_control propagation
oss-agent-shin fea9c94
style: black format LIT-2594 propagation
oss-agent-shin 0acdbd9
fix(types): add cache_control to GenericChatCompletionMessage TypedDi…
oss-agent-shin 1a2bd36
style: drop type:ignore now that GenericChatCompletionMessage has cac…
oss-agent-shin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
...lm/responses/litellm_completion_transformation/test_cache_control_propagation_lit_2594.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| """ | ||
| LIT-2594: cache_control_injection_points is broken with Responses API | ||
|
|
||
| These tests pin the propagation of message-level `cache_control` from the | ||
| Responses-API input items onto the resulting Chat-Completion messages, so that | ||
| `anthropic_messages_pt` / `AnthropicConfig.translate_system_message` can apply | ||
| it to system/user/assistant blocks. | ||
|
|
||
| Before the fix, AnthropicCacheControlHook wrote `cache_control` onto each input | ||
| message (chat-completion format), but the subsequent | ||
| `_transform_responses_api_input_item_to_chat_completion_message` call dropped | ||
| every field except `role`+`content`, so `cache_control` never reached the | ||
| provider transform and prompt caching silently no-op-ed. | ||
| """ | ||
|
|
||
| import os | ||
| import sys | ||
|
|
||
| sys.path.insert(0, os.path.abspath("../../..")) | ||
|
|
||
| from litellm.integrations.anthropic_cache_control_hook import ( | ||
| AnthropicCacheControlHook, | ||
| ) | ||
| from litellm.litellm_core_utils.prompt_templates.factory import ( | ||
| anthropic_messages_pt, | ||
| ) | ||
| from litellm.llms.anthropic.chat.transformation import AnthropicConfig | ||
| from litellm.responses.litellm_completion_transformation.transformation import ( | ||
| LiteLLMCompletionResponsesConfig, | ||
| ) | ||
|
|
||
|
|
||
| class TestCacheControlPropagationLit2594: | ||
| def _ephemeral(self): | ||
| return {"type": "ephemeral"} | ||
|
|
||
| def test_string_user_message_with_cache_control_is_preserved(self): | ||
| input_item = { | ||
| "role": "user", | ||
| "content": "What is the capital of France?", | ||
| "cache_control": self._ephemeral(), | ||
| } | ||
| result = LiteLLMCompletionResponsesConfig._transform_responses_api_input_item_to_chat_completion_message( | ||
| input_item=input_item, | ||
| ) | ||
| assert len(result) == 1 | ||
| msg = result[0] | ||
| assert msg["role"] == "user" | ||
| assert msg.get("cache_control") == self._ephemeral() | ||
|
|
||
| def test_string_system_message_with_cache_control_is_preserved(self): | ||
| input_item = { | ||
| "role": "system", | ||
| "content": "You are a helpful assistant.", | ||
| "cache_control": self._ephemeral(), | ||
| } | ||
| result = LiteLLMCompletionResponsesConfig._transform_responses_api_input_item_to_chat_completion_message( | ||
| input_item=input_item, | ||
| ) | ||
| assert len(result) == 1 | ||
| msg = result[0] | ||
| assert msg["role"] == "system" | ||
| assert msg.get("cache_control") == self._ephemeral() | ||
|
|
||
| def test_assistant_message_with_cache_control_is_preserved(self): | ||
| input_item = { | ||
| "role": "assistant", | ||
| "content": "France is a country in Europe.", | ||
| "cache_control": self._ephemeral(), | ||
| } | ||
| result = LiteLLMCompletionResponsesConfig._transform_responses_api_input_item_to_chat_completion_message( | ||
| input_item=input_item, | ||
| ) | ||
| assert len(result) == 1 | ||
| msg = result[0] | ||
| assert msg["role"] == "assistant" | ||
| assert msg.get("cache_control") == self._ephemeral() | ||
|
|
||
| def test_message_without_cache_control_has_no_key(self): | ||
| """No cache_control on the input item => the resulting message must not have a stray cache_control key.""" | ||
| input_item = {"role": "user", "content": "Hi"} | ||
| result = LiteLLMCompletionResponsesConfig._transform_responses_api_input_item_to_chat_completion_message( | ||
| input_item=input_item, | ||
| ) | ||
| assert len(result) == 1 | ||
| assert "cache_control" not in result[0] | ||
|
|
||
| def test_list_content_with_cache_control_is_preserved(self): | ||
| input_item = { | ||
| "role": "user", | ||
| "content": [{"type": "input_text", "text": "Hello"}], | ||
| "cache_control": self._ephemeral(), | ||
| } | ||
| result = LiteLLMCompletionResponsesConfig._transform_responses_api_input_item_to_chat_completion_message( | ||
| input_item=input_item, | ||
| ) | ||
| assert len(result) == 1 | ||
| assert result[0].get("cache_control") == self._ephemeral() | ||
|
|
||
| def test_none_content_short_circuits_to_empty(self): | ||
| input_item = { | ||
| "role": "user", | ||
| "content": None, | ||
| "cache_control": self._ephemeral(), | ||
| } | ||
| result = LiteLLMCompletionResponsesConfig._transform_responses_api_input_item_to_chat_completion_message( | ||
| input_item=input_item, | ||
| ) | ||
| assert result == [] | ||
|
|
||
| def _build_after_hook_messages(self): | ||
| responses_input = [ | ||
| {"role": "system", "content": "You are a helpful assistant."}, | ||
| {"role": "user", "content": "Tell me about France."}, | ||
| {"role": "assistant", "content": "France is a country in Europe."}, | ||
| {"role": "user", "content": "What is its capital?"}, | ||
| ] | ||
| cache_control_injection_points = [ | ||
| {"location": "message", "role": "system", "control": self._ephemeral()}, | ||
| {"location": "message", "index": -1, "control": self._ephemeral()}, | ||
| ] | ||
| hook = AnthropicCacheControlHook() | ||
| non_default_params = { | ||
| "cache_control_injection_points": list(cache_control_injection_points) | ||
| } | ||
| _, after_hook, _ = hook.get_chat_completion_prompt( | ||
| model="anthropic/claude-3-5-sonnet", | ||
| messages=responses_input, | ||
| non_default_params=non_default_params, | ||
| prompt_id=None, | ||
| prompt_variables=None, | ||
| dynamic_callback_params={}, # type: ignore[arg-type] | ||
| ) | ||
| return after_hook | ||
|
|
||
| def test_anthropic_system_block_carries_cache_control(self): | ||
| after_hook = self._build_after_hook_messages() | ||
| messages = LiteLLMCompletionResponsesConfig._transform_response_input_param_to_chat_completion_message( | ||
| input=after_hook, | ||
| ) | ||
| system_blocks = AnthropicConfig().translate_system_message(messages=messages) # type: ignore[arg-type] | ||
| assert len(system_blocks) == 1 | ||
| assert system_blocks[0].get("cache_control") == self._ephemeral() | ||
|
|
||
| def test_anthropic_last_user_message_carries_cache_control(self): | ||
| after_hook = self._build_after_hook_messages() | ||
| messages = LiteLLMCompletionResponsesConfig._transform_response_input_param_to_chat_completion_message( | ||
| input=after_hook, | ||
| ) | ||
| non_system = [m for m in messages if m.get("role") != "system"] | ||
| anthropic_msgs = anthropic_messages_pt( | ||
| messages=non_system, # type: ignore[arg-type] | ||
| model="claude-3-5-sonnet", | ||
| llm_provider="anthropic", | ||
| ) | ||
| last_user = anthropic_msgs[-1] | ||
| assert last_user["role"] == "user" | ||
| last_block = last_user["content"][-1] | ||
| assert last_block.get("cache_control") == self._ephemeral() | ||
|
|
||
| def test_anthropic_middle_user_message_has_no_cache_control(self): | ||
| after_hook = self._build_after_hook_messages() | ||
| messages = LiteLLMCompletionResponsesConfig._transform_response_input_param_to_chat_completion_message( | ||
| input=after_hook, | ||
| ) | ||
| non_system = [m for m in messages if m.get("role") != "system"] | ||
| anthropic_msgs = anthropic_messages_pt( | ||
| messages=non_system, # type: ignore[arg-type] | ||
| model="claude-3-5-sonnet", | ||
| llm_provider="anthropic", | ||
| ) | ||
| first_user = anthropic_msgs[0] | ||
| assert first_user["role"] == "user" | ||
| for block in first_user["content"]: | ||
| assert "cache_control" not in block | ||
|
|
||
| def test_baseline_without_injection_points_unchanged(self): | ||
| messages = LiteLLMCompletionResponsesConfig._transform_response_input_param_to_chat_completion_message( | ||
| input=[ | ||
| {"role": "system", "content": "sys"}, | ||
| {"role": "user", "content": "hi"}, | ||
| ], | ||
| ) | ||
| for m in messages: | ||
| assert "cache_control" not in m |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking
isinstance(input_item, dict)will silently dropcache_controlif messages are passed in as pre-parsed objects or Pydantic models (such asChatCompletionSystemMessageParamorResponseInputTextParam) rather than standard Python dictionaries.Let's add a fallback check to extract the property from object attributes using
hasattr/getattrto make this completely bulletproof: