diff --git a/fastdeploy/engine/request.py b/fastdeploy/engine/request.py index 391e2038534..7e67d007b01 100644 --- a/fastdeploy/engine/request.py +++ b/fastdeploy/engine/request.py @@ -16,6 +16,7 @@ from __future__ import annotations +import dataclasses import json import time import traceback @@ -896,7 +897,27 @@ def to_dict(self): """ Convert the RequestMetrics object to a dictionary. """ - return {k: v for k, v in asdict(self).items()} + d = {} + for k in self.__dataclass_fields__: + v = getattr(self, k) + if v is None or type(v) in (int, float, str, bool): + d[k] = v + elif dataclasses.is_dataclass(v): + if hasattr(v, "to_dict"): + d[k] = v.to_dict() + else: + d[k] = asdict(v) + elif isinstance(v, list): + # Note: This is a shallow copy for performance. If RequestMetrics + # ever contains lists of nested dataclasses, they must be manually + # serialized here or fallback to asdict. + d[k] = list(v) + elif isinstance(v, dict): + # Note: This is a shallow copy for performance. + d[k] = dict(v) + else: + d[k] = v + return d def record_recv_first_token(self): cur_time = time.time() diff --git a/fastdeploy/worker/output.py b/fastdeploy/worker/output.py index 365fec12475..84095398902 100644 --- a/fastdeploy/worker/output.py +++ b/fastdeploy/worker/output.py @@ -164,6 +164,16 @@ class SpeculateMetrics: """ accept_ratio_per_head: list[float] + def to_dict(self): + return { + "accepted_tokens": self.accepted_tokens, + "rejected_tokens": self.rejected_tokens, + "accept_ratio": self.accept_ratio, + "average_accept_length": self.average_accept_length, + "accepted_tokens_per_head": self.accepted_tokens_per_head, + "accept_ratio_per_head": self.accept_ratio_per_head, + } + @dataclass class SamplerOutput: