Skip to content

fix(api): fall back to factory max_tokens for tenant models - #16364

Merged
wangq8 merged 12 commits into
infiniflow:mainfrom
Harsh23Kashyap:Harsh23Kashyap/fix/tenant-model-max-tokens-fallback
Jul 1, 2026
Merged

wangq8 merged 12 commits into
infiniflow:mainfrom
Harsh23Kashyap:Harsh23Kashyap/fix/tenant-model-max-tokens-fallback

Conversation

@Harsh23Kashyap

Copy link
Copy Markdown
Collaborator

Summary

When a tenant_model row exists but extra is empty, get_model_config_from_provider_instance defaulted max_tokens to 8192. That silently truncates retrieved knowledge in chat completions even when the factory config defines a much larger context window (e.g. 128000).

Fall back to the factory model definition when tenant_model.extra has no max_tokens.

Fixes #16362

Test plan

  • test_max_tokens_falls_back_to_factory_when_model_extra_empty
  • test_max_tokens_prefers_model_extra_over_factory

@Harsh23Kashyap

Copy link
Copy Markdown
Collaborator Author

Could a maintainer add the ci label when convenient? Thanks.

@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. 🐞 bug Something isn't working, pull request that fix bug. 🧪 test Pull requests that update test cases. labels Jun 25, 2026
@Harsh23Kashyap

Copy link
Copy Markdown
Collaborator Author

Could a maintainer add the ci label when convenient? Thanks.

@coderabbitai

coderabbitai Bot commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It 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 reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Tenant model config resolution now falls back to factory max_tokens when tenant_model.extra omits it, while still letting model-level overrides win. New unit tests cover both paths.

Changes

Tenant model max_tokens resolution

Layer / File(s) Summary
Factory lookup and max_tokens fallback
api/db/joint_services/tenant_model_service.py
A helper resolves factory LLM metadata from FACTORY_LLM_INFOS, and get_model_config_from_provider_instance now uses model extra max_tokens, then factory max_tokens, then 8192.
max_tokens unit tests
test/unit_test/api/db/joint_services/test_tenant_model_service_max_tokens.py
Unit tests stub the tenant model service dependencies and verify the factory fallback path and the model override path for max_tokens.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Possibly related PRs

  • infiniflow/ragflow#15945: Modifies the same service path to populate max_tokens from model_extra and FACTORY_LLM_INFOS.

Poem

A rabbit hopped through tokens bright,
and found the factory map in sight.
If pockets came up empty, the model still could sing,
with override carrots leading everything.
🐰 max_tokens now leaps just right.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly states the main fix: falling back to factory max_tokens for tenant models.
Description check ✅ Passed The PR includes the required Summary section and explains the bug, fix, issue link, and test plan.
Linked Issues check ✅ Passed The code change matches #16362 by using factory max_tokens when tenant_model.extra lacks it and preserving tenant overrides.
Out of Scope Changes check ✅ Passed The changes stay focused on the max_tokens fallback and matching tests, with no obvious unrelated additions.

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.

@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 (2)
test/unit_test/api/db/joint_services/test_tenant_model_service_max_tokens.py (1)

25-123: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add a regression test for explicit-but-falsy max_tokens in model.extra.

Current tests won’t catch the fallback bug when max_tokens is present but falsy. Add one case to verify key-presence precedence behavior explicitly.

🤖 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 `@test/unit_test/api/db/joint_services/test_tenant_model_service_max_tokens.py`
around lines 25 - 123, Add a regression test around
get_model_config_from_provider_instance to cover explicit-but-falsy max_tokens
in model.extra. The current TenantModelService tests only validate empty extra
and a truthy override, so add a case where model.extra contains max_tokens with
a falsy value (such as 0 or null) and assert the config preserves that explicit
value instead of falling back to settings.FACTORY_LLM_INFOS; keep the setup
aligned with the existing provider/instance/model monkeypatch pattern in
test_max_tokens_falls_back_to_factory_when_model_extra_empty and
test_max_tokens_prefers_model_extra_over_factory.
api/db/joint_services/tenant_model_service.py (1)

37-47: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add observability for the new factory/max_tokens fallback flow.

This new path silently falls back when factory/model values are absent; add debug/warn logs around lookup miss and fallback selection to make production diagnosis possible.

