Skip to content

Commit b30cb12

Browse files
NayukiChibaSoulter
andauthored
fix(provider): 修复 dict 格式 content 导致的 JSON 残留问题 (#5250)
* fix(provider): 修复 dict 格式 content 导致的 JSON 残留问题 修复 _normalize_content 函数未处理 dict 类型 content 的问题。 当 LLM 返回 {"type": "text", "text": "..."} 格式的 content 时, 现在会正确提取 text 字段而非直接转为字符串。 同时改进 fallback 行为,对 None 值返回空字符串。 Fixes #5244 * Update warning message for unexpected dict format --------- Co-authored-by: Soulter <37870767+Soulter@users.noreply.github.com>
1 parent 31d4e30 commit b30cb12

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

astrbot/core/provider/sources/openai_source.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,22 @@ def _normalize_content(raw_content: Any, strip: bool = True) -> str:
381381
plain string. This method handles both formats.
382382
383383
Args:
384-
raw_content: The raw content from LLM response, can be str, list, or other.
384+
raw_content: The raw content from LLM response, can be str, list, dict, or other.
385385
strip: Whether to strip whitespace from the result. Set to False for
386386
streaming chunks to preserve spaces between words.
387387
388388
Returns:
389389
Normalized plain text string.
390390
"""
391+
# Handle dict format (e.g., {"type": "text", "text": "..."})
392+
if isinstance(raw_content, dict):
393+
if "text" in raw_content:
394+
text_val = raw_content.get("text", "")
395+
return str(text_val) if text_val is not None else ""
396+
# For other dict formats, return empty string and log
397+
logger.warning(f"Unexpected dict format content: {raw_content}")
398+
return ""
399+
391400
if isinstance(raw_content, list):
392401
# Check if this looks like OpenAI content-part format
393402
# Only process if at least one item has {'type': 'text', 'text': ...} structure
@@ -450,7 +459,8 @@ def _normalize_content(raw_content: Any, strip: bool = True) -> str:
450459
return "".join(text_parts)
451460
return content
452461

453-
return str(raw_content)
462+
# Fallback for other types (int, float, etc.)
463+
return str(raw_content) if raw_content is not None else ""
454464

455465
async def _parse_openai_completion(
456466
self, completion: ChatCompletion, tools: ToolSet | None

0 commit comments

Comments
 (0)