Skip to content

Commit 5d779e7

Browse files
authored
fix(ollama): correctly parse nested embeddings array from ollama endpoint /api/embed (#1227)
* fix: correctly parse nested embeddings array from /api/embed Updates the 'OllamaEmbeddingResponse' struct to match the official Ollama API, which returns a nested 'embeddings: Vec<Vec<f32>>' array instead of a flat 'embedding: Vec<f32>'. * fix: remove trailing newline
1 parent 67da574 commit 5d779e7

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/llm/ollama.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ struct OllamaEmbeddingRequest<'a> {
6464

6565
#[derive(Debug, Deserialize)]
6666
struct OllamaEmbeddingResponse {
67-
pub embedding: Vec<f32>,
67+
pub embeddings: Vec<Vec<f32>>,
6868
}
6969

7070
const OLLAMA_DEFAULT_ADDRESS: &str = "http://localhost:11434";
@@ -153,10 +153,17 @@ impl LlmEmbeddingClient for Client {
153153
)
154154
.await
155155
.context("Ollama API error")?;
156+
156157
let embedding_resp: OllamaEmbeddingResponse = resp.json().await.context("Invalid JSON")?;
157-
Ok(super::LlmEmbeddingResponse {
158-
embedding: embedding_resp.embedding,
159-
})
158+
159+
// Extract the first embedding (index 0)
160+
let embedding = embedding_resp
161+
.embeddings
162+
.into_iter()
163+
.next()
164+
.context("Ollama API returned no embeddings")?;
165+
166+
Ok(super::LlmEmbeddingResponse { embedding })
160167
}
161168

162169
fn get_default_embedding_dimension(&self, model: &str) -> Option<u32> {

0 commit comments

Comments
 (0)