datawhalechina/hello-agents · error · ValueError

无法从输出解析 note_id: {output}

Error message

无法从输出解析 note_id:
{output}

What it means

extract_note_id() in chapter_generate_agent.py applies the regex r"ID:\s*(note_[0-9_]+)" to the free text returned by hello_agents' NoteTool and raises ValueError when no match is found. The failure means the tool output did not contain a line shaped like 'ID: note_123_...' — either the note creation failed, the tool printed an error/usage message instead, or the tool's output format drifted from what the regex expects. It is a brittle text-protocol coupling between agent code and tool output.

Source

Thrown at Co-creation-projects/lgs-only-NovelGenerator/agents/chapter_generate_agent.py:18

from dotenv import load_dotenv
load_dotenv()
import re
import os
import json
from pydantic import BaseModel
from typing import List, Dict, Any
from datetime import datetime
from hello_agents import SimpleAgent, HelloAgentsLLM
from hello_agents.tools import NoteTool
from prompt import CHAPTER_PROMPT, CHAPTER_REVIEW_PROMPT, CHAPTER_START_PROMPT


def extract_note_id(output: str) -> str:
    """从 NoteTool 的输出文本中提取 note_id"""
    match = re.search(r"ID:\s*(note_[0-9_]+)", output)
    if not match:
        raise ValueError(f"无法从输出解析 note_id:\n{output}")
    return match.group(1)


class MemoryItem(BaseModel):
    """记忆项数据结构"""
    node_id: str
    novel_id: str
    title: str
    content: str
    summary: str
    timestamp: datetime
    metadata: Dict[str, Any] = {}
    next_chapter_prediction: str = ""


class ChapterGenerateAgent:
    """具有上下文感知能力的 Agent"""

View on GitHub (pinned to 606a07d341)

Solutions

  1. Print/inspect the actual output passed to extract_note_id to see whether note creation succeeded and what format the ID line uses.
  2. Fix the upstream failure if the output is an error message (check NoteTool backend, novel_id, workspace path) — the parser is correct, the input is not.
  3. Widen the regex to match the current format, e.g. r"(?:ID|note_id)\s*[::]?\s*(note_[0-9A-Za-z_-]+)" keeping the same capture group.
  4. Prefer a structured return from NoteTool (dict/JSON with a note_id field) instead of parsing English text, if the hello_agents version supports it.
  5. Pin the hello-agents version in requirements so output format cannot drift unnoticed.

Example fix

# before
match = re.search(r"ID:\s*(note_[0-9_]+)", output)
# after
match = re.search(r"(?:ID|note_id)\s*[::]?\s*(note_[0-9A-Za-z_-]+)", output, re.IGNORECASE)
if not match:
    raise ValueError(f"无法从输出解析 note_id:\n{output}")
Defensive patterns

Strategy: try-catch

Validate before calling

import re
def has_note_id(output: str) -> bool:
    return bool(re.search(r"ID:\s*(note_[0-9_]+)", output))
# gate before parsing
if not has_note_id(tool_output):
    logger.error("NoteTool output has no ID; creation likely failed: %r", tool_output)

Type guard

import re
from typing import Optional

def extract_note_id_safe(output: str) -> Optional[str]:
    m = re.search(r"ID:\s*(note_[0-9_]+)", output)
    return m.group(1) if m else None

Try / catch

try:
    note_id = extract_note_id(output)
except ValueError:
    logger.exception("note_id parse failed; raw output: %s", output)
    # treat as generation failure: retry the NoteTool call or abort chapter
    raise

Prevention

When it happens

Trigger: NoteTool.create returns an error string (e.g. storage failure, invalid novel_id) so the literal 'ID:' never appears; the agent LLM paraphrases or truncates the tool output before extract_note_id runs; hello_agents is upgraded and NoteTool now prints 'note_id: ...' or JSON instead of 'ID: note_...'; the note id contains characters outside [0-9_] (letters, dashes) so the pattern skips it.

Common situations: Version drift after pip install -U hello-agents changes NoteTool formatting; running with a memory backend that fails silently and returns a message without an ID; copy-pasting this helper into another agent whose tool output uses a different template; long generations where the ID line is cut off by a token limit.

Related errors


AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14). Data as JSON: /api/errors/afdec4482f775648. Report an issue: GitHub.