Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix: reuse block builder for lint budget, assert unflagged cache e2e

Shifat Islam Santo1b5e50727c62
diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py b/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py
index 1e016cffb0e..84fb7a1f45e 100644
--- a/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py
+++ b/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py
@@ -167,10 +167,8 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig):
return {
**message,
"role": "user",
- "content": [
- {"type": "text", "text": self._CONVERTED_SYSTEM_NOTE},
- *self._as_system_content_blocks(message.get("content")),
- ],
+ "content": self._as_system_content_blocks(self._CONVERTED_SYSTEM_NOTE)
+ + self._as_system_content_blocks(message.get("content")),
}
def _normalize_system_role_messages(self, anthropic_messages_request: dict, model: str) -> None:
diff --git a/tests/e2e/llm_translation/test_messages_mid_conversation_system_e2e.py b/tests/e2e/llm_translation/test_messages_mid_conversation_system_e2e.py
index e1d836108e0..bd59df959c4 100644
--- a/tests/e2e/llm_translation/test_messages_mid_conversation_system_e2e.py
+++ b/tests/e2e/llm_translation/test_messages_mid_conversation_system_e2e.py
@@ -208,24 +208,36 @@ class TestBedrockInvokeMidConversationSystem:
endpoints_client, resources, UNFLAGGED_INVOKE_MODEL
)
key = resources.key(models=[model])
+ system_block = _cacheable_system_block(unique_marker())
- body = RichMessagesRequest(
+ primed = _prime_prompt_cache(endpoints_client, key, model, system_block)
+
+ reminder_turn_body = RichMessagesRequest(
model=model,
- system=[TextBlock(text="You are terse.")],
+ system=[system_block],
messages=[
- _user_turn(f"Say hi. Run {unique_marker()}."),
+ _user_turn(primed.first_user_text, cached=True),
_system_reminder_turn(),
- RichMessage(role="assistant", content=[TextBlock(text="Hi.")]),
- _user_turn("Say bye."),
+ RichMessage(role="assistant", content=[TextBlock(text="OK.")]),
+ _user_turn("Reply with one word again.", cached=True),
],
)
- completion = unwrap(_post_messages(endpoints_client, key, body))
+ second = unwrap(_post_messages(endpoints_client, key, reminder_turn_body))
- assert completion.role == "assistant", (
- f"{model}: unexpected role {completion.role!r}"
+ assert second.role == "assistant", (
+ f"{model}: unexpected role {second.role!r}"
)
- assert completion.text.strip(), (
+ assert second.text.strip(), (
f"{model}: conversation with a mid-conversation system reminder "
f"returned no text; the reminder was forwarded in place to a model "
f"that rejects role 'system' inside messages instead of being converted to a user turn"
)
+ assert second.usage.cache_read_input_tokens >= primed.full_prefix_tokens, (
+ f"{model}: reminder turn read {second.usage.cache_read_input_tokens} "
+ f"cached tokens, expected at least the {primed.full_prefix_tokens} "
+ f"cached on turn one ({primed.prefix_read_tokens} system prefix + "
+ f"{primed.first_turn_creation_tokens} first user turn); the reminder "
+ f"was hoisted into the top-level system field instead of being "
+ f"converted to a user turn in place, mutating the cached prefix and "
+ f"re-billing the conversation at cache-write pricing"
+ )
diff --git a/tests/e2e/llm_translation/test_messages_mid_conversation_system_native_providers_e2e.py b/tests/e2e/llm_translation/test_messages_mid_conversation_system_native_providers_e2e.py
index 6eae0d66c59..bf4e662e02e 100644
--- a/tests/e2e/llm_translation/test_messages_mid_conversation_system_native_providers_e2e.py
+++ b/tests/e2e/llm_translation/test_messages_mid_conversation_system_native_providers_e2e.py
@@ -211,25 +211,37 @@ def _assert_unflagged_model_converts_and_succeeds(
) -> None:
model = _register_deployment(client, resources, params)
key = resources.key(models=[model])
+ system_block = _cacheable_system_block(unique_marker())
+
+ primed = _prime_prompt_cache(client, key, model, system_block)
- body = RichMessagesRequest(
+ reminder_turn_body = RichMessagesRequest(
model=model,
- system=[TextBlock(text="You are terse.")],
+ system=[system_block],
messages=[
- _user_turn(f"Say hi. Run {unique_marker()}."),
+ _user_turn(primed.first_user_text, cached=True),
_system_reminder_turn(),
- RichMessage(role="assistant", content=[TextBlock(text="Hi.")]),
- _user_turn("Say bye."),
+ RichMessage(role="assistant", content=[TextBlock(text="OK.")]),
+ _user_turn("Reply with one word again.", cached=True),
],
)
- completion = unwrap(_post_messages(client, key, body))
+ second = unwrap(_post_messages(client, key, reminder_turn_body))
- assert completion.role == "assistant", f"{model}: unexpected role {completion.role!r}"
- assert completion.text.strip(), (
+ assert second.role == "assistant", f"{model}: unexpected role {second.role!r}"
+ assert second.text.strip(), (
f"{model}: conversation with a mid-conversation system reminder returned "
f"no text; the reminder was forwarded in place to a model that rejects "
f"role 'system' inside messages instead of being converted to a user turn"
)
+ assert second.usage.cache_read_input_tokens >= primed.full_prefix_tokens, (
+ f"{model}: reminder turn read {second.usage.cache_read_input_tokens} cached "
+ f"tokens, expected at least the {primed.full_prefix_tokens} cached on turn "
+ f"one ({primed.prefix_read_tokens} system prefix + "
+ f"{primed.first_turn_creation_tokens} first user turn); the reminder was "
+ f"hoisted into the top-level system field instead of being converted to a "
+ f"user turn in place, mutating the cached prefix and re-billing the "
+ f"conversation at cache-write pricing"
+ )
class TestAzureFoundryMidConversationSystem: