perf(schema): skip meta-schema validation for Pydantic-generated schemas - #577
Conversation
Pydantic's model_json_schema() produces valid Draft 2020-12 schemas by construction. The previous code validated every registered schema against the meta-schema via check_draft202012_schema, which walks the large meta-schema tree — the dominant cost of runtime construction (~6.7s across 527 unique schemas at ~12.7ms each). Add a trusted registration path that skips the meta-schema check while still rejecting external references. Use it from register_model(), the only producer of Pydantic-generated schemas. Measured improvement: ~0.5s reduction in cold runtime construction time.
|
Bugbot is not enabled for your account, so this pull request was not reviewed. Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 18d6042780
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 01cbe257c9
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| and model.model_config.get("json_schema_extra") is None | ||
| and _core_schema_uses_only_pydantic_defaults(model.__pydantic_core_schema__) |
There was a problem hiding this comment.
Validate untracked Pydantic schema metadata
When an operator-owned model supplies malformed schema metadata through an unchecked path—for example ConfigDict(title=17) under the pinned Pydantic 2.13.4—model_json_schema() emits the meta-schema-invalid "title": 17, but the model has no overridden method, hook, or json_schema_extra, so this predicate marks it trusted and persists it without validation. Fresh evidence beyond the earlier comment is that the mitigation still ignores other model config metadata and the analogous pydantic_js_updates generated by Field(title=17); the descriptor then fails only during later uncached resolution or payload validation. Ensure every schema-affecting customization is either validated or safely checked before taking the trusted path.
AGENTS.md reference: AGENTS.md:L81-L82
Useful? React with 👍 / 👎.
Summary
Runtime construction spends ~6.7s validating 527 unique schemas against the Draft 2020-12 meta-schema via
check_draft202012_schema. Each validation costs ~12.7ms because the meta-schema tree is large.Fix
Pydantics
model_json_schema()produces valid Draft 2020-12 schemas by construction. Add atrustedparameter toSchemaRegistry.register()that skips the expensive meta-schema validation while still rejecting external references. Use it fromregister_model(), the only producer of Pydantic-generated schemas.Performance
The savings are smaller on warm caches (the
lru_cachealready avoids re-validation on repeat calls), but the fix eliminates a consistent cold-start cost.Validation
tests/unit/schema and registry tests: 16 passed, 661 deselected