binary-husky/gpt_academic · error · RuntimeError
GPT is not generating proper code.
Error message
GPT is not generating proper code.
What it means
The Manim plugin requires the LLM reply to contain exactly one fenced code block. get_code_block() raises when re.findall finds zero blocks or more than one. Unlike the dynamic-function parser, it does not search multiple blocks for a Scene class.
Source
Thrown at crazy_functions/Math_Animation_Gen.py:50
time_str = gen_time_str()
subprocess.check_output([sys.executable, '-c', f"from gpt_log.MyAnimation import {class_name}; {class_name}().render()"])
shutil.move(f'media/videos/1080p60/{class_name}.mp4', f'gpt_log/{class_name}-{time_str}.mp4')
return f'gpt_log/{time_str}.mp4'
except subprocess.CalledProcessError as e:
output = e.output.decode()
logger.error(f"Command returned non-zero exit status {e.returncode}: {output}.")
return f"Evaluating python script failed: {e.output}."
except:
logger.error('generating mp4 failed')
return "Generating mp4 failed."
def get_code_block(reply):
import re
pattern = r"```([\s\S]*?)```" # regex pattern to match code blocks
matches = re.findall(pattern, reply) # find all code blocks in text
if len(matches) != 1:
raise RuntimeError("GPT is not generating proper code.")
return matches[0].strip('python') # code block
@CatchException
def 动画生成(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, user_request):
"""
txt 输入栏用户输入的文本,例如需要翻译的一段话,再例如一个包含了待处理文件的路径
llm_kwargs gpt模型参数,如温度和top_p等,一般原样传递下去就行
plugin_kwargs 插件模型的参数,暂时没有用武之地
chatbot 聊天显示框的句柄,用于显示给用户
history 聊天历史,前情提要
system_prompt 给gpt的静默提醒
user_request 当前用户的请求信息(IP地址等)
"""
# 清空历史,以免输入溢出
history = []
# 基本信息:功能、贡献者
chatbot.append([View on GitHub (pinned to d6bde0fa54)
Solutions
- Regenerate with a stronger code model and lower temperature.
- Strengthen the system prompt: return exactly one Python block starting with from manim import *.
- Prefer a block that defines a manim Scene instead of failing merely because there is more than one.
- Validate the response before calling eval_manim and retry once with feedback.
- Inspect the raw gpt_say to distinguish model formatting from a proxy error.
Example fix
# before
matches = re.findall(r"```([\s\S]*?)```", reply)
if len(matches) != 1:
raise RuntimeError("GPT is not generating proper code.")
return matches[0].strip('python')
# after
matches = re.findall(r"```(?:python)?\s*([\s\S]*?)```", reply)
scene_blocks = [m for m in matches if re.search(r"class\s+\w+\s*\(\s*Scene\s*\)", m)]
if len(scene_blocks) == 1:
return scene_blocks[0]
if len(matches) == 1:
return matches[0]
raise RuntimeError(f"GPT reply contains {len(matches)} code blocks and no unique Scene")
Defensive patterns
Strategy: validation
Validate before calling
import re
def has_single_manim_block(reply) -> bool:
return len(re.findall(r"```(?:python)?\s*([\s\S]*?)```", reply)) == 1
Type guard
def has_manim_scene(reply: str) -> bool:
blocks = re.findall(r"```(?:python)?\s*([\s\S]*?)```", reply or "")
return any(re.search(r"class\s+\w+\s*\(\s*Scene\s*\)", block) for block in blocks)
Try / catch
try:
code = get_code_block(gpt_say)
except RuntimeError:
gpt_say = yield from request_manim_rewrite(gpt_say)
code = get_code_block(gpt_say)
Prevention
- Ask for exactly one complete Python fenced block.
- Remove extra example blocks from the active prompt if the model echoes them.
- Use a code-capable model for Manim generation.
- Validate that a Scene class exists before rendering.
When it happens
Trigger: The model wraps zero or multiple snippets in ``` fences, adds a second example or output block, replies with prose/refusal, or uses ~~~ fences.
Common situations: The prompt/history already contains example code blocks and the model echoes them; a weak model ignores the one-block instruction; output is truncated; high temperature produces extra examples.
Related errors
- GPT is not generating proper code.
- 无法提取query_type标签内容
- 无法提取query_type标签内容
- 抱歉, 我们暂时无法解析此PDF文档: {fp}。
- Invalid URL: {url}
AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14).
Data as JSON: /api/errors/a9c831714673921a.
Report an issue: GitHub.