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 <noreply@anthropic.com>
This commit is contained in:
Phimmasone Phonpaseuth 2026-09-19 16:53:52 -04:00
parent 516b33cce0
commit 2e37ea2533
2 changed files with 32 additions and 2 deletions

View file

@ -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 ""

View file

@ -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