datawhalechina/hello-agents · error · Error
章节生成失败:
Error message
章节生成失败:
What it means
'章节生成失败: ' + e (chapter generation failed) is alerted by the NovelGenerator frontend when POST {API_URL}/chapter/generate responds non-OK; the thrown Error wraps the raw response text. This is the heaviest endpoint — it asks the LLM to draft N chapters — so failures are frequently timeout/LLM-provider errors rather than simple validation issues. After success the code immediately loops to fetch each chapter's content, so a partial backend success can still leave missing previews.
Source
Thrown at Co-creation-projects/lgs-only-NovelGenerator/frontend/index.html:465
const generateChapters = async () => {
loading.value = true;
currentAction.value = 'chapter';
lastGeneratedChapter.value = null;
try {
const res = await fetch(`${API_URL}/chapter/generate`, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
novel_id: project.novel_id,
title: project.title,
user_input: chapterInput.user_input,
num_chapters: chapterInput.num_chapters,
chapter_length: chapterInput.chapter_length
})
});
if (!res.ok) throw new Error(await res.text());
const data = await res.json();
// 添加新生成的章节到列表
const newChapters = [];
for (const c of data.generated_chapters) {
// 为了能够立即预览,我们需要获取内容。
// 此时我们其实只知道 ID。为了简单,我们立即去 fetch 一次内容,或者后端返回的时候能带上内容最好。
// 查看后端接口,generate 只返回 id, title, summary。
// 所以我们需要单独 fetch 内容用于预览。
const chapterObj = {...c, content: null, loadingContent: false};
chapters.value.push(chapterObj);
newChapters.push(chapterObj);
}
// 自动加载最后一个生成的章节内容用于预览
if (newChapters.length > 0) {
const lastOne = newChapters[newChapters.length - 1];View on GitHub (pinned to 606a07d341)
Solutions
- Read the alert text (server body) — FastAPI validation errors name the exact field; LLM errors name the provider fault.
- Coerce num_chapters/chapter_length with Number() before sending and validate ranges.
- Increase backend/proxy timeouts, or generate fewer chapters per request.
- Verify novel_id is current (project created in this backend session).
Example fix
// before
body: JSON.stringify({
novel_id: project.novel_id,
title: project.title,
user_input: chapterInput.user_input,
num_chapters: chapterInput.num_chapters,
chapter_length: chapterInput.chapter_length
})
// after
body: JSON.stringify({
novel_id: project.novel_id,
title: project.title,
user_input: chapterInput.user_input,
num_chapters: Number(chapterInput.num_chapters),
chapter_length: Number(chapterInput.chapter_length)
}) Defensive patterns
Strategy: validation
Validate before calling
if (!project.novel_id) { alert('请先创建项目'); return; }
const num = Number(chapterInput.num_chapters);
const len = Number(chapterInput.chapter_length);
if (!Number.isInteger(num) || num < 1 || num > 20) { alert('章节数必须是 1-20 的整数'); return; }
if (!Number.isFinite(len) || len < 100) { alert('章节长度无效'); return; } Type guard
function isChapterGenerateResponse(d: unknown): d is { generated_chapters: Array<{ id: string; title: string; summary: string }> } {
return typeof d === 'object' && d !== null && Array.isArray((d as { generated_chapters?: unknown }).generated_chapters);
} Try / catch
try {
const res = await fetch(`${API_URL}/chapter/generate`, opts);
if (!res.ok) throw new Error(`${res.status}: ${(await res.text()).slice(0, 200)}`);
const data = await res.json();
if (!isChapterGenerateResponse(data)) throw new Error('章节响应格式异常');
} catch (e) {
alert('章节生成失败: ' + (e instanceof Error ? e.message : String(e)));
} finally {
loading.value = false;
} Prevention
- Coerce form inputs with Number() and range-check before sending — 422s here are almost always type mismatches.
- Keep num_chapters small per request; long multi-chapter LLM calls hit proxy timeouts.
- Raise backend/proxy timeouts for this endpoint specifically.
When it happens
Trigger: POST /chapter/generate with {novel_id, title, user_input, num_chapters, chapter_length} returns 404 (stale novel_id / wrong API_URL), 422 (num_chapters/chapter_length wrong type from form inputs), 500 (LLM API failure/timeout — generating multiple chapters in one call is slow), 504 proxy timeout on long generations.
Common situations: Form inputs delivering strings where the backend expects ints; LLM key/quota problems; proxy or server timeout too short for multi-chapter generation; project state from an earlier backend run.
Related errors
AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14).
Data as JSON: /api/errors/b737b037f73d8685.
Report an issue: GitHub.