As per coding guidelines, **/*.py: “Add logging for new flows”.

Also applies to: 234-243

🤖 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/db/joint_services/tenant_model_service.py` around lines 37 - 47, The new
fallback path in _lookup_factory_llm_info currently returns None or selects a
fallback silently, so add observability for both the lookup miss and the
fallback choice. Update this helper to emit debug/warn logs when
provider_name/pure_model_name cannot be matched in settings.FACTORY_LLM_INFOS
and when the special siliconflow intl target_factory_name is selected, so
production issues can be diagnosed from logs.

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/db/joint_services/tenant_model_service.py`:
- Line 235: The max_tokens fallback logic in tenant_model_service should
preserve explicitly configured falsy values instead of treating them as missing.
Update the max_tokens assignment to check key presence on model_extra and
llm_info rather than using truthy or chaining, so an explicit value is used
whenever the key exists. Use the max_tokens handling in the tenant_model_service
flow to locate and adjust this logic.

---

Nitpick comments:
In `@api/db/joint_services/tenant_model_service.py`:
- Around line 37-47: The new fallback path in _lookup_factory_llm_info currently
returns None or selects a fallback silently, so add observability for both the
lookup miss and the fallback choice. Update this helper to emit debug/warn logs
when provider_name/pure_model_name cannot be matched in
settings.FACTORY_LLM_INFOS and when the special siliconflow intl
target_factory_name is selected, so production issues can be diagnosed from
logs.

In
`@test/unit_test/api/db/joint_services/test_tenant_model_service_max_tokens.py`:
- Around line 25-123: Add a regression test around
get_model_config_from_provider_instance to cover explicit-but-falsy max_tokens
in model.extra. The current TenantModelService tests only validate empty extra
and a truthy override, so add a case where model.extra contains max_tokens with
a falsy value (such as 0 or null) and assert the config preserves that explicit
value instead of falling back to settings.FACTORY_LLM_INFOS; keep the setup
aligned with the existing provider/instance/model monkeypatch pattern in
test_max_tokens_falls_back_to_factory_when_model_extra_empty and
test_max_tokens_prefers_model_extra_over_factory.
🪄 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: f7fbe974-3298-47ed-b0db-78c4d9a76dbc

📥 Commits

Reviewing files that changed from the base of the PR and between e8bb534 and a404f4c.

📒 Files selected for processing (2)
  • api/db/joint_services/tenant_model_service.py
  • test/unit_test/api/db/joint_services/test_tenant_model_service_max_tokens.py

@Harsh23Kashyap

Copy link
Copy Markdown
Collaborator Author

Hi maintainers, this branch is current and CodeRabbit is passing. Could you add the ci label when convenient so the full workflows can run?

1 similar comment
@Harsh23Kashyap

Copy link
Copy Markdown
Collaborator Author

Hi maintainers, this branch is current and CodeRabbit is passing. Could you add the ci label when convenient so the full workflows can run?

@yuzhichang
yuzhichang force-pushed the Harsh23Kashyap/fix/tenant-model-max-tokens-fallback branch from 397a0ea to b5e59cf Compare June 29, 2026 06:10
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Jun 29, 2026
@yuzhichang
yuzhichang force-pushed the Harsh23Kashyap/fix/tenant-model-max-tokens-fallback branch from b5e59cf to 132300d Compare June 29, 2026 06:11
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Jun 29, 2026
@Harsh23Kashyap

Copy link
Copy Markdown
Collaborator Author

Synced with latest main. Could a maintainer add the ci label so workflows can run? Okay, please review it.

@Harsh23Kashyap
Harsh23Kashyap force-pushed the Harsh23Kashyap/fix/tenant-model-max-tokens-fallback branch from 2e73788 to 4e8e20e Compare June 30, 2026 04:48
@Harsh23Kashyap
Harsh23Kashyap force-pushed the Harsh23Kashyap/fix/tenant-model-max-tokens-fallback branch from a11945c to 390e871 Compare June 30, 2026 13:43
@Harsh23Kashyap

Copy link
Copy Markdown
Collaborator Author

Follow-up: tried syncing with latest main via gh pr update-branch, but the branch has merge conflicts (behind_by: 7, merge state DIRTY). I can resolve and push if you want this current before review. CodeRabbit passed; no unresolved review threads. Fork workflows stay skipped until a maintainer adds the ci label.

@wangq8 wangq8 added the ci Continue Integration label Jul 1, 2026
@wangq8
wangq8 requested a review from Lynn-Inf July 1, 2026 06:16

@Lynn-Inf Lynn-Inf 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.

LGTM

@codecov

codecov Bot commented Jul 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.16%. Comparing base (b8e960e) to head (fa46b8c).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #16364   +/-   ##
=======================================
  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.

@wangq8
wangq8 merged commit d770217 into infiniflow:main Jul 1, 2026
2 of 3 checks passed
@Harsh23Kashyap
Harsh23Kashyap deleted the Harsh23Kashyap/fix/tenant-model-max-tokens-fallback branch July 1, 2026 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration size:S This PR changes 10-29 lines, ignoring generated files. 🧪 test Pull requests that update test cases.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tenant_model.extra missing max_tokens causes knowledge truncation to 8192 in chat completions

3 participants