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(api): type log identity dict with IdentityDict TypedDict (#34485)

YBoye178451d04e6
diff --git a/api/core/logging/structured_formatter.py b/api/core/logging/structured_formatter.py
index 4295d2dd349..9baf6c4682b 100644
--- a/api/core/logging/structured_formatter.py
+++ b/api/core/logging/structured_formatter.py
@@ -3,13 +3,19 @@
import logging
import traceback
from datetime import UTC, datetime
-from typing import Any
+from typing import Any, TypedDict
import orjson
from configs import dify_config
+class IdentityDict(TypedDict, total=False):
+ tenant_id: str
+ user_id: str
+ user_type: str
+
+
class StructuredJSONFormatter(logging.Formatter):
"""
JSON log formatter following the specified schema:
@@ -84,7 +90,7 @@ class StructuredJSONFormatter(logging.Formatter):
return log_dict
- def _extract_identity(self, record: logging.LogRecord) -> dict[str, str] | None:
+ def _extract_identity(self, record: logging.LogRecord) -> IdentityDict | None:
tenant_id = getattr(record, "tenant_id", None)
user_id = getattr(record, "user_id", None)
user_type = getattr(record, "user_type", None)
@@ -92,7 +98,7 @@ class StructuredJSONFormatter(logging.Formatter):
if not any([tenant_id, user_id, user_type]):
return None
- identity: dict[str, str] = {}
+ identity: IdentityDict = {}
if tenant_id:
identity["tenant_id"] = tenant_id
if user_id: