Skip to content

Strip <think> blocks from multi-turn history in chat_multiturn.py - #284

Open
jaydeepthik wants to merge 2 commits into
rasbt:mainfrom
jaydeepthik:fix/chat-multiturn-strip-thinking
Open

jaydeepthik wants to merge 2 commits into
rasbt:mainfrom
jaydeepthik:fix/chat-multiturn-strip-thinking

Conversation

@jaydeepthik

@jaydeepthik jaydeepthik commented Nov 27, 2026 •

Copy link
Copy Markdown

Summary

When running `ch02/05_use_model/chat_multiturn.py --reasoning`, keeping `<think>...</think>` blocks from previous assistant turns in `history` puts the Qwen3 reasoning model out of distribution on subsequent turns (Qwen3's multi-turn chat template expects prior assistant turns to contain only the final response after `</think>`). As a result, after one or two turns the model can emit `<|im_end|>` / `<|endoftext|>` prematurely mid-`<think>` before reaching `max_new_tokens`.

Changes

- Added `strip_thinking()` in `ch02/05_use_model/chat_multiturn.py` to remove `<think>...</think>` blocks (as well as unclosed `<think>...` blocks if a turn hits `max_new_tokens` mid-thought).
- Updated `run_generate()` to store `strip_thinking(assistant_text)` in `history` while still returning the full `assistant_text`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant