Commit 0af57a8
committed
Fix guided generation: stop on grammar termination, include <|endoftext|>
Two fixes for the same bug (defense in depth):
1. When the grammar session terminates (isTerminated), return nil
immediately instead of emitting the terminal token's decoded text.
This prevents ANY special token from leaking into structured output,
regardless of whether it's in the stop sequences list.
2. Add 'endoftext' to turnEndPatterns so <|endoftext|> (token 151643)
is picked up from added_tokens_decoder as an additional stop token.
Qwen3 declares eos_token as <|im_end|> (151645) but xgrammar can
also produce <|endoftext|> as a valid grammar terminal.1 parent 2607aea commit 0af57a8
2 files changed
Lines changed: 6 additions & 5 deletions
File tree
- swift/Sources/CoreAILanguageModels
- Bundle
- DecodingStrategies
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
129 | | - | |
| 129 | + | |
130 | 130 | | |
131 | 131 | | |
132 | 132 | | |
| |||
Lines changed: 5 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
324 | 324 | | |
325 | 325 | | |
326 | 326 | | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
327 | 332 | | |
328 | 333 | | |
329 | 334 | | |
330 | 335 | | |
331 | 336 | | |
332 | 337 | | |
333 | 338 | | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | 339 | | |
339 | 340 | | |
340 | 341 | | |
| |||
0 commit comments