Skip to content

fix(api): report why provider model discovery failed instead of swallowing it - #18027

Merged
JinHai-CN merged 4 commits into
infiniflow:mainfrom
olifarhaan:fix_model_discovery_error_masking
Aug 19, 2026
Merged

JinHai-CN merged 4 commits into
infiniflow:mainfrom
olifarhaan:fix_model_discovery_error_masking

Conversation

@olifarhaan

@olifarhaan olifarhaan commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

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 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_SECONDS and 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:1234 reaches the user instead.

That URL is user-typed and routinely carries secrets, as userinfo or as a query token, and _normalize_provider_base_url only 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.py covers 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 on main and all seven pass here.

python3 run_tests.py -i in a clean worktree: 2497 passed and 3 failed on main, 2504 passed and 3 failed here. The three are the same in both runs, all in test/unit_test/agent/sandbox/test_local_provider.py, which cannot pass on macOS because preexec_fn sets Pdeathsig. lefthook run pre-commit on the changed files is clean.

One case is deliberately left alone. ModelMeta.get_model_list returns [] 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.

…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.
@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. labels Aug 9, 2026
@coderabbitai

coderabbitai Bot commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 515a70d8-4280-4d98-b63d-eabe3bb99322

📥 Commits

Reviewing files that changed from the base of the PR and between f814a7e and ed7908d.

📒 Files selected for processing (1)
  • test/unit_test/api/apps/services/test_provider_api_service_verify_discovery.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • test/unit_test/api/apps/services/test_provider_api_service_verify_discovery.py

📝 Walkthrough

Walkthrough

verify_api_key now logs model discovery exceptions, redacts sensitive URL data, and reports sanitized failure details. Tests cover unreachable URLs, discovery exceptions, empty results, and redaction edge cases.

Changes

Discovery Error Reporting

Layer / File(s) Summary
Report remote discovery failures
api/apps/services/provider_api_service.py
verify_api_key records discovery errors and includes the redacted URL and scrubbed exception text in the no-models response. URL redaction removes credentials, query parameters, and fragments.
Validate discovery outcomes
test/unit_test/api/apps/services/test_provider_api_service_verify_discovery.py
Tests inject discovery behavior and verify unreachable URLs, raised exceptions, empty results, non-propagation, and URL redaction.

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
Loading

Poem

A rabbit cleaned a URL bright,
And hid each secret out of sight.
Errors now explain the way,
Empty models keep words at bay.
Discovery fails, but requests stay.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 38.89% 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 and concisely describes the main change: reporting provider model discovery failures instead of silently suppressing them.
Description check ✅ Passed The description includes a clear summary, background, implementation details, tests, known failures, and preserved behavior relevant to the requested change.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

🤖 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

📥 Commits

Reviewing files that changed from the base of the PR and between 99110c2 and 54feeac.

📒 Files selected for processing (2)
  • api/apps/services/provider_api_service.py
  • test/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.
@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 Aug 9, 2026

@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: 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

📥 Commits

Reviewing files that changed from the base of the PR and between 54feeac and 2d5ccee.

📒 Files selected for processing (2)
  • api/apps/services/provider_api_service.py
  • test/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.

@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: 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

📥 Commits

Reviewing files that changed from the base of the PR and between 2d5ccee and f814a7e.

📒 Files selected for processing (2)
  • api/apps/services/provider_api_service.py
  • test/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.
@JinHai-CN
JinHai-CN requested a review from Lynn-Inf August 19, 2026 06:47

@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

@Lynn-Inf Lynn-Inf added the ci Continue Integration label Aug 19, 2026
@JinHai-CN
JinHai-CN merged commit ece9638 into infiniflow:main Aug 19, 2026
6 checks passed
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:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants