Skip to content

UnboundLocalError in OpenAI-compatible validate_credentials #287

@Blackoutta

Description

@Blackoutta

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.

Image

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

  1. Configure a model provider credential with an invalid or unreachable endpoint_url, or just with an turned-off Wifi.
  2. A credential validation will be triggered on save.
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions