datawhalechina/hello-agents · error · ValueError

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

Error message

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

What it means

A ValueError raised by extract_note_id() when re.search(r"ID:\s*(note_[0-9_]+)", output) finds no match in the NoteTool output text. The demo script needs the note's id to chain subsequent operations (view/update/delete), so it regex-parses the tool's human-readable output; if the tool printed the id in a different format (or printed an error instead), extraction fails.

Source

Thrown at code/chapter9/03_note_tool_operations.py:22

展示 NoteTool 的核心操作:
1. 创建笔记 (create)
2. 读取笔记 (read)
3. 更新笔记 (update)
4. 搜索笔记 (search)
5. 列出笔记 (list)
6. 笔记摘要 (summary)
7. 删除笔记 (delete)
"""

from hello_agents.tools import NoteTool
import re


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)


def main():
    print("=" * 80)
    print("NoteTool 基本操作示例")
    print("=" * 80 + "\n")

    # 初始化 NoteTool
    notes = NoteTool(workspace="./project_notes")

    # 1. 创建笔记
    print("1. 创建笔记...")
    create_output_1 = notes.run({
        "action": "create",
        "title": "重构项目 - 第一阶段",
        "content": """## 完成情况
已完成数据模型层的重构,测试覆盖率达到85%。

View on GitHub (pinned to 606a07d341)

Solutions

  1. Print the actual output passed to extract_note_id and compare it against the regex — a full-width colon or missing 'ID:' label is the usual mismatch.
  2. If the format changed, widen the regex: r"ID[::]\s*(note_[0-9_]+)" or extract any token matching r"note_[0-9_]+".
  3. Extract the id at creation time from NoteTool's data structures if it exposes one, instead of regex-parsing prose.
  4. If the create itself failed, fix that first — this error is downstream of a failed operation.

Example fix

# before
match = re.search(r"ID:\s*(note_[0-9_]+)", output)

# after
match = re.search(r"ID[::]\s*(note_[0-9_]+)", output) or re.search(r"\b(note_[0-9_]+)\b", output)
Defensive patterns

Strategy: try-catch

Validate before calling

import re
def has_note_id(output: str) -> bool:
    return re.search(r"note_[0-9_]+", output) is not None

Type guard

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

Try / catch

try:
    note_id = extract_note_id(output)
except ValueError:
    print(f"unexpected NoteTool output:\n{output}")
    raise

Prevention

When it happens

Trigger: Calling extract_note_id on output from NoteTool operations whose text does not contain an 'ID: note_...' line — e.g. running it on the output of a failed create, a summary/list operation, or a create whose success message format changed (different prefix, no 'ID:' label, id characters outside [0-9_]). Note the regex requires the literal 'ID:' with an ASCII colon.

Common situations: NoteTool's message templates updated in a newer helloagents version so 'ID:' no longer appears or uses a Chinese full-width colon ':'; the create failed (bad workspace path) and the output is an error string; the note id gained letters/hyphens not matched by [0-9_]; locale-dependent formatting.

Related errors


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