Skip to content

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

fix(llm): validate recovered search terms before use

Rudy Celekli4d63e3570b9d
diff --git a/app/services/llm.py b/app/services/llm.py
index b86b2b8e..827d1f39 100644
--- a/app/services/llm.py
+++ b/app/services/llm.py
@@ -901,6 +901,7 @@ Please note that you must use English for generating video search terms; Chinese
search_terms = []
response = ""
for i in range(_max_retries):
+ search_terms = []
try:
if app_config is None:
response = _generate_response(prompt)
@@ -914,12 +915,6 @@ Please note that you must use English for generating video search terms; Chinese
logger.error(f"failed to generate video terms: {response}")
return []
search_terms = json.loads(_strip_code_fence(response))
- if not isinstance(search_terms, list) or not all(
- isinstance(term, str) for term in search_terms
- ):
- logger.error("response is not a list of strings.")
- continue
-
except Exception as e:
logger.warning(f"failed to generate video terms: {str(e)}")
if response:
@@ -933,6 +928,14 @@ Please note that you must use English for generating video search terms; Chinese
# 是模型格式问题还是解析逻辑问题。
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.
+ if not isinstance(search_terms, list) or not all(
+ isinstance(term, str) for term in search_terms
+ ):
+ logger.error("response is not a list of strings.")
+ search_terms = []
+
if search_terms and len(search_terms) > 0:
break
if i < _max_retries - 1:
diff --git a/test/services/test_llm.py b/test/services/test_llm.py
index d056200d..18f05c9f 100644
--- a/test/services/test_llm.py
+++ b/test/services/test_llm.py
@@ -253,6 +253,24 @@ class TestScriptPromptOptions(unittest.TestCase):
self.assertEqual(result, [])
self.assertIsInstance(result, list)
+ def test_generate_terms_retries_non_string_items_in_recovered_json(self):
+ """The prose-wrapped JSON recovery path must enforce List[str] too."""
+ with patch.object(
+ llm,
+ "_generate_response",
+ side_effect=[
+ 'Search terms: [123, {"query": "coffee"}]',
+ 'Search terms: ["coffee beans", "barista tools"]',
+ ],
+ ) as generate_response:
+ result = llm.generate_terms(
+ video_subject="Coffee",
+ video_script="How to brew coffee.",
+ )
+
+ self.assertEqual(result, ["coffee beans", "barista tools"])
+ self.assertEqual(generate_response.call_count, 2)
+
def test_video_script_request_rejects_invalid_advanced_options(self):
"""
API 请求模型需要限制高级 prompt 参数,避免外部调用绕过 WebUI