From 2e37ea2533bc2f7fe80c824deae3fbb172e95337 Mon Sep 17 00:00:00 2001 From: Phimmasone Phonpaseuth Date: Sat, 19 Sep 2026 16:53:52 -0400 Subject: [PATCH] fix(straiker): seed the derived session on every text block of the first turn A user turn that opens with an image or a document block and carries its text later seeded the session on an empty string, so two different conversations under the same preamble shared one Straiker session. Read every text block of the turn instead of only the first block. A plain string or a single text block seeds exactly as before. Co-Authored-By: Claude Fable 5.1 --- .../guardrail_hooks/straiker/straiker.py | 8 ++++-- .../guardrail_hooks/test_straiker.py | 26 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/guardrails/guardrail_hooks/straiker/straiker.py b/litellm/proxy/guardrails/guardrail_hooks/straiker/straiker.py index 8ade91f636f..fbef7181d62 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/straiker/straiker.py +++ b/litellm/proxy/guardrails/guardrail_hooks/straiker/straiker.py @@ -673,11 +673,15 @@ _V3_PREAMBLE_ROLES: Final = frozenset({"system", "developer"}) def _v3_message_text(message: object) -> str: + """Every text block of a message, so a turn that opens with an image or a document still + seeds on what the user wrote.""" content: Final = message.get("content") if isinstance(message, Mapping) else None if isinstance(content, str): return content - if isinstance(content, (list, tuple)) and content and isinstance(content[0], Mapping): - return str(content[0].get("text") or "") + if isinstance(content, (list, tuple)): + return "\n".join( + str(block["text"]) for block in content if isinstance(block, Mapping) and isinstance(block.get("text"), str) + ) return "" diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_straiker.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_straiker.py index caaca625104..3e4d5e6b966 100644 --- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_straiker.py +++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_straiker.py @@ -2185,3 +2185,29 @@ async def test_v3_openai_format_conversations_that_share_a_system_prompt_get_the assert refund != other_preamble assert developer == refund and developer != other_preamble assert responses_input.startswith("litellm-") + + +@pytest.mark.asyncio +async def test_v3_derived_session_reads_the_text_of_a_turn_that_opens_with_an_image(): + async def session_for(first_user_content): + g = _make_guardrail(api_key=V3_KEY) + g.async_handler.post.return_value = _v3_mock(V3_GATEWAY_ALLOW) + data = _v3_request_data( + system="You are the claims assistant.", + messages=[{"role": "user", "content": first_user_content}], + metadata={"user_api_key_end_user_id": "alice.chen@example.com"}, + ) + data["proxy_server_request"] = {"headers": {}} + await g.apply_guardrail( + inputs={"texts": ["x"]}, request_data=data, input_type="request", logging_obj=_logging_obj() + ) + return _posted_payload(g)["session_id"] + + image = {"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "AAAA"}} + dent = await session_for([image, {"type": "text", "text": "Assess the dent on the rear door"}]) + dent_again = await session_for([image, {"type": "text", "text": "Assess the dent on the rear door"}]) + windshield = await session_for([image, {"type": "text", "text": "Assess the cracked windshield"}]) + text_first = await session_for([{"type": "text", "text": "Assess the dent on the rear door"}, image]) + assert dent == dent_again + assert dent != windshield + assert text_first == dent