Memory Schema
Manage structured note types using Basic Memory's Picoschema system. Schemas define what fields a note type should have, making notes uniform, queryable, and validatable.
When to Use
- - New note type emerging — you notice several notes share the same structure (meetings, people, decisions)
- Validation check — confirm existing notes conform to their schema
- Schema drift — detect fields that notes use but the schema doesn't define (or vice versa)
- Schema evolution — add/remove/change fields as requirements evolve
- On demand — user asks to create, check, or manage schemas
Picoschema Syntax Reference
Schemas are defined in YAML frontmatter using Picoschema — a compact notation for describing note structure.
Basic Types
CODEBLOCK0
Supported types: string, integer, number, boolean.
Optional Fields
Append ? to the field name:
CODEBLOCK1
Enums
Use (enum) with a list of allowed values:
CODEBLOCK2
Optional enum:
CODEBLOCK3
Arrays
Use (array) for list fields:
CODEBLOCK4
Relations
Reference other entity types directly:
CODEBLOCK5
Relations create edges in the knowledge graph, linking notes together.
Validation Settings
CODEBLOCK6
Complete Example
CODEBLOCK7
Discovering Unschemaed Notes
Look for clusters of notes that share structure but have no schema:
- 1. Search by type:
search_notes(query="type:Meeting") — if many notes share a type but no schema/Meeting.md exists, it's a candidate.
- 2. Infer a schema: Use
schema_infer to analyze existing notes and generate a suggested schema:
schema_infer(noteType="Meeting")
schema_infer(noteType="Meeting", threshold=0.5) # fields in 50%+ of notes
The threshold (0.0–1.0) controls how common a field must be to be included. Default is usually fine; lower it to catch rarer fields.
- 3. Review the suggestion — the inferred schema shows field names, types, and frequency. Decide which fields to keep, make optional, or drop.
Creating a Schema
Write the schema note to schema/<EntityName>:
CODEBLOCK9
Key Principles
- - Schema notes live in
schema/ — one note per entity type note_type="schema" marks it as a schema definitionentity: Meeting in metadata names the type it applies toversion: 1 in metadata — increment when making breaking changessettings.validation: warn is recommended to start — it logs issues without blocking writes
Validating Notes
Check how well existing notes conform to their schema:
CODEBLOCK10
Important: schema_validate checks for schema fields as observation categories in the note body — e.g., a status field expects - [status] active as an observation. Fields stored only in frontmatter metadata won't satisfy validation. To pass cleanly, include schema fields as both frontmatter values (for metadata search) and observations (for schema validation).
Validation reports:
- - Missing required fields — the note lacks a field the schema requires (as an observation category)
- Unknown fields — the note has fields the schema doesn't define
- Type mismatches — a field value doesn't match the expected type
- Invalid enum values — a value isn't in the allowed set
Handling Validation Results
- -
warn mode: Review warnings periodically. Fix notes that are clearly wrong; add optional fields to the schema for legitimate new patterns. error mode: Use for strict schemas where conformance matters (e.g., automated pipelines consuming notes).
Detecting Drift
Over time, notes evolve and schemas lag behind. Use schema_diff to find divergence:
CODEBLOCK11
Diff reports:
- - Fields in notes but not in schema — candidates for adding to the schema (as optional)
- Schema fields rarely used — consider making optional or removing
- Type inconsistencies — fields used as different types across notes
Schema Evolution
When note structure changes:
- 1. Run diff to see current state: INLINECODE23
- Update the schema note via
edit_note:
edit_note(
identifier="schema/Meeting",
operation="find_replace",
find_text="version: 1",
content="version: 2",
expected_replacements=1
)
- 3. Add/remove/modify fields in the
schema: block - Re-validate to confirm existing notes still pass: INLINECODE26
- Fix outliers — update notes that don't conform to the new schema
Evolution Guidelines
- - Additive changes (new optional fields) are safe — no version bump needed
- Breaking changes (new required fields, removed fields, type changes) should bump INLINECODE27
- Prefer optional over required — most fields should be optional to start
- Don't over-constrain — schemas should describe common structure, not enforce rigid templates
- Schema as documentation — even if validation is set to
warn, the schema serves as living documentation for what notes of that type should contain
Workflow Summary
CODEBLOCK13
记忆模式
使用Basic Memory的Picoschema系统管理结构化笔记类型。模式定义了笔记类型应包含的字段,使笔记统一、可查询且可验证。
使用场景
- - 新笔记类型出现 — 发现多个笔记共享相同结构(会议、人员、决策)
- 验证检查 — 确认现有笔记符合其模式
- 模式漂移 — 检测笔记使用但模式未定义的字段(反之亦然)
- 模式演进 — 根据需求变化添加/删除/修改字段
- 按需操作 — 用户要求创建、检查或管理模式
Picoschema语法参考
模式使用YAML前置元数据定义,采用Picoschema——一种描述笔记结构的紧凑标记法。
基本类型
yaml
schema:
name: string, 人员全名
age: integer, 年龄(岁)
score: number, 浮点评分
active: boolean, 当前是否活跃
支持的类型:string、integer、number、boolean。
可选字段
在字段名后附加?:
yaml
schema:
title: string, 必填字段
subtitle?: string, 可选字段
枚举
使用(enum)配合允许值列表:
yaml
schema:
status(enum): [active, blocked, done, abandoned], 当前状态
可选枚举:
yaml
schema:
priority?(enum): [low, medium, high, critical], 任务优先级
数组
使用(array)表示列表字段:
yaml
schema:
tags(array): string, 分类标签
steps?(array): string, 完成步骤
关系
直接引用其他实体类型:
yaml
schema:
parent_task?: Task, 父任务(如果是子任务)
attendees?(array): Person, 参会人员
关系在知识图谱中创建边,将笔记链接在一起。
验证设置
yaml
settings:
validation: warn # warn(记录问题)或 error(严格模式)
完整示例
yaml
title: 会议
type: schema
entity: Meeting
version: 1
schema:
topic: string, 讨论内容
date: string, 发生日期(YYYY-MM-DD)
attendees?(array): Person, 参会人员
decisions?(array): string, 做出的决策
action_items?(array): string, 后续任务
status?(enum): [scheduled, completed, cancelled], 会议状态
settings:
validation: warn
发现未定义模式的笔记
寻找共享结构但未定义模式的笔记集群:
- 1. 按类型搜索:search_notes(query=type:Meeting) — 如果多个笔记共享type但不存在schema/Meeting.md,则是一个候选。
- 2. 推断模式:使用schema_infer分析现有笔记并生成建议模式:
python
schema_infer(noteType=Meeting)
schema_infer(noteType=Meeting, threshold=0.5) # 50%+笔记中出现的字段
阈值(0.0–1.0)控制字段出现的频率要求。默认值通常合适;降低阈值可捕获更罕见的字段。
- 3. 审查建议 — 推断的模式显示字段名、类型和频率。决定保留哪些字段、设为可选或删除。
创建模式
将模式笔记写入schema/<实体名称>:
python
write_note(
title=Meeting,
directory=schema,
note_type=schema,
metadata={
entity: Meeting,
version: 1,
schema: {
topic: string, 讨论内容,
date: string, 发生日期,
attendees?(array): Person, 参会人员,
decisions?(array): string, 做出的决策
},
settings: {validation: warn}
},
content=# 会议
会议笔记的模式。
观察
- - [convention] 会议笔记存放在 memory/meetings/ 或作为每日条目
- [convention] 始终包含日期和主题
- [convention] 复杂的行动项应转化为任务
)
关键原则
- - 模式笔记存放在schema/ — 每个实体类型一个笔记
- note_type=schema 标记为模式定义
- 元数据中的entity: Meeting 指定其适用的类型
- 元数据中的version: 1 — 进行破坏性变更时递增
- 推荐以settings.validation: warn开始 — 记录问题但不阻止写入
验证笔记
检查现有笔记是否符合其模式:
python
验证某类型的所有笔记
schema_validate(noteType=Meeting)
验证单个笔记
schema_validate(identifier=meetings/2026-02-10-standup)
重要提示: schema_validate将模式字段作为笔记正文中的观察类别进行检查——例如,status字段期望- [status] active作为观察项。仅存储在前置元数据中的字段无法通过验证。要顺利通过,请将模式字段同时作为前置元数据值(用于元数据搜索)和观察项(用于模式验证)。
验证报告:
- - 缺少必填字段 — 笔记缺少模式要求的字段(作为观察类别)
- 未知字段 — 笔记包含模式未定义的字段
- 类型不匹配 — 字段值与预期类型不符
- 无效的枚举值 — 值不在允许的集合中
处理验证结果
- - warn模式:定期审查警告。修复明显错误的笔记;为合理的新模式添加可选字段。
- error模式:用于需要严格遵循模式的场景(例如,消费笔记的自动化流水线)。
检测漂移
随着时间推移,笔记会演变,模式会滞后。使用schema_diff发现差异:
python
schema_diff(noteType=Meeting)
差异报告:
- - 笔记中有但模式中没有的字段 — 考虑添加到模式中(作为可选字段)
- 很少使用的模式字段 — 考虑设为可选或删除
- 类型不一致 — 不同笔记中字段被用作不同类型
模式演进
当笔记结构发生变化时:
- 1. 运行差异分析查看当前状态:schemadiff(noteType=Meeting)
- 通过editnote更新模式笔记:
python
edit_note(
identifier=schema/Meeting,
operation=find_replace,
find_text=version: 1,
content=version: 2,
expected_replacements=1
)
- 3. 在schema:块中添加/删除/修改字段
- 重新验证确认现有笔记仍通过:schema_validate(noteType=Meeting)
- 修复异常笔记 — 更新不符合新模式的笔记
演进指南
- - 增量变更(新增可选字段)是安全的 — 无需版本号递增
- 破坏性变更(新增必填字段、删除字段、类型变更)应递增version
- 优先使用可选而非必填 — 大多数字段应初始设为可选
- 不要过度约束 — 模式应描述通用结构,而非强制执行僵化模板
- 模式即文档 — 即使验证设置为warn,模式仍作为该类型笔记应包含内容的活文档
工作流程总结
- 1. 发现重复笔记结构 → 推断模式(schemainfer)
- 审查并创建模式笔记 → 写入 schema/(writenote)
- 验证现有笔记 → 检查符合性(schemavalidate)
- 修复异常笔记 → 编辑不符合的笔记(editnote)
- 定期检查漂移 → 检测差异(schemadiff)
- 根据需要演进模式 → 更新模式笔记(editnote)