Skip to content

Text-based state machine - #1501

Merged
angeloskath merged 1 commit into
mainfrom
str-state-machine
Jul 8, 2026
Merged

Text-based state machine#1501
angeloskath merged 1 commit into
mainfrom
str-state-machine

Conversation

@angeloskath

Copy link
Copy Markdown
Member

The token based SequenceStateMachine has a design flaw that makes it impossible to identify the state change because substrings can be encoded in different ways.

This replaces it with TextStateMachine so we switch state on the actual string and not the tokens. It also introduces a StopSequenceMatcher that can be used to make sure that generation stops on specific token sequences.

This fixes #1373, #1447, #1406, #1336, #1160 .

@angeloskath
angeloskath requested a review from nastya236 July 8, 2026 20:58
Replace the token-based SequenceStateMachine with a StopSequenceMatcher
(EOS/stop words, token-level, used by the batch generator) and a
TextStateMachine that matches decoded text to track reasoning/tool state
and strip control sequences.

Matching on text rather than token ids is robust to tokenization
differences (e.g. a marker's trailing '>' merging with the next byte) and
keeps state on stop, so tool calls with an empty tool_call_end (Mistral/
Devstral) are no longer dropped. Also wires a reasoning->tool transition.
@nastya236

Copy link
Copy Markdown
Collaborator

Beautiful fix, thank you! Splitting it into two machines makes a lot of sense: one on decoded text for stripping thinking / tool-calling, one on token ids to stop generation. Nice buffering approach to ensure that we find a full match.

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.

2 participants