diff --git a/src/ai_client.py b/src/ai_client.py index 215ceeda..028c4b14 100644 --- a/src/ai_client.py +++ b/src/ai_client.py @@ -2318,10 +2318,11 @@ def _send_deepseek(md_content: str, user_message: str, base_dir: str, _append_comms("IN", "response", {"round": round_idx, "text": "(No choices returned)", "usage": response_data.get("usage", {})}) break choice = choices[0] - message = choice.get("message", {}) - assistant_text = message.get("content", "") - tool_calls_raw = message.get("tool_calls", []) - reasoning_content = message.get("reasoning_content", "") + from src.openai_schemas import ChatMessage as _CM + message = _CM.from_dict(choice.get("message", {})) + assistant_text = message.content or "" + tool_calls_raw = [tc.to_dict() for tc in message.tool_calls] if message.tool_calls else [] + reasoning_content = choice.get("message", {}).get("reasoning_content", "") finish_reason = choice.get("finish_reason", "stop") usage = response_data.get("usage", {}) @@ -2451,7 +2452,8 @@ def _repair_minimax_history(history: list[Metadata]) -> None: elif isinstance(tc, dict) and tc.get("id"): call_ids.append(tc["id"]) for cid in call_ids: - already_has = any(m.get("role") == "tool" and m.get("tool_call_id") == cid for m in history[-len(call_ids)-1:]) + from src.openai_schemas import ChatMessage as _CM + already_has = any(_CM.from_dict(m).role == "tool" and _CM.from_dict(m).tool_call_id == cid for m in history[-len(call_ids)-1:]) if not already_has: history.append({ "role": "tool", diff --git a/src/openai_schemas.py b/src/openai_schemas.py index 7fab91e3..173eec2d 100644 --- a/src/openai_schemas.py +++ b/src/openai_schemas.py @@ -78,7 +78,12 @@ class ChatMessage: tool_calls = None if raw_tool_calls is not None: tool_calls = tuple(ToolCall.from_dict(tc) for tc in raw_tool_calls) - return cls(**{**_from_dict_filter(cls, data), "tool_calls": tool_calls}) + filtered = _from_dict_filter(cls, data) + if "role" not in filtered: + filtered["role"] = "assistant" + if "content" not in filtered: + filtered["content"] = "" + return cls(**{**filtered, "tool_calls": tool_calls}) @dataclass(frozen=True)