Bug Report: Embedding dimension mismatch for Jina-v3 and others (Returning 1/4 of expected dimensions)
Description When using embedding models via the LocalAI OpenAI-compatible endpoint (specifically /v1/embeddings), the API returns vectors with only 1/4 of their native dimension if the dimensions parameter is omitted in the request.
This issue was primarily observed with jina-embeddings-v3, which returns 256 dimensions instead of its native 1024.
Crucial Observation: This is not limited to models that support Matryoshka Embeddings (like Jina-v3). We have seen similar behavior where LocalAI defaults to truncated output dimensions for other models that do not explicitly support this feature, leading to vector database errors (dimension mismatch) in client applications like AnythingLLM.
Steps to Reproduce Start LocalAI with jina-embeddings-v3 (or another model like bge-base-en-v1.5) configured. Send a POST request to /v1/embeddings without specifying the dimensions parameter. Observe the length of the returned embedding vector. Example Request:
{ "model": "jina-embeddings-v3", "input": "This is a test sentence." } Expected Behavior The API should return the full native vector length of the model (e.g., 1024 for jina-embeddings-v3) unless a different dimension is explicitly requested. This is the standard behavior of the OpenAI API which LocalAI aims to emulate.
Actual Behavior The API returns a truncated vector (e.g., 256 dimensions for jina-embeddings-v3).
Environment LocalAI Version: [Bitte hier deine Version eintragen, z.B. v2.21.0] Models tested: jina-embeddings-v3 (Native: 1024, Received: 256) [Andere betroffene Modelle hier eintragen] Client: Official openai JavaScript library (v4.x) / AnythingLLM Additional Context & Investigation In AnythingLLM, which uses the official openai Node.js SDK, we encountered persistent dimension mismatch errors. Upon investigation, we found:
SDK Behavior: The standard OpenAI SDK does not send a dimensions field by default if it's not explicitly provided by the user. Incompatibility: While a real OpenAI endpoint returns the full vector, LocalAI seems to apply an internal default that triggers a "small" output (exactly native / 4) when the parameter is missing. Non-Matryoshka Models: Even models without native Matryoshka support are affected by this truncation, which suggests a general issue in how the embedding wrapper handles default parameters. Impact: This causes immediate failures in vector databases (Pinecone, Chroma, LanceDB, etc.) because the returned vector length does not match the collection's defined dimension. Current Workaround in AnythingLLM: We had to bypass the official OpenAI SDK and implement a manual fetch call to ensure the dimensions parameter is always explicitly sent:
// Workaround to force full dimensions in LocalAI const body = { model: "jina-embeddings-v3", input: chunk, dimensions: 1024 // Manually forcing 1024 fixes the issue };
const response = await fetch(${baseURL}/embeddings, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
Suggestion
LocalAI should ensure that for all models, the default behavior of the /v1/embeddings endpoint is to return the full native dimension (standard OpenAI-compatible behavior) unless the user explicitly requests a different size. Clients using standard SDKs should not be required to know and send the native dimension just to get a valid full-length vector.
Versions: LocalAI v3.12.1 (fcecc12e57be39bad2ebf50cf729408b64409553) (but has been there in all versions since 3.9.x) AnythinLLM 1.11.0 (but has been there forever)
Thank you for this detailed report. This appears to be a well-defined issue with the embedding endpoint returning truncated vectors when the dimensions parameter is omitted. I'll create a task for investigation and potential fix.
@mudler is there any rough idea of a timeline when this issue could be available in a docker image? I need this to see how to approach the next steps of a project. Either I can wait or need to find another solution. Any help really appreciated. I cannot make sense or determine the current state oder next steps from the git history
@walcz-de the fix for this is already in the 4.0.0 release - do you still see this issue? The fix above was API-only as we were cutting leading 0s from results (that might have given wrong results occasionally).
Otherwise if persist can you share your model config? It might look more a backend issue rather then API, but without more details on how you configure the model I can't help.
Did you tried for instance other smaller models (like granite-embedding-107m-multilingual) ? These gets covered in the CI and getting continuously tested
As of yesterday with 4.0.0 I still had the issue, since the both commits I found in the repo were not merged I thought thy would have made it to the codebase.. I will test again and get back to you with the findings and config if necessary
Hi Again, so I was looking into the issue today a little deeper.
First test: since I had a very fine tuned setup with a lot of optimisations and a custom build llama-cpp (rocm-7.2 ) which is working fine performance wise. I decided to "bootstrap" my setup. Meaning:
- localai 4.0.0 latest--gpu-hipblas
- minimal docker-compose setup, no specific optimiztions just added qdratn 1.17 to make the case. Runniing on my Geekom A9-Mega with 128GB RAM (yeah the sometimes tricky strip halo)
- frontend running on my Mac Anythingllm 1.11.2
I ended up:
INFO BackendLoader starting modelID="granite-embedding-107m-multilingual" backend="llama-cpp" model="granite-embedding-107m-multilingual-f16.gguf" Mar 24 20:25:55 ERROR Failed to load model modelID="granite-embedding-107m-multilingual" error=failed to load model with internal loader: could not load model: rpc error: code = Unavailable desc = error reading from server: EOF backend="llama-cpp"
second test: my old setup but using jina-embeddings-v3 again: still 256 dimensions besides 1024 as configured.
I am going to do a third test:
Building localai from scratch myself using rocm 7.11-preview (with a little help from friend Claude). build went through fallback to CPU though, but still: 256 instead of 1024
I am going to have a look at it tomorrow again.
P.S. I also tried configuring everything to 256 dimensions since Jina-embeddings-v3 supports matryoshka. While processes went through. the Vectors created were. unusable with quadrants search quality feature measuring a flat 0 so this workaround doesn't help either. ..
@mudler while analyzing I found the solution and provided a PR for it #9135
maybe this helps
I'm guessing that not truncating trailing zeroes has caused a bug when trying the Qwen 2b and 8b embeddings: the backend traces show that embedding_dimensions is 20480 for 2b and 40960 for 8b instead of 2048 and 4096. This still occurs even if 2048 and 4096 are passed into the API call. Using v4.3.1 gpu-vulkan docker image.
This was the OpenAI-SDK encoding_format=base64 mismatch: older LocalAI returned a raw float array where the SDK expected a base64 string and decoded it as bytes, yielding exactly 1/4 of the dimensions. It is fixed since v4.4.0 - the embeddings endpoint now honors encoding_format and base64-encodes the floats (core/http/endpoints/openai/embeddings.go). Please upgrade to v4.4.0+. Closing as resolved.