Fix ChatSampler.conversation crash for non-Gemma4 tokenizers (breaks ToolSampler multi-turn)#734
Open
sohumt123 wants to merge 1 commit into
Open
Conversation
ChatSampler stores turn text formatted with the tokenizer's FORMAT (legacy <start_of_turn> tags for Gemma 2/3/3n tokenizers), but the conversation property passed the joined text directly to dialog.Conversation, which only parses the canonical <|turn> tags. As a result, accessing conversation after any successful chat() turn raised ValueError for all non-Gemma4 models, and ToolSampler.chat, which evaluates self.conversation on every call, crashed on every second turn (including the internal recursive call after a tool executes), breaking multi-turn and tool-call flows for those models. Convert the stored text to the canonical format via FORMAT.to_gemma4 before parsing (a no-op for Gemma4 tokenizers), and add regression tests for the conversation property and ToolSampler multi-turn chat with a legacy-format tokenizer.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ChatSampler.conversationraisesValueErrorfor all non-Gemma4 models after any successfulchat()turn, and this crashesToolSamplermulti-turn and tool-call flows for those model families.ChatSamplerstores each turn's text pre-formatted withself.tokenizer.FORMAT— which isdialog.Format.GEMMA3(<start_of_turn>...<end_of_turn>tags) for the Gemma 2/3/3n tokenizers. But theconversationproperty passes the joined text directly todialog.Conversation(str), which only parses the canonical Gemma4<|turn>...<turn|>tags:The impact goes beyond the public
conversationproperty:ToolSampler.chatevaluatesself.conversationon every call (if not self.conversation or not multi_turn,_tool_sampler.py:107), including the internal recursive call made after a tool call executes. So for Gemma 2/3/3n models, turn 1 succeeds but every second turn raises, fully breaking tool use and multi-turn tool chat.The mismatch was introduced when the turns storage switched to holding text pre-formatted with the tokenizer's legacy format while
conversationmigrated to thedialoglibrary's parser, with no format conversion between the two.Fix
In the
conversationproperty, convert the stored text to the canonical format before parsing:This is a no-op for Gemma4 tokenizers (
FORMATisGEMMA4) and covers all currentdialog.Formatmembers.Testing
Added two regression tests in
gemma/gm/text/_sampler_test.py, following the existing mock-basedChatSamplertest pattern (gm.testing.DummyGemma+DummyTokenizer, CPU-only, no downloads):test_chat_sampler_conversation_legacy_format— after achat()turn with aGEMMA3-format tokenizer,conversationreturns adialog.Conversationwith the expectedUser/Modelturns. Fails with theValueErrorabove without the fix.test_tool_sampler_multi_turn_legacy_format—ToolSampler.chatcompletes a second turn with a legacy-format tokenizer. Fails on turn 2 without the fix.