[PR #12733] [CLOSED] Flush EOS piece before removing sequence #13931

Closed
opened 2026-04-13 00:40:35 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/12733
Author: @A-Akhil
Created: 10/22/2025
Status: Closed

Base: mainHead: feature/eos-response


📝 Commits (1)

  • b0bbb25 Flush EOS piece before removing sequence

📊 Changes

1 file changed (+11 additions, -8 deletions)

View changed files

📝 runner/ollamarunner/runner.go (+11 -8)

📄 Description

This PR addresses a TODO left by @jmorganca in runner/ollamarunner/runner.go#L742-L744:

// TODO (jmorganca): we should send this back
// as it's important for the /api/generate context
// seq.responses <- piece

The end-of-sequence (EOS) token was being detected and causing sequence removal, but the decoded piece representing that token was never sent to the client. This meant /api/generate consumers would miss the final token in the context, which is important for accurate context tracking and state management.

Changes

This PR reorders the EOS handling logic to:

  1. Decode the token before the EOS check — previously, the code checked model.SpecialEOS before calling Decode, so the final piece was unavailable when the sequence terminated.
  2. Append the EOS piece to pendingResponses — ensures the decoded content is queued for delivery.
  3. Flush pending responses before sequence removal — calls flushPending(seq) to send the EOS piece through seq.responses before invoking removeSequence.
  4. Preserve connection-closed handling — if the client disconnects mid-flush, the sequence is still torn down cleanly with DoneReasonConnectionClosed instead of DoneReasonStop.

The existing UTF-8 validation in flushPending ensures no partial code points are emitted, and because removeSequence also calls flushPending on an already-empty buffer, there is no risk of duplicate sends.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ollama/ollama/pull/12733 **Author:** [@A-Akhil](https://github.com/A-Akhil) **Created:** 10/22/2025 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feature/eos-response` --- ### 📝 Commits (1) - [`b0bbb25`](https://github.com/ollama/ollama/commit/b0bbb25bc3d62586783221c9a97c8b42fa1b14a2) Flush EOS piece before removing sequence ### 📊 Changes **1 file changed** (+11 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `runner/ollamarunner/runner.go` (+11 -8) </details> ### 📄 Description This PR addresses a TODO left by @jmorganca in [`runner/ollamarunner/runner.go#L742-L744`](https://github.com/ollama/ollama/blob/d515aed6c3c23cc7830462c2b4055076e207fc9d/runner/ollamarunner/runner.go#L742C1-L744C29): ```go // TODO (jmorganca): we should send this back // as it's important for the /api/generate context // seq.responses <- piece ``` The end-of-sequence (EOS) token was being detected and causing sequence removal, but the decoded piece representing that token was never sent to the client. This meant `/api/generate` consumers would miss the final token in the context, which is important for accurate context tracking and state management. ## Changes This PR reorders the EOS handling logic to: 1. **Decode the token before the EOS check** — previously, the code checked `model.SpecialEOS` before calling `Decode`, so the final piece was unavailable when the sequence terminated. 2. **Append the EOS piece to `pendingResponses`** — ensures the decoded content is queued for delivery. 3. **Flush pending responses before sequence removal** — calls `flushPending(seq)` to send the EOS piece through `seq.responses` before invoking `removeSequence`. 4. **Preserve connection-closed handling** — if the client disconnects mid-flush, the sequence is still torn down cleanly with `DoneReasonConnectionClosed` instead of `DoneReasonStop`. The existing UTF-8 validation in `flushPending` ensures no partial code points are emitted, and because `removeSequence` also calls `flushPending` on an already-empty buffer, there is no risk of duplicate sends. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-13 00:40:35 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#13931