diff --git a/fastdeploy/engine/request.py b/fastdeploy/engine/request.py index 391e2038534..b544ad2989c 100644 --- a/fastdeploy/engine/request.py +++ b/fastdeploy/engine/request.py @@ -19,7 +19,7 @@ import json import time import traceback -from dataclasses import asdict, dataclass, fields +from dataclasses import asdict, dataclass, fields, is_dataclass from enum import Enum from typing import Any, Dict, Generic, Optional from typing import TypeVar as TypingTypeVar @@ -896,7 +896,23 @@ def to_dict(self): """ Convert the RequestMetrics object to a dictionary. """ - return {k: v for k, v in asdict(self).items()} + res = {} + for k in self.__dataclass_fields__: + v = getattr(self, k) + if type(v) in (int, float, str, bool, type(None)): + res[k] = v + elif type(v) is list: + res[k] = list(v) + elif type(v) is dict: + res[k] = dict(v) + elif is_dataclass(v): + if hasattr(v, "to_dict"): + res[k] = v.to_dict() + else: + res[k] = asdict(v) + else: + res[k] = v + return res def record_recv_first_token(self): cur_time = time.time() diff --git a/fastdeploy/worker/output.py b/fastdeploy/worker/output.py index 365fec12475..2174fb24bf4 100644 --- a/fastdeploy/worker/output.py +++ b/fastdeploy/worker/output.py @@ -164,6 +164,20 @@ class SpeculateMetrics: """ accept_ratio_per_head: list[float] + def to_dict(self): + res = {} + for k in self.__dataclass_fields__: + v = getattr(self, k) + if type(v) in (int, float, str, bool, type(None)): + res[k] = v + elif type(v) is list: + res[k] = list(v) + elif type(v) is dict: + res[k] = dict(v) + else: + res[k] = v + return res + @dataclass class SamplerOutput: