[PR #14225] openai: use crypto/rand for unique request IDs #14579

Open
opened 2026-04-13 00:58:27 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/14225
Author: @shtse8
Created: 2/12/2026
Status: 🔄 Open

Base: mainHead: fix/openai-random-ids


📝 Commits (1)

  • df599d5 openai: use crypto/rand for unique request IDs

📊 Changes

4 files changed (+63 additions, -8 deletions)

View changed files

📝 middleware/openai.go (+4 -5)
📝 openai/openai.go (+12 -0)
📝 openai/openai_test.go (+45 -0)
📝 openai/responses.go (+2 -3)

📄 Description

Summary

Replace weak math/rand-based ID generation with crypto/rand-based IDs that are both unique and match the format used by the OpenAI API more closely.

Problem

The current ID generation for OpenAI-compatible endpoints uses rand.Intn(999) which only produces 1,000 possible values:

  • chatcmpl-42
  • cmpl-123

And rand.Intn(999999) for responses API producing 1,000,000 possible values:

  • resp_456789
  • msg_123456

This is problematic because:

  1. ID collisions are likely in concurrent usage scenarios
  2. Clients that use IDs for deduplication or request tracking may see incorrect behavior
  3. The format doesn't match OpenAI's actual ID format

Solution

Add openai.GenerateID() which uses crypto/rand to generate 18 random bytes (2^144 possible values) encoded as base64url. This produces IDs like:

  • chatcmpl-7Z3nR4v8T2b1eLqXyZAbCd
  • resp-Kf2mN9pQ4rS6tU8vW0xYzA

This follows the same pattern already used in the Anthropic middleware (anthropic/anthropic.go:generateID).

Changes

  • Add openai.GenerateID() using crypto/rand with base64url encoding
  • Update middleware/openai.go to use GenerateID for chat completions, completions, and responses API IDs
  • Update openai/responses.go to use GenerateID for reasoning and function call item IDs
  • Remove unused math/rand imports
  • Add tests for GenerateID

🔄 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/14225 **Author:** [@shtse8](https://github.com/shtse8) **Created:** 2/12/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/openai-random-ids` --- ### 📝 Commits (1) - [`df599d5`](https://github.com/ollama/ollama/commit/df599d5067061f784c45d27de8429016cbe6775a) openai: use crypto/rand for unique request IDs ### 📊 Changes **4 files changed** (+63 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `middleware/openai.go` (+4 -5) 📝 `openai/openai.go` (+12 -0) 📝 `openai/openai_test.go` (+45 -0) 📝 `openai/responses.go` (+2 -3) </details> ### 📄 Description ## Summary Replace weak `math/rand`-based ID generation with `crypto/rand`-based IDs that are both unique and match the format used by the OpenAI API more closely. ## Problem The current ID generation for OpenAI-compatible endpoints uses `rand.Intn(999)` which only produces **1,000 possible values**: - `chatcmpl-42` - `cmpl-123` And `rand.Intn(999999)` for responses API producing **1,000,000 possible values**: - `resp_456789` - `msg_123456` This is problematic because: 1. **ID collisions** are likely in concurrent usage scenarios 2. Clients that use IDs for deduplication or request tracking may see incorrect behavior 3. The format doesn't match OpenAI's actual ID format ## Solution Add `openai.GenerateID()` which uses `crypto/rand` to generate 18 random bytes (2^144 possible values) encoded as base64url. This produces IDs like: - `chatcmpl-7Z3nR4v8T2b1eLqXyZAbCd` - `resp-Kf2mN9pQ4rS6tU8vW0xYzA` This follows the same pattern already used in the Anthropic middleware (`anthropic/anthropic.go:generateID`). ## Changes - Add `openai.GenerateID()` using `crypto/rand` with base64url encoding - Update `middleware/openai.go` to use `GenerateID` for chat completions, completions, and responses API IDs - Update `openai/responses.go` to use `GenerateID` for reasoning and function call item IDs - Remove unused `math/rand` imports - Add tests for `GenerateID` --- <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:58:27 -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#14579