Repository navigation
fix(api): report why provider model discovery failed instead of swallowing it - #18027
Conversation
…owing it Providers whose static catalogue is empty discover their models by calling the base URL the user typed. `verify_api_key` wrapped that call in a bare `except Exception: pass` and then returned a flat "No models found for provider 'X'", so a host that is unreachable from inside the container, a closed port or a bad TLS setup all surfaced as the same message. It reads as a credential or catalogue problem, which is why these reports arrive as "my API key is correct but RAGFlow says no models found" and get chased through key and timeout settings that were never involved. Keep the failure non-fatal, but log it and append the reason and the URL that was probed to the returned message.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthrough
ChangesDiscovery Error Reporting
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant verify_api_key
participant ModelDiscovery
participant Logger
verify_api_key->>ModelDiscovery: discover models
ModelDiscovery-->>verify_api_key: return models or exception
verify_api_key->>verify_api_key: redact URL and scrub exception
verify_api_key->>Logger: log sanitized discovery failure
verify_api_key-->>verify_api_key: return verification result
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: 1
🤖 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/services/provider_api_service.py`:
- Around line 690-691: Update the discovery failure handling around
_normalize_provider_base_url to sanitize model_base_url before using it in
logging.exception and discovery_error. Remove userinfo, query parameters, and
fragments while preserving the safe URL base, and use only this redacted value
for both log and error messages.
🪄 Autofix
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 Plus
Run ID: 90302a91-86d1-483f-ac2b-1e06a79132cd
📒 Files selected for processing (2)
api/apps/services/provider_api_service.pytest/unit_test/api/apps/services/test_provider_api_service_verify_discovery.py
Surfacing the discovery error meant echoing a user-supplied base URL into the application log and into the API response. Provider base URLs routinely carry secrets, as userinfo (https://user:token@host/v1) or as a query token (https://host/v1?api-key=...), and _normalize_provider_base_url only appends /v1, so nothing stripped them. Report the URL through _redact_url, which keeps the scheme, host, port and path and drops userinfo, query and fragment. aiohttp echoes the URL it was handed, so scrub the raw form out of the exception text as well.
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 `@api/apps/services/provider_api_service.py`:
- Around line 715-720: Replace the raw logging.exception call in the provider
discovery error path with a sanitized error record that logs only safe
diagnostic fields and never attaches the exception or traceback; sanitize the
exception reason before constructing discovery_error. In
test/unit_test/api/apps/services/test_provider_api_service_verify_discovery.py
lines 133-148, capture logs with caplog and assert that both userinfo
credentials and query-token values are absent.
- Around line 67-76: Update the URL sanitization logic around urlparse in
api/apps/services/provider_api_service.py (lines 67-76) to return "<unparsable
url>" for missing authorities and catch invalid-port ValueError before accessing
parsed.port; never return raw input. Add coverage in
test/unit_test/api/apps/services/test_provider_api_service_verify_discovery.py
(lines 151-163) for an authority-less URL containing a query token and a URL
with an invalid port.
🪄 Autofix
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 Plus
Run ID: 0fde949d-452f-4f01-927d-5db660b473f8
📒 Files selected for processing (2)
api/apps/services/provider_api_service.pytest/unit_test/api/apps/services/test_provider_api_service_verify_discovery.py
Three holes in the redaction added by the previous commit, each verified against the interpreter rather than reasoned about. `urlparse` accepts a URL with no authority, which is what a base URL typed without a scheme parses as, and `_redact_url` returned that input verbatim, query token included. `urlparse` also defers port validation to attribute access, so `https://user:tok@host:notaport/v1` raised ValueError out of the `except` block that was meant to report the failure. Both now yield `<unparsable url>`, since neither input is safe to echo. `logging.exception` writes the raw exception and its traceback, and clients echo the URL they were handed, so credentials reached the application log even though the caller-visible message was clean. Log the scrubbed reason without the traceback instead, and scrub userinfo, query and fragment out of provider text rather than only replacing the exact URL string.
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 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
`@test/unit_test/api/apps/services/test_provider_api_service_verify_discovery.py`:
- Line 139: Add narrow Ruff S105 suppressions to the synthetic credential
fixture assignments at the referenced locations, including a brief comment that
the values are synthetic test credentials. Preserve the existing redaction cases
and values unchanged.
- Around line 156-169: The test_authority_less_base_url_is_not_echoed_back test
should capture logging via caplog and assert that the complete captured log text
does not contain AKIA-SECRET, while preserving the existing message assertions
for the returned verification result.
- Around line 201-206: Remove _load_helper and its dynamic source-reading,
compilation, and exec logic. Update the tests to reuse _load_service and access
the loaded module’s _redact_url directly, ensuring they exercise the actual
service implementation rather than reconstructed source.
🪄 Autofix
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 Plus
Run ID: 5ecdcbb6-0032-4774-8473-b98aa0d40f12
📒 Files selected for processing (2)
api/apps/services/provider_api_service.pytest/unit_test/api/apps/services/test_provider_api_service_verify_discovery.py
🚧 Files skipped from review as they are similar to previous changes (1)
- api/apps/services/provider_api_service.py
…he exec helper Reach _redact_url through the loaded service module instead of recompiling an extracted copy, so the test exercises the function that actually ships, and assert the authority-less case keeps its query token out of the log as well as out of the response.
Summary
Providers whose static catalogue is empty discover their models by calling the base URL the user typed.
verify_api_keywrapped that call in a bareexcept Exception: passand then returned a flatNo models found for provider 'X', so an unreachable host, a closed port, a wrong scheme and a bad TLS setup all produced the same sentence, with the actual error discarded and not even logged.That message reads as a credential or catalogue problem, which is why these reports keep arriving as "my API key is definitely correct but RAGFlow says no models found" and then get chased through
LLM_TIMEOUT_SECONDSand key formats that were never involved. #17757 and #17103 are both this shape, and the usual real cause, a base URL that resolves inside the container differently than on the host, is exactly what the swallowed exception said.Discovery stays non-fatal. The failure is now logged with the provider and URL, and the reason plus the probed URL are appended to the returned message, so
No models found for provider 'LM-Studio'. Discovery against http://127.0.0.1:1234/v1 failed: Cannot connect to host 127.0.0.1:1234reaches the user instead.That URL is user-typed and routinely carries secrets, as userinfo or as a query token, and
_normalize_provider_base_urlonly appends/v1, so it is reported through_redact_url, which keeps the scheme, host, port and path and drops the rest. A URL with no authority, which is how a base URL typed without a scheme parses, and one whose port fails to validate on access both become<unparsable url>rather than being echoed. Clients echo back the URL they were handed, so userinfo, query and fragment are scrubbed out of the exception text too, and the failure is logged without the raw exception or its traceback.test/unit_test/api/apps/services/test_provider_api_service_verify_discovery.pycovers a raising probe, a probe that simply finds nothing, the guarantee that a failure still returns a normal result rather than propagating, and that credentials in the base URL reach neither the response nor the log, including the authority-less and invalid-port shapes. Two of its seven cases fail onmainand all seven pass here.python3 run_tests.py -iin a clean worktree: 2497 passed and 3 failed onmain, 2504 passed and 3 failed here. The three are the same in both runs, all intest/unit_test/agent/sandbox/test_local_provider.py, which cannot pass on macOS becausepreexec_fnsetsPdeathsig.lefthook run pre-commiton the changed files is clean.One case is deliberately left alone.
ModelMeta.get_model_listreturns[]on a non-200, so a provider whose discovery endpoint does not exist on the user's server, such as pointing a vLLM instance at the LocalAI provider and getting a 404 from/api/tags, still reports nothing found without the status code. That contract is pinned by an existing test,test_get_model_list_tags_endpoint_non_200_returns_empty, so changing it is a separate decision rather than something to slip into this fix.