Skip to content

avoid rebuilding the message validator on every add request - #2612

Open
yiwang-one wants to merge 1 commit into
MemTensor:mainfrom
yiwang-one:fix/hoist-add-message-validator
Open

yiwang-one wants to merge 1 commit into
MemTensor:mainfrom
yiwang-one:fix/hoist-add-message-validator

Conversation

@yiwang-one

Copy link
Copy Markdown

Description

What & Why

handle_add_memories defined the feedback message validator (_check_messages)
inside the request-handler body, decorated with @validate_call. In Python a
decorator executes at function-definition time, so this rebuilt the pydantic
validation schema — which covers the full OpenAI chat-message union type — on
every add request, even for requests that never reach the feedback branch.
This schema construction was a significant recurring cost on the write path.

Approach

  • Hoist the decorated function to module level: the schema is built once at
    import time and shared by all requests.
  • Validation semantics are unchanged: same schema, same call site, same
    exception flow — a validation failure or any other error inside the feedback
    branch still falls back to the regular add path, as before.
  • The shared validator is safe under concurrency: it is stateless, its schema
    is immutable after construction, and all per-call data lives on the call
    stack (same model FastAPI itself uses for request validation).
  • An inline comment documents why the definition must stay at module scope.

No new dependencies.

中文说明

handle_add_memories 此前在函数体内定义带 @validate_call 装饰的消息校验器,
导致其 pydantic 校验 schema(覆盖完整的 OpenAI 聊天消息联合类型)在每个写请求
时都重新构建——包括从不进入 feedback 分支的请求。本 PR 将该函数提升到模块级:
schema 在 import 时构建一次、全局共享;校验语义、调用点、异常降级路径全部不变,
并发下安全(无状态 + 构建后不可变)。无新增依赖。

Equivalence: unit tests compare the shared validator against a freshly
built per-request replica across valid/invalid inputs — accept/reject decisions
match one-for-one. Routing tests lock in the feedback success path, the
fallback-to-add degradation, and the regular add path.

Related Issue (Required): N/A — small performance fix with no functional change

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Unit Test
  • Test Script Or Steps (please provide)

6 unit tests in tests/api/test_add_handler.py (new file): validator
accepts/rejects correctly; equivalence vs a freshly built per-request
validator; feedback routing with history/content extraction assertions;
feedback-internal error falls back to regular add; non-feedback add unchanged.

Reproduce (repo root):

PYTHONPATH=src python -m pytest tests/api/test_add_handler.py -q
# => 6 passed

PYTHONPATH=src python -m pytest tests/api/ -q
# => 153 passed

ruff format --check src/memos/api/handlers/add_handler.py tests/api/test_add_handler.py
# => already formatted

Checklist

  • I have performed a self-review of my own code | 我已自行检查了自己的代码
  • I have commented my code in hard-to-understand areas | 我已在难以理解的地方对代码进行了注释
  • I have added tests that prove my fix is effective or that my feature works | 我已添加测试以证明我的修复有效或功能正常
  • MemOS-Docs not applicable — internal performance fix, no public API change | 不适用——内部性能修复,无公共 API 变化
  • I have linked the issue to this PR (if applicable) | N/A
  • I have mentioned the person who will review this PR | 我已提及将审查此 PR 的人

Reviewer Checklist

  • Made sure Checks passed
  • Tests have been provided

Targeting main following the post-v2.0.36 release flow; happy to retarget if
a dedicated dev branch is introduced.

The feedback message validator (_check_messages) was defined inside
handle_add_memories with @validate_call, so its pydantic schema —
which covers the full OpenAI chat message union — was rebuilt on
every add request, whether or not the feedback branch ran. Hoist the
decorated function to module level: the schema is built once at
import and shared across requests. Validation behavior and the
failure fallback path are unchanged.
@Memtensor-AI Memtensor-AI added area:api 云服务 / FastAPI / OpenAPI / MCP status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Nov 10, 2026
@Memtensor-AI

Copy link
Copy Markdown
Collaborator

🤖 Open Code Review

Target: PR #2612
Task: 3d3b6c1c53f6e895
Base: main
Head: fix/hoist-add-message-validator
Head SHA: b6633f1d311e4b70e948772b844e238ec8d692f2

✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s).

Generated by cloud-assistant via Open Code Review.

@Memtensor-AI

Copy link
Copy Markdown
Collaborator

✅ Automated Test Results: PASSED

Required tests passed (6/6 executed, 0 skipped). Duration: 13s [advisory, non-gating] AI-generated tests on branch test/auto-gen-3d3b6c1c53f6e895-20261110170303: 89/91 passed, 2 failed — these do NOT affect the PR verdict; review the branch manually.

Branch: fix/hoist-add-message-validator

Tested source: b6633f1d311e4b70e948772b844e238ec8d692f2
Tested base: 3a16e5fe3048a209f722806f4123fa2e533588fd
Tested commit: b6633f1d311e4b70e948772b844e238ec8d692f2
These results apply only to this immutable source/base snapshot.

@Memtensor-AI Memtensor-AI added status:ready Ready for implementation; waiting for assignee or AI dispatch | 可进入实现,等待认领或派发 and removed status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:api 云服务 / FastAPI / OpenAPI / MCP status:ready Ready for implementation; waiting for assignee or AI dispatch | 可进入实现,等待认领或派发

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants