Repository navigation
fix(go-models): harden Hunyuan embedding validation - #15249
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughHunyuanModel.Embed adds input validation (modelName, apiConfig.ApiKey), resolves the embeddings endpoint via baseURLForRegion(region), uses a timeout-bound request context, and preallocates the embeddings slice. Tests add routing for embeddings and include happy-path and input-validation tests. ChangesHunyuan Embedding Validation and Testing
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 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. Warning Review ran into problems🔥 ProblemsGit: Failed to clone repository. Please run the 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.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
internal/entity/models/hunyuan.go (1)
455-471:⚠️ Potential issue | 🟠 Major | ⚡ Quick winAdd a request deadline to
EmbedHTTP calls.Line 471 builds the request without context timeout. Since this driver’s
httpClienthas no client-levelTimeout,Embedcan hang indefinitely on slow/stalled response bodies.🔧 Proposed fix
+ ctx, cancel := context.WithTimeout(context.Background(), nonStreamCallTimeout) + defer cancel() - req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) + req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData)) if err != nil { return nil, fmt.Errorf("failed to create request: %w", err) }Based on learnings: when
httpClient.Timeoutis intentionally unset and only transport/header timeout is configured, per-requestcontext.WithTimeoutfor non-stream calls is appropriate.🤖 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/entity/models/hunyuan.go` around lines 455 - 471, The Embed HTTP call builds a request without a per-request deadline and can hang; update the Embed implementation to create the request with a context timeout by using context.WithTimeout and http.NewRequestWithContext (e.g., derive ctx from the existing ctx param, call ctx, cancel := context.WithTimeout(ctx, <reasonable duration or configurable field>), defer cancel()), then pass that ctx into http.NewRequestWithContext before calling a.httpClient.Do so the call cannot block indefinitely; reference the Embed function and a.httpClient/http.NewRequestWithContext when making this change.
🧹 Nitpick comments (1)
internal/entity/models/hunyuan_test.go (1)
462-481: ⚡ Quick winAdd explicit empty-string API key coverage in
Embedvalidation tests.You verify missing
ApiKey(nilpointer), but not the separate*ApiKey == ""branch. Adding it will lock the new validation path against regressions.🧪 Suggested test addition
func TestHunyuanEmbedValidatesInputs(t *testing.T) { apiKey := "test-key" model := "hunyuan-embedding" @@ if _, err := newHunyuanForTest("http://unused").Embed(&model, []string{"x"}, &APIConfig{}, nil); err == nil || !strings.Contains(err.Error(), "api key is required") { t.Errorf("missing api key: %v", err) } + emptyAPIKey := "" + if _, err := newHunyuanForTest("http://unused").Embed(&model, []string{"x"}, &APIConfig{ApiKey: &emptyAPIKey}, nil); err == nil || !strings.Contains(err.Error(), "api key is required") { + t.Errorf("empty api key: %v", err) + } }🤖 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/entity/models/hunyuan_test.go` around lines 462 - 481, TestHunyuanEmbedValidatesInputs doesn't cover the case where APIConfig.ApiKey is a non-nil pointer to an empty string; add an assertion that newHunyuanForTest(...).Embed(&model, []string{"x"}, &APIConfig{ApiKey: &emptyKey}, nil) returns an error and that err.Error() contains "api key is required". Update TestHunyuanEmbedValidatesInputs to declare emptyKey := "" and call Embed with &APIConfig{ApiKey: &emptyKey}, referencing newHunyuanForTest and Embed to ensure the empty-string branch of validation is exercised.
🤖 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.
Outside diff comments:
In `@internal/entity/models/hunyuan.go`:
- Around line 455-471: The Embed HTTP call builds a request without a
per-request deadline and can hang; update the Embed implementation to create the
request with a context timeout by using context.WithTimeout and
http.NewRequestWithContext (e.g., derive ctx from the existing ctx param, call
ctx, cancel := context.WithTimeout(ctx, <reasonable duration or configurable
field>), defer cancel()), then pass that ctx into http.NewRequestWithContext
before calling a.httpClient.Do so the call cannot block indefinitely; reference
the Embed function and a.httpClient/http.NewRequestWithContext when making this
change.
---
Nitpick comments:
In `@internal/entity/models/hunyuan_test.go`:
- Around line 462-481: TestHunyuanEmbedValidatesInputs doesn't cover the case
where APIConfig.ApiKey is a non-nil pointer to an empty string; add an assertion
that newHunyuanForTest(...).Embed(&model, []string{"x"}, &APIConfig{ApiKey:
&emptyKey}, nil) returns an error and that err.Error() contains "api key is
required". Update TestHunyuanEmbedValidatesInputs to declare emptyKey := "" and
call Embed with &APIConfig{ApiKey: &emptyKey}, referencing newHunyuanForTest and
Embed to ensure the empty-string branch of validation is exercised.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 03f5dd74-3e58-45bc-8373-d6d777348220
📒 Files selected for processing (2)
internal/entity/models/hunyuan.gointernal/entity/models/hunyuan_test.go
Use a request-scoped timeout for Hunyuan embedding calls and cover empty API key pointers in embedding validation tests.
|
Another improvement, if you don't mind reviewing - thanks @Haruko386 @JinHai-CN |
|
Thanks for merging my other PRs! I believe this one is ready for merge as well, @JinHai-CN @Haruko386 🤝 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15249 +/- ##
==========================================
- Coverage 94.56% 93.16% -1.40%
==========================================
Files 10 10
Lines 717 717
Branches 118 118
==========================================
- Hits 678 668 -10
- Misses 25 29 +4
- Partials 14 20 +6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary
What changed
Why
Hunyuan Embed is implemented, but the existing test still expected it to be unsupported and could panic before returning a normal validation error. This keeps the implemented embedding path aligned with the current driver behavior and prevents nil input panics.
Closes #15087
Refs #14736
Validation
go test ./internal/entity/models -run TestHunyuan -count=1git diff --checkgo test ./internal/entity/models -count=1currently fails on unrelatedTestAstraflowEmbedReturnsNoSuchMethod, outside this diff.