Repository navigation
avoid rebuilding the message validator on every add request - #2612
yiwang-one wants to merge 1 commit into
Conversation
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.
🤖 Open Code ReviewTarget: PR #2612 ✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s). Generated by cloud-assistant via Open Code Review. |
✅ Automated Test Results: PASSEDRequired 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: Tested source: |
Description
What & Why
handle_add_memoriesdefined the feedback message validator (_check_messages)inside the request-handler body, decorated with
@validate_call. In Python adecorator 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
import time and shared by all requests.
exception flow — a validation failure or any other error inside the feedback
branch still falls back to the regular add path, as before.
is immutable after construction, and all per-call data lives on the call
stack (same model FastAPI itself uses for request validation).
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
How Has This Been Tested?
6 unit tests in
tests/api/test_add_handler.py(new file): validatoraccepts/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):
Checklist
Reviewer Checklist
Targeting
mainfollowing the post-v2.0.36 release flow; happy to retarget ifa dedicated dev branch is introduced.