Repository navigation
Fix(Go): make OpenRouter Encode fail loudly on malformed responses - #14717
Conversation
|
ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThis PR refactors OpenRouterModel.Encode to use a typed openrouterEmbeddingResponse, requires non-nil/non-empty modelName, conditionally includes a dimensions field when provided, and enforces strict response validation: matching counts, in-range non-duplicate indexes, and direct embedding extraction. ChangesOpenRouter Embeddings Validation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ 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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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/entity/models/minimax.go`:
- Around line 353-355: The code currently accepts vectors on HTTP 200 without
verifying MiniMax's application-level success; update the Embed response parsing
(type minimaxEmbedResponse handling used by the Encode method) to inspect the
nested base_resp.status_code field first and only proceed to use Vectors when
status_code indicates success; if status_code indicates an error, return or
propagate an error containing the base_resp message/details instead of trusting
vectors. Ensure the same check is added for the other Embed parsing site
referenced around lines 430-437 so both code paths validate
base_resp.status_code before accepting the vectors slice.
- Around line 407-416: Create a per-call context with a deadline using
context.WithTimeout and use http.NewRequestWithContext instead of
http.NewRequest so the HTTP call is request-scoped; specifically, wrap the call
that builds the request (currently using http.NewRequest with variables endpoint
and jsonData) in a ctx, cancel := context.WithTimeout(parentCtx, timeout) (defer
cancel()), call http.NewRequestWithContext(ctx, "POST", endpoint,
bytes.NewBuffer(jsonData)), preserve setting headers (req.Header.Set...) and
pass that request to z.httpClient.Do(req); add necessary imports (context, time)
and use an incoming context parameter if available or a sensible timeout
constant.
🪄 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: 93194ebb-e604-4201-93b5-6510d45d44cb
📒 Files selected for processing (2)
conf/models/minimax.jsoninternal/entity/models/minimax.go
71954c5 to
2c31f43
Compare
|
Hi, @Haruko386 |
Haruko386
left a comment
There was a problem hiding this comment.
Hi @Joseff531, please let me know where you find embo-01
MiniMax Model List
2c31f43 to
cd9dbf5
Compare
|
@Joseff531 Encode interface is updated, would you please update your implementation accordingly? please refer to #14735 |
cd9dbf5 to
a9999ad
Compare
Thanks for the heads-up. Rebased this branch onto current main and updated the OpenRouter Encode to honor the new EmbeddingConfig.Dimension field — when set, it's now passed as the |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #14717 +/- ##
==========================================
- Coverage 95.59% 94.16% -1.43%
==========================================
Files 10 10
Lines 703 703
Branches 112 112
==========================================
- Hits 672 662 -10
- Misses 21 25 +4
- Partials 10 16 +6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…nfiniflow#14717) ### What problem does this PR solve? The OpenRouter `Encode` method silently swallowed malformed responses. If a `data[]` item from the API was missing a field (`index`, `embedding`, or unexpected shape), the loop did `continue` instead of returning an error — leaving `nil` entries in the result slice. Callers got back partial results with no indication anything went wrong, which then crashes downstream consumers when they try to use a `nil` vector. There were three concrete gaps: - No count-mismatch check between `data` length and input texts (only checked for empty) - No duplicate-index detection (a duplicate would silently overwrite) - Parse failures on individual items returned partial slices instead of erroring This PR replaces `map[string]interface{}` parsing with a typed `openrouterEmbeddingResponse` struct and applies the same 3-layer validation used in the other drivers (count mismatch → out-of-range index → duplicate index), so any malformed response produces a clear error instead of corrupted data. ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue)
What problem does this PR solve?
The OpenRouter
Encodemethod silently swallowed malformed responses. If adata[]item from the API was missing a field (index,embedding, or unexpected shape), the loop didcontinueinstead of returning an error — leavingnilentries in the result slice. Callers got back partial results with no indication anything went wrong, which then crashes downstream consumers when they try to use anilvector. There were three concrete gaps:datalength and input texts (only checked for empty)This PR replaces
map[string]interface{}parsing with a typedopenrouterEmbeddingResponsestruct and applies the same 3-layer validation used in the other drivers (count mismatch → out-of-range index → duplicate index), so any malformed response produces a clear error instead of corrupted data.Type of change