Repository navigation
fix(llm): strip non-generation keys from gen_conf for LiteLLM providers (#15427) - #15432
Conversation
…rs (infiniflow#15427) A chat assistant's llm_setting is forwarded to the model as gen_conf. llm_setting can carry RAGFlow-internal metadata such as model_type (the chat REST APIs read it back out), so that key ends up in gen_conf. Base._clean_conf already whitelisted the keys it forwards, so OpenAI- compatible providers were unaffected. LiteLLMBase._clean_conf only dropped max_tokens and passed everything else through to litellm.acompletion, which forwarded model_type to the upstream provider. Anthropic rejected it with "model_type: Extra inputs are not permitted" and OpenAI with "Unknown parameter: 'model_type'", breaking all LiteLLM-routed chats (regression from v0.25.4). Extract the allowed-key set into a shared ALLOWED_GEN_CONF_KEYS constant, reuse it in Base._clean_conf, and apply the same whitelist in LiteLLMBase._clean_conf (plus LiteLLM reasoning params such as thinking / reasoning_effort that the model-family policies inject). Adds unit tests covering both backends.
|
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 (1)
📝 WalkthroughWalkthroughCentralizes generation-parameter allowlists in ChangesGeneration Config Sanitization
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes 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.
🧹 Nitpick comments (1)
test/unit_test/rag/llm/test_clean_conf_whitelist.py (1)
69-72: 💤 Low valueConsider adding pytest priority markers for test categorization.
Per coding guidelines, backend tests should use priority markers. These regression tests for a critical bug fix would benefit from
@pytest.mark.p1or@pytest.mark.p2markers to ensure they run in priority test suites.Example marker application
+@pytest.mark.p1 def test_litellm_drops_model_type(): cleaned = _make_litellm()._clean_conf({"temperature": 0.5, "model_type": "chat"})As per coding guidelines:
test/**/*.py: Use pytest with priority markers (p1/p2/p3) for Python backend tests.🤖 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/rag/llm/test_clean_conf_whitelist.py` around lines 69 - 72, The test function test_litellm_drops_model_type should be marked with the appropriate pytest priority marker per backend guidelines; add a decorator like `@pytest.mark.p1` (or `@pytest.mark.p2` as appropriate) above the function and ensure pytest is imported in the test module (e.g., add "import pytest" if missing) so the test runs in the prioritized test suite and remains unchanged otherwise; locate the test by the function name test_litellm_drops_model_type in the test file and apply the marker decorator directly above it.
🤖 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 `@test/unit_test/rag/llm/test_clean_conf_whitelist.py`:
- Around line 69-72: The test function test_litellm_drops_model_type should be
marked with the appropriate pytest priority marker per backend guidelines; add a
decorator like `@pytest.mark.p1` (or `@pytest.mark.p2` as appropriate) above the
function and ensure pytest is imported in the test module (e.g., add "import
pytest" if missing) so the test runs in the prioritized test suite and remains
unchanged otherwise; locate the test by the function name
test_litellm_drops_model_type in the test file and apply the marker decorator
directly above it.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 70759101-018f-4845-8135-a99147fadd34
📒 Files selected for processing (2)
rag/llm/chat_model.pytest/unit_test/rag/llm/test_clean_conf_whitelist.py
|
@yingfeng , I'd really appreciate it if you could take a look and review this when you have some time. Thanks! |
|
@buua436 , Could you take a look at this one as well? |
|
@nickmopen please fix the UT. |
rag/graphrag/conftest.py mocks heavy deps at import time by mutating the
global sys.modules. It guarded module *creation* (`if name not in
sys.modules`) but then unconditionally assigned
`sys.modules["rag.llm.chat_model"].Base = object`.
rag/graphrag/ is collected before rag/llm/, and earlier test packages
(e.g. api/db/services) already import the real chat_model. The
unconditional assignment overwrote the genuine `Base` class with
`object`, and that corruption leaked into the rag/llm unit tests added in
this PR, which import the real class:
AttributeError: '_ConcreteBase' object has no attribute '_clean_conf'
(LiteLLMBase was untouched, so only the Base-path tests failed.)
Only stub `Base` when chat_model was actually mocked. graphrag uses it
purely as a type alias (`from rag.llm.chat_model import Base as
CompletionLLM`), so the real class is equally fine when already loaded.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15432 +/- ##
==========================================
- 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:
|
What problem does this PR solve?
Fixes #15427.
All LiteLLM-routed chats fail with:
litellm.BadRequestError: AnthropicException - {"type":"invalid_request_error","message":"model_type: Extra inputs are not permitted"}litellm.BadRequestError: OpenAIException - Unknown parameter: 'model_type'This is a regression from v0.25.4.
Root cause
A chat assistant's
llm_settingis forwarded to the model asgen_conf.llm_settingcan legitimately carry RAGFlow-internal metadata such asmodel_type(the chat REST APIs inapi/apps/restful_apis/read it back out ofllm_setting), so that key ends up insidegen_conf.Base._clean_conf(OpenAI-compatible providers) already whitelists the keys it forwards, so direct-OpenAI providers were unaffected.LiteLLMBase._clean_confonly droppedmax_tokensand passed everything else straight through tolitellm.acompletion, which forwardedmodel_typeto the upstream provider — and Anthropic / OpenAI reject it. Because both Claude and GPT route through LiteLLM, every chat broke.Fix
ALLOWED_GEN_CONF_KEYSconstant and reuse it inBase._clean_conf.LiteLLMBase._clean_conf, plus the LiteLLM-specific reasoning params (thinking,reasoning_effort,extra_body) that the model-family policies inject for reasoning models.This covers all four LiteLLM completion paths (
async_chat,async_chat_streamly,async_chat_with_tools,async_chat_streamly_with_tools), since they all route through_clean_conf.Tests
Adds
test/unit_test/rag/llm/test_clean_conf_whitelist.pycovering both backends:model_type(and other stray keys) are dropped, genuine generation params andthinkingsurvive,max_tokensis removed, and the whitelist invariants hold.Type of change