refactor(caption.py): 改进 CaptionChatResp 解析逻辑,增强字段处理健壮性

This commit is contained in:
begoniezhao
2025-08-06 16:33:16 +08:00
parent db7930535b
commit 6d1e192a2c
+21 -6
View File
@@ -115,12 +115,27 @@ class CaptionChatResp:
A parsed CaptionChatResp object A parsed CaptionChatResp object
""" """
logger.info("Parsing CaptionChatResp from JSON") logger.info("Parsing CaptionChatResp from JSON")
# Manually parse nested fields # Manually parse nested fields with safe field extraction
choices = [ choices = []
Choice(message=Message(**choice["message"])) for choice in json_data.get("choices", []):
for choice in json_data.get("choices", []) message_data = choice.get("message", {})
] message = Message(
usage = Usage(**json_data["usage"]) if "usage" in json_data else None role=message_data.get("role"),
content=message_data.get("content"),
tool_calls=message_data.get("tool_calls"),
)
choices.append(Choice(message=message))
# Handle usage with safe field extraction
usage_data = json_data.get("usage", {})
usage = None
if usage_data:
usage = Usage(
prompt_tokens=usage_data.get("prompt_tokens", 0),
total_tokens=usage_data.get("total_tokens", 0),
completion_tokens=usage_data.get("completion_tokens", 0),
)
logger.info( logger.info(
f"Parsed {len(choices)} choices and usage data: {usage is not None}" f"Parsed {len(choices)} choices and usage data: {usage is not None}"
) )