Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

studio: keep a resumed turn's provider metadata through the continuation merge (#9684)

Daniel Hanadca7b9aab62
diff --git a/studio/backend/core/inference/chat_template_helpers.py b/studio/backend/core/inference/chat_template_helpers.py
index a313fb64afd..ead7216a8ea 100644
--- a/studio/backend/core/inference/chat_template_helpers.py
+++ b/studio/backend/core/inference/chat_template_helpers.py
@@ -2416,12 +2416,19 @@ def append_assistant_turn(
model just added are one turn, so they are merged: appending would instead give two
consecutive assistant messages and break role alternation. Self-limiting, since
after a tool result the conversation no longer ends with a plain assistant turn.
+
+ Merge over the resumed turn rather than replacing it, or every key the partial
+ carried but the continuation does not repeat is lost. ``extra_content`` is such a
+ key, and Gemini reads the text part's thought signature back from it alone, so a
+ resumed turn replayed without it is rejected.
"""
# Same acceptance rule as the prompt boundary, so a partial sent as text parts merges too.
prev_text = trailing_assistant_text(conversation) if continue_final_message else None
if prev_text is not None and isinstance(assistant_msg.get("content"), str):
- assistant_msg["content"] = f"{prev_text}{assistant_msg['content']}"
- conversation[-1] = assistant_msg
+ # Copy rather than mutate: the caller owns assistant_msg and may still read it.
+ merged_msg = {**conversation[-1], **assistant_msg}
+ merged_msg["content"] = f"{prev_text}{assistant_msg['content']}"
+ conversation[-1] = merged_msg
return
conversation.append(assistant_msg)
diff --git a/studio/backend/tests/test_chat_template_continuation.py b/studio/backend/tests/test_chat_template_continuation.py
index 76bc299f0b7..5317a860228 100644
--- a/studio/backend/tests/test_chat_template_continuation.py
+++ b/studio/backend/tests/test_chat_template_continuation.py
@@ -587,6 +587,67 @@ def test_a_text_part_partial_merges_rather_than_doubling_the_turn():
assert conversation[-1]["tool_calls"] == [{"id": "c1"}]
+def test_a_resumed_partial_keeps_the_metadata_the_continuation_does_not_repeat():
+ """Gemini pins the text part's signature back on from extra_content alone.
+
+ The merge used to assign the continuation into the slot, so every key the
+ partial carried and the continuation did not was dropped and the replayed
+ turn was rejected.
+ """
+ conversation = [
+ {"role": "user", "content": "q"},
+ {
+ "role": "assistant",
+ "content": "Looking that ",
+ "extra_content": {"google": {"thought_signature": "SIG-PARTIAL"}},
+ },
+ ]
+ append_assistant_turn(
+ conversation,
+ {"role": "assistant", "content": "up now."},
+ continue_final_message = True,
+ )
+
+ assert conversation[-1]["content"] == "Looking that up now."
+ assert conversation[-1]["extra_content"] == {"google": {"thought_signature": "SIG-PARTIAL"}}
+
+
+def test_a_merge_leaves_the_callers_message_alone():
+ """The caller owns the dict it passed and may still read it afterwards."""
+ conversation = [
+ {"role": "user", "content": "q"},
+ {"role": "assistant", "content": "Looking that "},
+ ]
+ generated = {"role": "assistant", "content": "up now."}
+ append_assistant_turn(conversation, generated, continue_final_message = True)
+
+ assert conversation[-1]["content"] == "Looking that up now."
+ assert generated["content"] == "up now."
+
+
+def test_a_continuation_still_wins_on_a_key_it_does_repeat():
+ """Merging must not resurrect a stale value the new turn deliberately set."""
+ conversation = [
+ {"role": "user", "content": "q"},
+ {
+ "role": "assistant",
+ "content": "Looking that ",
+ "extra_content": {"google": {"thought_signature": "STALE"}},
+ },
+ ]
+ append_assistant_turn(
+ conversation,
+ {
+ "role": "assistant",
+ "content": "up now.",
+ "extra_content": {"google": {"thought_signature": "FRESH"}},
+ },
+ continue_final_message = True,
+ )
+
+ assert conversation[-1]["extra_content"] == {"google": {"thought_signature": "FRESH"}}
+
+
def test_a_merge_stops_once_the_turn_is_no_longer_the_resumed_one():
"""Self-limiting: after a tool result or a nudge the partial is not trailing."""
after_tool = [