Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions python/dify_plugin/interfaces/model/openai_compatible/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ def validate_credentials(self, model: str, credentials: dict) -> None:
endpoint_url += "/"

# prepare the payload for a simple ping to the model
data = {"model": credentials.get("endpoint_model_name", model), "max_tokens": 5}
validate_credentials_max_tokens = credentials.get("validate_credentials_max_tokens", 5) or 5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The use of or 5 can lead to unexpected behavior. If a user provides validate_credentials_max_tokens: 0, this expression will evaluate to 5, which is likely not the intended behavior as 0 can be a valid value for some APIs.

A more robust way to handle this is to explicitly check for None before applying the default. This can be done concisely using an assignment expression (walrus operator), which is supported by your target Python version (>=3.11).

Suggested change
validate_credentials_max_tokens = credentials.get("validate_credentials_max_tokens", 5) or 5
validate_credentials_max_tokens = v if (v := credentials.get("validate_credentials_max_tokens")) is not None else 5

data = {
"model": credentials.get("endpoint_model_name", model),
"max_tokens": validate_credentials_max_tokens,
}

completion_type = LLMMode.value_of(credentials["mode"])

Expand All @@ -201,8 +205,11 @@ def validate_credentials(self, model: str, credentials: dict) -> None:
# ADD stream validate_credentials
stream_mode_auth = credentials.get("stream_mode_auth", "not_use")
if stream_mode_auth == "use":
stream_validate_max_tokens = credentials.get("validate_credentials_max_tokens") or 10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This line has the same issue as in the non-streaming case. Using or 10 will incorrectly override an explicit setting of validate_credentials_max_tokens: 0 to 10.

To handle this correctly while keeping it concise, you can use an assignment expression to check for None before applying the default value.

Suggested change
stream_validate_max_tokens = credentials.get("validate_credentials_max_tokens") or 10
stream_validate_max_tokens = v if (v := credentials.get("validate_credentials_max_tokens")) is not None else 10

data["stream"] = True
data["max_tokens"] = 10
# default 10 (introduced in PR #93) to ensure streaming endpoints emit a token chunk;
# allow overriding via credentials when explicitly provided.
data["max_tokens"] = stream_validate_max_tokens
response = requests.post(endpoint_url, headers=headers, json=data, timeout=(10, 300), stream=True)
if response.status_code != 200:
raise CredentialsValidateFailedError(
Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "dify_plugin"
version = "0.7.0"
version = "0.7.1"
description = "Dify Plugin SDK"
authors = [{ name = "langgenius", email = "hello@dify.ai" }]
dependencies = [
Expand Down