Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix: guard Dashscope response attribute access in token/log utils (#12082)

Yongteng Lei5d391fb1f93f
diff --git a/common/log_utils.py b/common/log_utils.py
index abbcd286b4..7a5335aeae 100644
--- a/common/log_utils.py
+++ b/common/log_utils.py
@@ -75,9 +75,12 @@ def init_root_logger(logfile_basename: str, log_format: str = "%(asctime)-15s %(
def log_exception(e, *args):
logging.exception(e)
for a in args:
- if hasattr(a, "text"):
- logging.error(a.text)
- raise Exception(a.text)
- else:
- logging.error(str(a))
+ try:
+ text = getattr(a, "text")
+ except Exception:
+ text = None
+ if text is not None:
+ logging.error(text)
+ raise Exception(text)
+ logging.error(str(a))
raise e
diff --git a/common/token_utils.py b/common/token_utils.py
index 5763dc97e9..981e98a1b5 100644
--- a/common/token_utils.py
+++ b/common/token_utils.py
@@ -44,23 +44,23 @@ def total_token_count_from_response(resp):
if resp is None:
return 0
- if hasattr(resp, "usage") and hasattr(resp.usage, "total_tokens"):
- try:
+ try:
+ if hasattr(resp, "usage") and hasattr(resp.usage, "total_tokens"):
return resp.usage.total_tokens
- except Exception:
- pass
+ except Exception:
+ pass
- if hasattr(resp, "usage_metadata") and hasattr(resp.usage_metadata, "total_tokens"):
- try:
+ try:
+ if hasattr(resp, "usage_metadata") and hasattr(resp.usage_metadata, "total_tokens"):
return resp.usage_metadata.total_tokens
- except Exception:
- pass
+ except Exception:
+ pass
- if hasattr(resp, "meta") and hasattr(resp.meta, "billed_units") and hasattr(resp.meta.billed_units, "input_tokens"):
- try:
- return resp.meta.billed_units.input_tokens
- except Exception:
- pass
+ try:
+ if hasattr(resp, "meta") and hasattr(resp.meta, "billed_units") and hasattr(resp.meta.billed_units, "input_tokens"):
+ return resp.meta.billed_units.input_tokens
+ except Exception:
+ pass
if isinstance(resp, dict) and 'usage' in resp and 'total_tokens' in resp['usage']:
try:
@@ -85,4 +85,3 @@ def total_token_count_from_response(resp):
def truncate(string: str, max_len: int) -> str:
"""Returns truncated text if the length of text exceed max_len."""
return encoder.decode(encoder.encode(string)[:max_len])
-