Summary
Credential validation can raise UnboundLocalError when a network exception occurs before response is assigned.
Problem
In dify_plugin/interfaces/model/openai_compatible/llm.py, the validate_credentials method wraps the HTTP call in a broad except Exception and constructs an error message that references response.text. If requests.post(...) raises before response is assigned (connection error, timeout, DNS failure), the handler itself throws UnboundLocalError: cannot access local variable 'response', masking the real failure.
Expected Behavior
Credential validation should surface the underlying request error without throwing a secondary UnboundLocalError.
Actual Behavior
Validation fails with UnboundLocalError in the error handler, hiding the original root cause.
Steps to Reproduce
- Configure a model provider credential with an invalid or unreachable
endpoint_url, or just with an turned-off Wifi.
- A credential validation will be triggered on save.
- Observe
UnboundLocalError instead of the original request exception.
Impact
- Masks the real failure cause in validation.
- Prevents correct error reporting to users.
Proposed Fix
Initialize response = None before the request and only read response.text when response is assigned. Keep the error message focused on the root exception.
Summary
Credential validation can raise
UnboundLocalErrorwhen a network exception occurs beforeresponseis assigned.Problem
In
dify_plugin/interfaces/model/openai_compatible/llm.py, thevalidate_credentialsmethod wraps the HTTP call in a broadexcept Exceptionand constructs an error message that referencesresponse.text. Ifrequests.post(...)raises beforeresponseis assigned (connection error, timeout, DNS failure), the handler itself throwsUnboundLocalError: cannot access local variable 'response', masking the real failure.Expected Behavior
Credential validation should surface the underlying request error without throwing a secondary
UnboundLocalError.Actual Behavior
Validation fails with
UnboundLocalErrorin the error handler, hiding the original root cause.Steps to Reproduce
endpoint_url, or just with an turned-off Wifi.UnboundLocalErrorinstead of the original request exception.Impact
Proposed Fix
Initialize
response = Nonebefore the request and only readresponse.textwhenresponseis assigned. Keep the error message focused on the root exception.