Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(llm): recover one JSON value from prose responses

Rudy Celekli905ec873e8e5
diff --git a/app/services/llm.py b/app/services/llm.py
index 1e5851e3..03e73360 100644
--- a/app/services/llm.py
+++ b/app/services/llm.py
@@ -905,6 +905,20 @@ def _strip_code_fence(text: str) -> str:
return t.strip()
+def _extract_embedded_json(text: str, opening: str):
+ """Decode one complete JSON value without absorbing later prose/examples."""
+ decoder = json.JSONDecoder()
+ for index, character in enumerate(text or ""):
+ if character != opening:
+ continue
+ try:
+ value, _end = decoder.raw_decode(text[index:])
+ except json.JSONDecodeError:
+ continue
+ return value
+ raise ValueError("response contains no complete JSON value")
+
+
def generate_terms(
video_subject: str,
video_script: str,
@@ -990,15 +1004,10 @@ Please note that you must use English for generating video search terms; Chinese
except Exception as e:
logger.warning(f"failed to generate video terms: {str(e)}")
if response:
- match = re.search(r"\[.*]", response, re.DOTALL)
- if match:
- try:
- search_terms = json.loads(match.group())
- except Exception as e:
- # 这里保留重试流程,但必须记录 LLM 返回的非标准 JSON,
- # 否则后续排查搜索词为空时无法定位
- # 是模型格式问题还是解析逻辑问题。
- logger.warning(f"failed to generate video terms: {str(e)}")
+ try:
+ search_terms = _extract_embedded_json(response, "[")
+ except ValueError as e:
+ logger.warning(f"failed to generate video terms: {str(e)}")
# Apply the same contract to direct JSON and prose-wrapped recovery.
# Otherwise a nonempty array of numbers or objects reaches material search.
@@ -1196,9 +1205,7 @@ def _parse_social_metadata(response: str, platform: str) -> dict:
except Exception:
# 部分模型会在 JSON 外层包一段说明文字或 markdown fence。
# API 调用方只需要稳定结构,所以这里尝试提取第一个 JSON object。
- match = re.search(r"\{.*\}", response or "", re.DOTALL)
- if match:
- data = json.loads(match.group())
+ data = _extract_embedded_json(response, "{")
if not isinstance(data, dict):
raise ValueError("social metadata response is not a JSON object")
diff --git a/test/services/test_llm_prose_json_boundaries.py b/test/services/test_llm_prose_json_boundaries.py
new file mode 100644
index 00000000..a6a6aab8
--- /dev/null
+++ b/test/services/test_llm_prose_json_boundaries.py
@@ -0,0 +1,19 @@
+import json
+from unittest.mock import patch
+
+from app.services import llm
+
+
+def test_terms_recovery_stops_after_first_array():
+ response = 'Suggested visuals: ["ocean waves", "sailing boat"]\nRelated alternatives: ["beach"]'
+ with patch.object(llm, "_generate_response", return_value=response) as provider:
+ assert llm.generate_terms("Ocean", "A boat sails.", amount=2) == [
+ "ocean waves", "sailing boat"
+ ]
+ assert provider.call_count == 1
+
+
+def test_social_recovery_stops_after_first_object():
+ metadata = {"title": "Ocean", "caption": "Go sailing", "hashtags": ["#ocean"]}
+ response = f'Publishing copy: {json.dumps(metadata)}\nExample settings: {{"temperature": 0.2}}'
+ assert llm._parse_social_metadata(response, "tiktok") == metadata