快速结论:该报错在 LiteLLM 使用 routing_strategy: latency-based-routing 且配合 Redis/Valkey 缓存时,对非聊天请求(embeddings、speech、image 等)产生,原因是 lowest_latency.py 中 timedelta 到秒的转换只覆盖了聊天响应分支。优先检查是否使用了非聊天模型组,并确认 lowest_latency.py 中是否存在该转换漏洞。
适用环境:LiteLLM(v1.92.0 及 v1.83.14-stable 经确认;litellm_oss_daily_2026_07_13 版本仍存在),路由策略为 latency-based-routing,缓存使用 Redis 或 Valkey。
最快修复方案:暂无确认的一步修复方案。可手动修改 litellm/router_strategy/lowest_latency.py,将 isinstance(response_ms, timedelta) → response_ms.total_seconds() 的转换逻辑从 if isinstance(response_obj, ModelResponse): 分支内提前到该分支之前,确保 final_value 始终为 float。
注意事项:此修改会改变延迟数据的单位(从原始 timedelta 改为秒),但原本的聊天路径已做相同转换,因此行为一致。修改后需重启 LiteLLM 实例;未合并到官方版本前,每次升级需重新应用补丁。
问题场景
在 LiteLLM 代理中配置了 routing_strategy: latency-based-routing 并启用 Redis 缓存的场景下,发送 非聊天请求(例如 embeddings、speech、image)后,后台日志频繁出现以下报错,且该模型组的延迟数据无法同步到 Redis,导致跨副本的延迟路由失效。
报错原文
LiteLLM Router Caching: Excepton occured in async_log_success_event(): Object of type timedelta is not JSON serializable
Traceback (most recent call last):
File "/usr/lib/python3.13/site-packages/litellm/router_strategy/lowest_latency.py", ...
File "/usr/lib/python3.13/site-packages/litellm/caching/redis_cache.py", line ..., in async_set_cache
json.dumps(value)
TypeError: Object of type timedelta is not JSON serializable
原因分析
根本原因在于 litellm/router_strategy/lowest_latency.py 中的 async_log_success_event(及同步版本)函数。代码中 final_value 的 timedelta → seconds 转换仅放在 if isinstance(response_obj, ModelResponse): 分支内部,而嵌入、语音、图像等非聊天响应的类型(如 EmbeddingResponse)不进入该分支,导致 final_value 仍为原始的 datetime.timedelta 对象。该对象随后被追加到延迟列表,并试图通过 json.dumps 写入 Redis 缓存,从而触发 TypeError。这是与 #8025 相同的缺陷,#14040 只修复了聊天分支。
环境排查
- 确认 LiteLLM 版本(如 v1.92.0 或更早),检查
litellm/router_strategy/lowest_latency.py中async_log_success_event函数的实现:定位到final_value: Union[float, timedelta] = response_ms这行,看其转换逻辑是否被包裹在if isinstance(response_obj, ModelResponse)内。 - 确认缓存后端是否为 Redis 或 Valkey。
- 确认路由策略配置为
latency-based-routing。 - 检查触发请求的类型是否为非聊天(embeddings、rerank、speech、image 等)。


![[Bug]: JWT team auth fails on /v1/messages unless x-litellm-team-id is explicitly set](https://www.chat-gpts.plus/wp-content/uploads/2026/07/31189-4bc625f4-768x403.jpg)
![[Bug]: /v1/messages adapter drops reasoning_content → thinking blocks for OpenAI-compatible chat-completions backends (streaming)](https://www.chat-gpts.plus/wp-content/uploads/2026/07/29518-e12a48c7-768x403.jpg)