fix(provider): normalize DeepSeek non-string message content
This commit is contained in:
@@ -345,10 +345,25 @@ class OpenAICompatProvider(LLMProvider):
|
||||
return json.dumps(arguments, ensure_ascii=False)
|
||||
return "{}"
|
||||
|
||||
@staticmethod
|
||||
def _coerce_content_to_string(content: Any) -> str | None:
|
||||
"""Coerce block/list content into plain text for strict string-only APIs."""
|
||||
if content is None or isinstance(content, str):
|
||||
return content
|
||||
text = OpenAICompatProvider._extract_text_content(content)
|
||||
if isinstance(text, str) and text:
|
||||
return text
|
||||
try:
|
||||
dumped = json.dumps(content, ensure_ascii=False)
|
||||
except Exception:
|
||||
dumped = str(content)
|
||||
return dumped or "(empty)"
|
||||
|
||||
def _sanitize_messages(self, messages: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
"""Strip non-standard keys, normalize tool_call IDs."""
|
||||
sanitized = LLMProvider._sanitize_request_messages(messages, _ALLOWED_MSG_KEYS)
|
||||
id_map: dict[str, str] = {}
|
||||
force_string_content = bool(self._spec and self._spec.name == "deepseek")
|
||||
|
||||
def map_id(value: Any) -> Any:
|
||||
if not isinstance(value, str):
|
||||
@@ -382,6 +397,11 @@ class OpenAICompatProvider(LLMProvider):
|
||||
clean["content"] = None
|
||||
if "tool_call_id" in clean and clean["tool_call_id"]:
|
||||
clean["tool_call_id"] = map_id(clean["tool_call_id"])
|
||||
if (
|
||||
force_string_content
|
||||
and not (clean.get("role") == "assistant" and clean.get("tool_calls"))
|
||||
):
|
||||
clean["content"] = self._coerce_content_to_string(clean.get("content"))
|
||||
return self._enforce_role_alternation(sanitized)
|
||||
|
||||
def _drop_deepseek_incomplete_reasoning_history(
|
||||
|
||||
Reference in New Issue
Block a user