Skip to content

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

Merged
danielhanchen merged 2 commits into
mainfrom
fix/continuation-merge-drops-provider-extra-content
Aug 26, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
fix/continuation-merge-drops-provider-extra-content

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

The bug

append_assistant_turn (studio/backend/core/inference/chat_template_helpers.py) merges a continuation into the trailing partial rather than appending, so that role alternation survives. It did that by assigning the new message into the slot:

assistant_msg["content"] = f"{prev_text}{assistant_msg['content']}"
conversation[-1] = assistant_msg

That replaces the resumed turn, so every key the partial carried and the continuation does not repeat is dropped.

extra_content is exactly such a key. It is the namespaced envelope a translator reads back on the way out, and there are three namespaces in the tree today: google (15 sites, Gemini thought signatures and native parts), anthropic (11 sites) and openai_codex_reasoning. The field's own schema in models/inference.py describes it as "provider-specific extra fields the translator may read".

For Gemini the consequence is hard rather than cosmetic. studio_tool_loop.py says it plainly:

Gemini 3 stows the text part's thoughtSignature here and its translator pins it back on from this field alone, so a turn replayed without it is rejected.

Observed on main:

merged turn     : {'role': 'assistant', 'content': 'Looking that up now.'}
caller dict now : {'role': 'assistant', 'content': 'Looking that up now.'}

The signature is gone, and the second line is a separate defect: the same statement mutates the dict the caller passed in, so a caller that reads it back afterwards sees text it never wrote.

The fix

Merge over the existing message instead of replacing it.

merged_msg = {**conversation[-1], **assistant_msg}
merged_msg["content"] = f"{prev_text}{assistant_msg['content']}"
conversation[-1] = merged_msg

After:

merged turn     : {'role': 'assistant', 'content': 'Looking that up now.',
                   'extra_content': {'google': {'thought_signature': 'SIG-PARTIAL'}}}
caller dict now : {'role': 'assistant', 'content': 'up now.'}

Namespace agnostic on purpose. Special casing extra_content["google"] would have left anthropic and openai_codex_reasoning broken and set the same trap for the next provider that adds one.

Scope

This is not specific to any one loop. Six of the eight append_assistant_turn call sites are tool-result replay across the GGUF, safetensors and external loops, and the merge path itself is reached by the continue-a-partial-response feature from #8064, which is independent of tool calling entirely.

Tests

Three added to tests/test_chat_template_continuation.py:

  • test_a_resumed_partial_keeps_the_metadata_the_continuation_does_not_repeat - fails on main
  • test_a_merge_leaves_the_callers_message_alone - fails on main
  • test_a_continuation_still_wins_on_a_key_it_does_repeat - passes on both, guarding the fix against over-correcting into resurrecting a stale value

Verified both directions:

against main:      2 failed, 1 passed
against this PR:   42 passed  (whole file)
blast radius:     731 passed  (continuation, GGUF, safetensors, external, hosted replay,
                               passthrough healing, plan classifier)
ruff check and scripts/run_ruff_format.py --check: clean

Found while looking at #9125, but it is independent of that PR and of tool-call nudging, so it is split out to land on its own.

append_assistant_turn merges a continuation into the trailing partial rather
than appending, so role alternation survives. It did that by assigning the new
message into the slot, which drops every key the partial carried and the
continuation does not repeat.

extra_content is exactly such a key. It is the namespaced envelope a translator
reads back: google for Gemini's thought signatures and native parts, plus
anthropic and openai_codex_reasoning. Gemini pins the text part's signature back
on from that field alone, so a resumed turn replayed without it is rejected
rather than merely degraded.

Merge over the existing message instead. A key the continuation does set still
wins, so this cannot resurrect a stale value.

The same line also mutated the caller's dict in place, so a caller that read the
message back after the call saw text it never wrote.

Six of the eight call sites are tool-result replay across the GGUF, safetensors
and external loops, and the merge path itself is reached by the continuation
feature from #8064, so this is not specific to any one of them.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: e8d89080fb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

Confirmed this on main, where chat_template_helpers.py still overwrites the last conversation entry so a resumed turn loses extra_content and the caller's dict gets mutated. Will get this reviewed.

@danielhanchen
danielhanchen merged commit adca7b9 into main Aug 26, 2026
10 of 41 checks passed
@danielhanchen
danielhanchen deleted the fix/continuation-merge-drops-provider-extra-content branch August 26, 2026 05:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant