Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

refactor: replace bare dict with typed annotations in llm_generator and prompt (#35100)

dataCenter430974d2f16274a
diff --git a/api/core/llm_generator/llm_generator.py b/api/core/llm_generator/llm_generator.py
index aa258c9f898..c43c0274cdd 100644
--- a/api/core/llm_generator/llm_generator.py
+++ b/api/core/llm_generator/llm_generator.py
@@ -2,7 +2,7 @@ import json
import logging
import re
from collections.abc import Sequence
-from typing import Protocol, TypedDict, cast
+from typing import Any, Protocol, TypedDict, cast
import json_repair
from graphon.enums import WorkflowNodeExecutionMetadataKey
@@ -533,7 +533,7 @@ class LLMGenerator:
def __instruction_modify_common(
tenant_id: str,
model_config: ModelConfig,
- last_run: dict | None,
+ last_run: dict[str, Any] | None,
current: str | None,
error_message: str | None,
instruction: str,
diff --git a/api/core/llm_generator/output_parser/structured_output.py b/api/core/llm_generator/output_parser/structured_output.py
index 9bdca1e83b3..a8ad7c91795 100644
--- a/api/core/llm_generator/output_parser/structured_output.py
+++ b/api/core/llm_generator/output_parser/structured_output.py
@@ -202,7 +202,7 @@ def _handle_native_json_schema(
structured_output_schema: Mapping,
model_parameters: dict[str, Any],
rules: list[ParameterRule],
-):
+) -> dict[str, Any]:
"""
Handle structured output for models with native JSON schema support.
@@ -224,7 +224,7 @@ def _handle_native_json_schema(
return model_parameters
-def _set_response_format(model_parameters: dict[str, Any], rules: list[ParameterRule]):
+def _set_response_format(model_parameters: dict[str, Any], rules: list[ParameterRule]) -> None:
"""
Set the appropriate response format parameter based on model rules.
@@ -326,7 +326,7 @@ def _prepare_schema_for_model(provider: str, model_schema: AIModelEntity, schema
return {"schema": processed_schema, "name": "llm_response"}
-def remove_additional_properties(schema: dict[str, Any]):
+def remove_additional_properties(schema: dict[str, Any]) -> None:
"""
Remove additionalProperties fields from JSON schema.
Used for models like Gemini that don't support this property.
@@ -349,7 +349,7 @@ def remove_additional_properties(schema: dict[str, Any]):
remove_additional_properties(item)
-def convert_boolean_to_string(schema: dict):
+def convert_boolean_to_string(schema: dict[str, Any]) -> None:
"""
Convert boolean type specifications to string in JSON schema.
diff --git a/api/core/prompt/simple_prompt_transform.py b/api/core/prompt/simple_prompt_transform.py
index d4e17613a25..dc8391a6a56 100644
--- a/api/core/prompt/simple_prompt_transform.py
+++ b/api/core/prompt/simple_prompt_transform.py
@@ -313,7 +313,7 @@ class SimplePromptTransform(PromptTransform):
return prompt_message
- def _get_prompt_rule(self, app_mode: AppMode, provider: str, model: str):
+ def _get_prompt_rule(self, app_mode: AppMode, provider: str, model: str) -> dict[str, Any]:
"""
Get simple prompt rule.
:param app_mode: app mode
@@ -325,7 +325,7 @@ class SimplePromptTransform(PromptTransform):
# Check if the prompt file is already loaded
if prompt_file_name in prompt_file_contents:
- return cast(dict, prompt_file_contents[prompt_file_name])
+ return cast(dict[str, Any], prompt_file_contents[prompt_file_name])
# Get the absolute path of the subdirectory
prompt_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "prompt_templates")
@@ -338,7 +338,7 @@ class SimplePromptTransform(PromptTransform):
# Store the content of the prompt file
prompt_file_contents[prompt_file_name] = content
- return cast(dict, content)
+ return cast(dict[str, Any], content)
def _prompt_file_name(self, app_mode: AppMode, provider: str, model: str) -> str:
# baichuan