Skip to content

Deliver streamed tokens live instead of after generation completes - #195

Merged
mattt merged 1 commit into
huggingface:mainfrom
james-333i:fix/llama-token-streaming
Sep 4, 2026
Merged

Deliver streamed tokens live instead of after generation completes#195
mattt merged 1 commit into
huggingface:mainfrom
james-333i:fix/llama-token-streaming

Conversation

@james-333i

@james-333i james-333i commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

streamResponse ran the whole generation loop before the consuming task received anything, so tokens arrived in one burst at the end. This yields each token as it is sampled.

@mattt

mattt commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

@james-333i This one picked up a conflict after #196 went in as a squash. Would you rebase it onto main? I'll merge as soon as it's green.

streamResponse consumed an inner AsyncThrowingStream whose builder ran
the entire generation loop synchronously on the consuming task, so
every snapshot buffered and arrived in one burst after generation
finished.

Yield snapshots directly from the generation loop on the streaming
task, and check for task cancellation between tokens so an abandoned
stream stops decoding promptly.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A decoding failure from llama_decode is still silently swallowed (ending the stream as if successful), and should be surfaced as an error now that the generator is throws.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR updates streamResponse in LlamaLanguageModel so streamed output snapshots are yielded incrementally during generation (instead of being produced only after the full generation loop completes), improving real-time token delivery to consumers.

Changes:

  • Replace the generateTextStream-based loop with a direct performTextGeneration(..., onToken:) callback to yield snapshots as tokens are sampled.
  • Refactor performTextGeneration to throw errors directly and emit tokens via an onToken closure.
  • Add a cooperative cancellation check inside the generation loop.
File summaries
File Description
Sources/AnyLanguageModel/Models/LlamaLanguageModel.swift Refactors streaming generation to yield per-token snapshots live during sampling rather than at completion.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +1283 to +1286
let decodeResult = llama_decode(context, batch)
guard decodeResult == 0 else {
break
}
@mattt

mattt commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

Merging now. Thanks, @james-333i!

@mattt
mattt merged commit 163afb3 into huggingface:main Sep 4, 2026
11 checks passed
james-333i added a commit to james-333i/AnyLanguageModel that referenced this pull request Sep 4, 2026
Brings in the merged huggingface#195, huggingface#205, huggingface#212 and huggingface#217 along with the follow-ups
applied on merge (all-text prompt drop, boolean and number probe items).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
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.

3 participants