Skip to content

feat(go): Add embedding dimension metadata and validation - #15939

Merged
JinHai-CN merged 1 commit into
infiniflow:mainfrom
Harry-Hz-Zhang:fix-dimension
Jun 11, 2026
Merged

JinHai-CN merged 1 commit into
infiniflow:mainfrom
Harry-Hz-Zhang:fix-dimension

Conversation

@Harry-Hz-Zhang

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

  • Replace embedding model dimension metadata with max_dimension.
  • Add optional dimensions metadata for models with fixed selectable output dimensions.
  • Include max_dimension and dimensions in model list responses.
  • Validate requested embedding dimensions before calling provider embedding APIs.
  • Forward SiliconFlow embedding dimensions with the correct dimensions request field.
  • Add unit coverage for embedding dimension validation rules.

Why

  • Some embedding providers support configurable output dimensions, while others only support a documented set of dimensions. RAGFlow should expose this metadata and reject unsupported requested dimensions before sending invalid embedding requests to providers.

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. 💞 feature Feature request, pull request that fullfill a new feature. labels Jun 11, 2026
@coderabbitai

coderabbitai Bot commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The PR updates model dimension support from a single field to a more expressive schema. Dimension is replaced by MaxDimension (nullable upper bound for custom embedding dimensions) and Dimensions (array of explicitly supported dimensions). This change propagates through entity types, provider responses, and service layer embedding validation logic.

Changes

Embedding Dimension Schema and Validation

Layer / File(s) Summary
Response schema and model struct
internal/entity/models/types.go, internal/entity/models/model.go
Response type ListModelResponse and entity struct Model replace Dimension *int with MaxDimension *int and Dimensions []int to express both custom and discrete dimension support.
Provider response population
internal/entity/models/model.go
ProviderManager.ListAllModels and ListModels now conditionally populate max_dimension and dimensions fields in response payloads when the model entity carries these values.
Entity list response mappers
internal/entity/models/base_model.go, internal/entity/models/replicate.go
ParseListModel and ReplicateModel.ListModels extract and map model entity MaxDimension and Dimensions to the updated response fields.
Service list response
internal/service/model_service.go
ListSupportedModels surfaces embedding dimension capabilities via max_dimension and dimensions populated from the model entity.
Dimension validation and embedding request handling
internal/service/model_service.go, internal/service/model_service_test.go
New validateEmbeddingDimension helper validates requested embedding dimensions against explicit Dimensions list or MaxDimension upper bound. EmbedText invokes validation in both provider and local-deployed model paths, returning CodeBadRequest on constraint violation. Table-driven tests cover nil/zero requests, supported dimensions, custom dimensions within bounds, and constraint violations.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A dimension dance, from one field to two,
MaxDimension bounds and Dimensions true,
Embeddings validated with careful care,
Constraints now checked throughout the layer!

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The description covers the problem statement and rationale but lacks explicit type-of-change selection as required by the repository template. Select an appropriate type of change (e.g., New Feature) by checking the corresponding checkbox in the template.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: adding embedding dimension metadata and validation logic to the codebase.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Harry-Hz-Zhang Harry-Hz-Zhang added the ci Continue Integration label Jun 11, 2026
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as draft June 11, 2026 07:18
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as ready for review June 11, 2026 07:19

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
internal/service/model_service_test.go (1)

13-51: ⚡ Quick win

Add a negative-dimension test case to lock the new validation contract.

Please add coverage for requested < 0 returning an error so this edge case cannot regress.

Suggested test case
 		{
+			name:      "rejects negative requested dimension",
+			model:     &modelModule.Model{Name: "embedding-3", MaxDimension: &maxDimension, Dimensions: []int{256, 512, 1024, 2048}},
+			requested: -1,
+			wantErr:   "positive integer",
+		},
+		{
 			name:      "allows dimension listed in explicit options",
 			model:     &modelModule.Model{Name: "embedding-3", MaxDimension: &maxDimension, Dimensions: []int{256, 512, 1024, 2048}},
 			requested: 1024,
 		},
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/service/model_service_test.go` around lines 13 - 51, Add a
negative-dimension test to the existing tests slice: include a new entry
(alongside the other cases) where requested is -1 and model is either nil or a
representative modelModule.Model, and set wantErr to a string that asserts the
validator rejects negative dimensions (e.g., "requested < 0" or "invalid
dimension"); this ensures the validation logic that reads requested and checks
against modelModule.Model (MaxDimension/Dimensions) returns an error for
requested < 0.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/service/model_service.go`:
- Around line 1112-1115: The function validateEmbeddingDimension currently
treats any requested <= 0 as "unset" and skips validation; change it to
explicitly reject negative values by returning an error when requested < 0,
while preserving the current bypass behavior only for requested == 0 or when
model == nil. Update validateEmbeddingDimension (which takes model
*modelModule.Model and requested int) to check for requested < 0 and return a
clear error (e.g., "invalid embedding dimension") before any further logic.

---

Nitpick comments:
In `@internal/service/model_service_test.go`:
- Around line 13-51: Add a negative-dimension test to the existing tests slice:
include a new entry (alongside the other cases) where requested is -1 and model
is either nil or a representative modelModule.Model, and set wantErr to a string
that asserts the validator rejects negative dimensions (e.g., "requested < 0" or
"invalid dimension"); this ensures the validation logic that reads requested and
checks against modelModule.Model (MaxDimension/Dimensions) returns an error for
requested < 0.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5497102b-f016-4f98-9108-35d528121330

📥 Commits

Reviewing files that changed from the base of the PR and between 47fb462 and cf8f135.

📒 Files selected for processing (6)
  • internal/entity/models/base_model.go
  • internal/entity/models/model.go
  • internal/entity/models/replicate.go
  • internal/entity/models/types.go
  • internal/service/model_service.go
  • internal/service/model_service_test.go

Comment on lines +1112 to +1115
func validateEmbeddingDimension(model *modelModule.Model, requested int) error {
if requested <= 0 || model == nil {
return nil
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Reject negative embedding dimensions instead of treating them as unset.

requested <= 0 currently bypasses validation, so negative values are forwarded downstream. That breaks the new “validate before provider call” guarantee.

Proposed fix
 func validateEmbeddingDimension(model *modelModule.Model, requested int) error {
-	if requested <= 0 || model == nil {
+	if requested == 0 {
+		return nil
+	}
+	if requested < 0 {
+		return fmt.Errorf("dimension must be a positive integer")
+	}
+	if model == nil {
 		return nil
 	}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/service/model_service.go` around lines 1112 - 1115, The function
validateEmbeddingDimension currently treats any requested <= 0 as "unset" and
skips validation; change it to explicitly reject negative values by returning an
error when requested < 0, while preserving the current bypass behavior only for
requested == 0 or when model == nil. Update validateEmbeddingDimension (which
takes model *modelModule.Model and requested int) to check for requested < 0 and
return a clear error (e.g., "invalid embedding dimension") before any further
logic.

@codecov

codecov Bot commented Jun 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.16%. Comparing base (ca00d23) to head (cf8f135).
⚠️ Report is 6 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #15939   +/-   ##
=======================================
  Coverage   93.16%   93.16%           
=======================================
  Files          10       10           
  Lines         717      717           
  Branches      118      118           
=======================================
  Hits          668      668           
  Misses         29       29           
  Partials       20       20           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration 💞 feature Feature request, pull request that fullfill a new feature. size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants