[PR #15088] openai: fix inconsistent created timestamp across streaming chunks #61710

Open
opened 2026-04-29 16:44:52 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/15088
Author: @Muszic
Created: 3/27/2026
Status: 🔄 Open

Base: mainHead: fix-openai-streaming-timestamp


📝 Commits (1)

  • 077a50b openai: fix inconsistent created timestamp across streaming chunks

📊 Changes

3 files changed (+215 additions, -212 deletions)

View changed files

📝 middleware/openai.go (+7 -3)
📝 openai/openai.go (+71 -72)
📝 openai/openai_test.go (+137 -137)

📄 Description

What is the Bug?

Currently, the created field in every streaming chunk (ChatCompletionChunk) uses time.Now().Unix(), while the non-streaming final response uses r.CreatedAt.Unix().

This violates the OpenAI specification, which states that all streaming chunks for the same request must share the same created timestamp. With time.Now() called independently on every chunk via ToChunk, a long response produces chunks with sliding timestamps.

The Fix

  1. Added a createdAt int64 field to both ChatWriter and CompleteWriter structs in middleware/openai.go.
  2. Initialized it exactly once with time.Now().Unix() when the middleware sets up the writer.
  3. Passed this static timestamp down through ToChunks, ToChunk, and ToCompleteChunk in openai/openai.go to ensure absolute timestamp consistency across the entire stream.
  4. Updated existing tests in openai_test.go to pass a static timestamp.

🔄 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/15088 **Author:** [@Muszic](https://github.com/Muszic) **Created:** 3/27/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix-openai-streaming-timestamp` --- ### 📝 Commits (1) - [`077a50b`](https://github.com/ollama/ollama/commit/077a50baa53553339fa1b545c74e08d88db90add) openai: fix inconsistent created timestamp across streaming chunks ### 📊 Changes **3 files changed** (+215 additions, -212 deletions) <details> <summary>View changed files</summary> 📝 `middleware/openai.go` (+7 -3) 📝 `openai/openai.go` (+71 -72) 📝 `openai/openai_test.go` (+137 -137) </details> ### 📄 Description ### What is the Bug? Currently, the `created` field in every streaming chunk (`ChatCompletionChunk`) uses `time.Now().Unix()`, while the non-streaming final response uses `r.CreatedAt.Unix()`. This violates the OpenAI specification, which states that all streaming chunks for the same request must share the same `created` timestamp. With `time.Now()` called independently on every chunk via `ToChunk`, a long response produces chunks with sliding timestamps. ### The Fix 1. Added a `createdAt` `int64` field to both `ChatWriter` and `CompleteWriter` structs in `middleware/openai.go`. 2. Initialized it exactly once with `time.Now().Unix()` when the middleware sets up the writer. 3. Passed this static timestamp down through `ToChunks`, `ToChunk`, and `ToCompleteChunk` in `openai/openai.go` to ensure absolute timestamp consistency across the entire stream. 4. Updated existing tests in `openai_test.go` to pass a static timestamp. --- <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-29 16:44:52 -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#61710