Repository navigation
fix: align model default handling - #16782
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughThis PR adds ChangesModel ID plumbing and tenant field migration
Thread pool execution behavior
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant Client
participant ModelsAPI
participant ModelsApiService
participant TenantModelService
Client->>ModelsAPI: PATCH /models/default with model_id
ModelsAPI->>ModelsApiService: set_tenant_default_models(...)
ModelsApiService->>TenantModelService: resolve_model_id / validate model
ModelsApiService->>ModelsApiService: update default model fields
ModelsApiService-->>ModelsAPI: success response
ModelsAPI-->>Client: updated defaults
Possibly related PRs
Suggested labels: Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
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: 5
🧹 Nitpick comments (1)
api/apps/services/models_api_service.py (1)
249-287: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd logging around the new default-model flows.
The new
model_id, legacy resolution, and clear branches update tenant defaults without branch-level logs. Add low-cardinality logs such as path andmodel_type, avoiding secrets or API keys. As per coding guidelines,**/*.py: Add logging for new flows.🤖 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 `@api/apps/services/models_api_service.py` around lines 249 - 287, The new default-model handling in the tenant update path lacks branch-level logging for the `model_id`, legacy `model_provider/model_instance/model_name`, and clear-default flows. Add low-cardinality logs inside the `models_api_service` default-model logic around `_check_model_available`, `resolve_model_id`, and the `TenantService.update_by_id` update, including the path taken and `model_type` while avoiding any secrets or API keys.Source: Coding guidelines
🤖 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 `@api/apps/restful_apis/chat_api.py`:
- Around line 401-404: The chat request defaulting logic in the request handler
should fall back to the tenant’s non-null LLM identifier when the tenant default
is unavailable. Update the `req.setdefault("llm_id", ...)` and subsequent `if
req["llm_id"] is None` handling in the chat request flow to use `tenant.llm_id`
as the fallback when `tenant.tenant_llm_id` is missing, so
`DialogService.save()` never receives a null `llm_id`.
In `@api/apps/restful_apis/models_api.py`:
- Around line 78-79: The default-model response schema is missing the new
model_id field even though list_tenant_default_models now returns it. Update the
response documentation in get_default_models so the models[] item schema
includes model_id, matching the added-model schema used elsewhere in
models_api.py.
In `@api/apps/services/models_api_service.py`:
- Line 244: The tenant lookup in TenantService.get_by_id is only being used for
existence checking, so the unused tenant binding should be renamed to satisfy
Ruff RUF059. Update the assignment in the models_api_service flow to keep the
error value binding while discarding the tenant result with an ignored
placeholder, and make sure the surrounding existence check logic remains
unchanged.
- Around line 274-280: The model lookup flow in the availability check is using
the raw API model type and then treating a missing tenant model ID as a hard
failure, which breaks legacy and builtin paths. In the same block that calls
_check_model_available and resolve_model_id, normalize the model type to the
canonical value used by _check_model_available before building the default model
key, and update the fallback so TEI builtin embeddings can proceed when
resolve_model_id returns None instead of immediately returning “not found”. Keep
the behavior aligned with the existing legacy mapping logic in
_check_model_available and the builtin handling in resolve_model_id.
In `@api/db/services/compilation_template_service.py`:
- Around line 40-42: The guard in fill_config_default_llm and _to_saved_dict is
checking the legacy tenant.llm_id field but writing tenant.tenant_llm_id into
config["llm_id"], which can leave a None value behind. Update both checks to
validate tenant.tenant_llm_id before assigning, and only populate
config["llm_id"] when that new field is present so the fallback behavior stays
consistent.
---
Nitpick comments:
In `@api/apps/services/models_api_service.py`:
- Around line 249-287: The new default-model handling in the tenant update path
lacks branch-level logging for the `model_id`, legacy
`model_provider/model_instance/model_name`, and clear-default flows. Add
low-cardinality logs inside the `models_api_service` default-model logic around
`_check_model_available`, `resolve_model_id`, and the
`TenantService.update_by_id` update, including the path taken and `model_type`
while avoiding any secrets or API keys.
🪄 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: e93e7a7c-57b1-4602-bdda-d063b15a9fc4
📒 Files selected for processing (7)
api/apps/restful_apis/chat_api.pyapi/apps/restful_apis/models_api.pyapi/apps/services/models_api_service.pyapi/db/services/compilation_template_service.pyapi/db/services/user_service.pytools/scripts/mysql_migration.pytools/scripts/run_migrations.sh
| return False, f"model type '{model_type}' is invalid" | ||
| tenant_field_name = MODEL_TYPE_TO_TENANT_ID_FIELD.get(model_type) | ||
|
|
||
| e, tenant = TenantService.get_by_id(tenant_id) |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Remove the unused tenant binding.
Line 244 only checks existence; rename the unused binding to satisfy Ruff RUF059.
Proposed fix
- e, tenant = TenantService.get_by_id(tenant_id)
+ e, _tenant = TenantService.get_by_id(tenant_id)📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| e, tenant = TenantService.get_by_id(tenant_id) | |
| e, _tenant = TenantService.get_by_id(tenant_id) |
🧰 Tools
🪛 Ruff (0.15.20)
[warning] 244-244: Unpacked variable tenant is never used
Prefix it with an underscore or any other dummy variable pattern
(RUF059)
🤖 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 `@api/apps/services/models_api_service.py` at line 244, The tenant lookup in
TenantService.get_by_id is only being used for existence checking, so the unused
tenant binding should be renamed to satisfy Ruff RUF059. Update the assignment
in the models_api_service flow to keep the error value binding while discarding
the tenant result with an ignored placeholder, and make sure the surrounding
existence check logic remains unchanged.
Source: Linters/SAST tools
There was a problem hiding this comment.
🧹 Nitpick comments (1)
common/misc_utils.py (1)
255-259: 🚀 Performance & Scalability | 🔵 Trivial | ⚖️ Poor tradeoffPer-call executor adds thread create/teardown overhead on hot paths.
Creating a fresh
ThreadPoolExecutorper invocation spawns and tears down a worker thread on every call. This is a reasonable workaround for the cited Python 3.13 executor-reuse deadlock, but ifthread_pool_execsits on a high-frequency path the churn may become measurable. If profiling later shows this matters, consider a bounded pool of long-lived executors keyed to avoid the reuse-deadlock condition, rather than spawning per call.Also worth confirming the deadlock is inherent to shared-executor reuse and not a symptom of a specific await pattern, so the workaround targets the true root cause.
🤖 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 `@common/misc_utils.py` around lines 255 - 259, The hot-path overhead comes from creating a new ThreadPoolExecutor inside thread_pool_exec on every call, so reuse a bounded long-lived executor strategy instead of per-invocation spawn/teardown. Keep the Python 3.13 deadlock workaround in place by ensuring the executor used by loop.run_in_executor is not shared in the problematic way, and verify the deadlock is tied to executor reuse rather than the ctx.run/await pattern.
🤖 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.
Nitpick comments:
In `@common/misc_utils.py`:
- Around line 255-259: The hot-path overhead comes from creating a new
ThreadPoolExecutor inside thread_pool_exec on every call, so reuse a bounded
long-lived executor strategy instead of per-invocation spawn/teardown. Keep the
Python 3.13 deadlock workaround in place by ensuring the executor used by
loop.run_in_executor is not shared in the problematic way, and verify the
deadlock is tied to executor reuse rather than the ctx.run/await pattern.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 1792af6e-05ed-4fac-8eac-cdc94e5d193f
📒 Files selected for processing (3)
common/misc_utils.pytest/unit_test/api/apps/services/test_delete_datasets.pytest/unit_test/api/apps/services/test_models_api_service_list_tenant_added_models.py
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #16782 +/- ##
=======================================
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. 🚀 New features to boost your workflow:
|
Bring in main fixes (infiniflow#16765, infiniflow#16782). Resolve conflicts using resolve_model_config from main with dev user_id on LLMBundle. Fix get_model_type_by_name single-record handling. Co-authored-by: Cursor <[email protected]>
What problem does this PR solve?
This PR updates model default handling and related compatibility paths across the backend.
It adds support for using
model_idwhere the system previously requiredmodel_provider/model_instance/model_name, while keeping the legacy format working. Default model reads now also returnmodel_id, and the default-model write path stores both the legacy composite value and the tenant model id.It also includes several related fixes:
Type of change