Skip to content

Stream text deltas by Unicode scalars, not Characters - #568

Open
hkarmoush wants to merge 1 commit into
apple:mainfrom
hkarmoush:hkarmoush-fix-stream-grapheme-delta
Open

hkarmoush wants to merge 1 commit into
apple:mainfrom
hkarmoush:hkarmoush-fix-stream-grapheme-delta

Conversation

@hkarmoush

@hkarmoush hkarmoush commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Type of Change

  • Bug fix
  • Small feature (agreed in an issue)
  • New model (export recipe only)
  • New model (export recipe and new runtime support)
  • Documentation update

Motivation and Context

Refs #315.

When a token extends the previous grapheme cluster, comparing by Character re-emits text that was already streamed. The vanilla, constrained and pipelined constrained strategies now compare and slice Unicode scalars.

This starts from the sukru/fix-issue-315 branch (originally #342 by @iannellomarco), moved onto current main. The one conflict was in VanillaDecodingStrategy, where the delta code now sits inside the if known block.

#564 also edits these files. Git merges the two cleanly, but it renames the stopSequences: argument to decoderOptions:, so whichever lands second needs the new test file updated. I can rebase if #564 goes in first.

I left out the streaming loops in CoreAILanguageModel and CoreAIVisionLanguageModel. They also use commonPrefix(with:), but they decode a short sliding window, and in a simulation they also lost text after a split character, so a scalar diff alone may not be enough. llm-runner has its own dropFirst(previousText.count) diff too. I can look at those separately.

Testing

  • Tested locally
  • Added/updated tests
  • Added/updated docs

I couldn't build or run the package: it needs Xcode 27 and I have 26.6, so the new tests haven't run yet.

What I did run:

  • swift format lint --strict --recursive swift/Sources/ swift/Tests/ passes.
  • A standalone script with copies of the old and new delta logic, using the three test strings at one UTF-8 byte per token. For the vanilla and constrained logic, the old version failed all six cases and the new one passed all six. The pipelined diff is the same as the constrained one, and the script doesn't exercise it separately.

@hkarmoush
hkarmoush requested a review from a team as a code owner November 29, 2026 15:34
A token that extends the previous grapheme cluster (a combining mark, a ZWJ
emoji sequence, an Indic vowel sign) was compared Character by Character, so
streamed text was emitted again. Compare and slice Unicode scalars in the
vanilla, constrained and pipelined constrained strategies.

Co-authored-by: Marco Iannello <148067382+iannellomarco@users.noreply.github.com>
@hkarmoush
hkarmoush force-pushed the hkarmoush-fix-stream-grapheme-delta branch from bbc8c67 to 477975c Compare December 1, 2026 08:22
@tjia1818
tjia1818 requested a review from stikves December 1, 2026 18:16
@stikves

stikves commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Thank you for the PR

However "Fixes #315." is an over claim, we will still have follow ups.

Let's merge this for the cases that you have, though. These are real fixes.

@hkarmoush

Copy link
Copy Markdown
Contributor Author

The lint and python-test-Mac failures look like a runner problem rather than the change: every step in both jobs passed and only the "Complete runner" step failed. Could someone re-run those two jobs? I don't have permission to.

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