Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(embedding): reject mismatched provider response counts (#20662)

Zheng Haorand7cbff21b950
diff --git a/internal/entity/models/embed_count_test.go b/internal/entity/models/embed_count_test.go
new file mode 100644
index 0000000000..5e61739d53
--- /dev/null
+++ b/internal/entity/models/embed_count_test.go
@@ -0,0 +1,110 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package models
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "sync/atomic"
+ "testing"
+
+ "ragflow/internal/common"
+)
+
+func TestEmbedRejectsUnexpectedResponseCount(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ inputs int
+ outputs int
+ wantError bool
+ }{
+ {"empty_query", 1, 0, true},
+ {"empty_batch", 2, 0, true},
+ {"short_batch", 2, 1, true},
+ {"extra_batch", 2, 3, true},
+ {"single_success", 1, 1, false},
+ {"batch_success", 2, 2, false},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ var calls atomic.Int32
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ calls.Add(1)
+ if r.Method != http.MethodPost || r.URL.Path != "/embeddings" {
+ t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
+ }
+ data := make([]map[string]any, tc.outputs)
+ for i := range data {
+ data[i] = map[string]any{"index": i, "embedding": []float64{float64(i + 1), float64(i + 2)}}
+ }
+ w.Header().Set("Content-Type", "application/json")
+ if err := json.NewEncoder(w).Encode(map[string]any{"data": data}); err != nil {
+ t.Errorf("encode response: %v", err)
+ }
+ }))
+ defer server.Close()
+
+ driver := newOpenAIForTest(server.URL)
+ // Exercise the real driver's response decoding with the local mock's client.
+ driver.baseModel.httpClient = server.Client()
+ model := newEmbedModel(t, driver, "", 8192)
+ modelName := "test-embedding"
+ model.ModelName = &modelName
+ texts := make([]string, tc.inputs)
+ for i := range texts {
+ texts[i] = fmt.Sprintf("input %d", i)
+ }
+ got, err := model.Embed(t.Context(), EmbedRequest{Texts: texts}, nil, nil)
+ if tc.wantError {
+ want := fmt.Sprintf("unexpected embedding count: got %d, want %d", tc.outputs, tc.inputs)
+ if err == nil || !strings.Contains(err.Error(), want) {
+ t.Fatalf("error = %v, want %q", err, want)
+ }
+ if got != nil {
+ t.Fatalf("invalid response returned partial results: %v", got)
+ }
+ } else if err != nil || len(got) != len(texts) {
+ t.Fatalf("complete response: embeddings = %d, error = %v", len(got), err)
+ }
+ if got := calls.Load(); got != 1 {
+ t.Fatalf("provider calls = %d, want 1; count mismatches must not trigger over-limit retries", got)
+ }
+ })
+ }
+}
+
+type emptyIsolatedEmbeddingDriver struct{ recordingEmbedDriver }
+
+func (d *emptyIsolatedEmbeddingDriver) Embed(ctx context.Context, modelName *string, req EmbedRequest, apiConfig *APIConfig, config *EmbeddingConfig, usage *common.ModelUsage) ([]EmbeddingData, error) {
+ _, err := d.recordingEmbedDriver.Embed(ctx, modelName, req, apiConfig, config, usage)
+ return nil, err
+}
+
+func TestEmbedRejectsEmptyIsolatedResponse(t *testing.T) {
+ driver := &emptyIsolatedEmbeddingDriver{recordingEmbedDriver: recordingEmbedDriver{rejectBatch: true}}
+ model := newEmbedModel(t, driver, "", 8192)
+ got, err := model.Embed(t.Context(), EmbedRequest{Texts: []string{"first", "second"}}, nil, nil)
+ if err == nil || !strings.Contains(err.Error(), "embedding input 0: driver returned 0 embeddings for one text") {
+ t.Fatalf("error = %v, want the isolated input count error", err)
+ }
+ if got != nil {
+ t.Fatalf("invalid isolated response returned partial results: %v", got)
+ }
+}
diff --git a/internal/entity/models/embed_limit.go b/internal/entity/models/embed_limit.go
index 9e9178002d..3b70183615 100644
--- a/internal/entity/models/embed_limit.go
+++ b/internal/entity/models/embed_limit.go
@@ -28,6 +28,8 @@ import (
// and retries with a smaller budget if the provider still rejects an input as over
// its window. It is the embedding counterpart of RerankModel.Rerank, which cuts its
// documents the same way for the same reason.
+// A successful response must contain exactly one embedding per input text;
+// a count mismatch is an error, not a partial result or an over-limit rejection.
//
// Callers use it instead of ModelDriver.Embed because a provider does NOT truncate
// an over-window input: it rejects the whole request (SiliconFlow answers 400 with
@@ -72,6 +74,9 @@ func (m *EmbeddingModel) Embed(ctx context.Context, req EmbedRequest, embeddingC
for _, budget := range tokenizer.OverLimitLadder(window) {
embeds, err := m.embedCut(ctx, req, embeddingConfig, usage, counter, budget)
if err == nil {
+ if len(embeds) != len(req.Texts) {
+ return nil, fmt.Errorf("embedding model: unexpected embedding count: got %d, want %d", len(embeds), len(req.Texts))
+ }
return embeds, nil
}
if !tokenizer.IsOverLimitError(err) {
diff --git a/internal/service/nlp/retrieval_embedding_test.go b/internal/service/nlp/retrieval_embedding_test.go
new file mode 100644
index 0000000000..56667bbb31
--- /dev/null
+++ b/internal/service/nlp/retrieval_embedding_test.go
@@ -0,0 +1,47 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package nlp
+
+import (
+ "context"
+ "strings"
+ "testing"
+
+ "ragflow/internal/common"
+ "ragflow/internal/entity/models"
+)
+
+type emptyEmbeddingResponseDriver struct{ models.ModelDriver }
+
+func (*emptyEmbeddingResponseDriver) Embed(context.Context, *string, models.EmbedRequest, *models.APIConfig, *models.EmbeddingConfig, *common.ModelUsage) ([]models.EmbeddingData, error) {
+ return []models.EmbeddingData{}, nil
+}
+
+func TestGetVectorRejectsEmptyEmbeddingResponse(t *testing.T) {
+ t.Setenv("TOKENIZER_EMBEDDING_TOKENIZER", "")
+ t.Setenv("TOKENIZER_EMBEDDING_MAX_TOKENS", "8192")
+ key := t.Name()
+ model := models.NewEmbeddingModel(&emptyEmbeddingResponseDriver{}, nil, &models.APIConfig{ApiKey: &key}, 8192)
+ service := &RetrievalService{}
+ got, err := service.GetVector(t.Context(), "query", model, 10, 10, 0.1)
+ if err == nil || !strings.Contains(err.Error(), "unexpected embedding count: got 0, want 1") {
+ t.Fatalf("error = %v, want an empty embedding response error", err)
+ }
+ if got != nil {
+ t.Fatalf("invalid embedding response returned a dense expression: %#v", got)
+ }
+}