Skip to content

[BUG] Ollama batch embedding fails after PR #621 (array input rejected) #629

@jlin53882

Description

@jlin53882

Bug Description (Detailed Analysis)

After PR #621 fixed Ollama 0.20.5+ single embedding, batch embedding now fails because /api/embeddings only accepts a single string prompt field, not an array.

Error

Error: Failed to generate batch embeddings from Ollama: Ollama embedding failed: 400 Bad Request ??{"error":"json: cannot unmarshal array into Go struct field EmbeddingRequest.prompt of type string"}

Root Cause Analysis

PR #621 Background

PR #621 (fixing Issue #620) discovered that Ollama 0.20.5+ returns empty arrays when using /v1/embeddings endpoint. The fix switched to using Ollama's native /api/embeddings endpoint with the prompt field instead:

// PR #621 fix in embedWithNativeFetch()
const ollamaPayload = {
  model: payload.model,
  prompt: payload.input,  // <-- This works for single string
};

The Regression

The problem is that payload.input is not always a string. When embedMany() builds a batch payload, it passes an array:

// In embedMany()
const response = await this.embedWithRetry(
  this.buildPayload(validTexts, task),  // <-- validTexts is string[]
  signal,
);

// In buildPayload()
const payload = {
  model: this.model,
  input,  // <-- When input is string[], this becomes input: ["a", "b", "c"]
};

So when embedWithNativeFetch() receives the payload and sets prompt: payload.input, it sends an array where Ollama expects a string:

// What Ollama receives:
{
  "model": "jina-v5-retrieval-test",
  "prompt": ["a", "b", "c"]  // <-- Array, but Ollama expects string!
}

// Ollama error:
"json: cannot unmarshal array into Go struct field EmbeddingRequest.prompt of type string"

Why Single Works But Batch Fails

Scenario payload.input type Works?
embedPassage("hello") string
embedQuery("hello") string
embedBatchPassage(["a","b"]) string[]

Suggested Fix

This is a routing fix based on verified behavior, not a universal solution.

Limitation: This assumes /v1/embeddings batch mode is valid for the target Ollama/model combination, which was verified in local testing with jina-v5-retrieval-test.

Testing

Local testing confirms both single and batch work correctly:

Test Endpoint Input Result
Single /api/embeddings prompt: string ✅ SUCCESS (1024 dims)
Batch 3 items /v1/embeddings input: string[] ✅ SUCCESS (3 embeddings)
Batch 1 item /v1/embeddings input: string[] ✅ SUCCESS

Model: jina-v5-retrieval-test

Related

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions