Skip to content

perf(schema): skip meta-schema validation for Pydantic-generated schemas - #577

Merged
morluto merged 3 commits into
mainfrom
agent/fix-schema-validation-bottleneck
Aug 6, 2026
Merged

perf(schema): skip meta-schema validation for Pydantic-generated schemas#577
morluto merged 3 commits into
mainfrom
agent/fix-schema-validation-bottleneck

Conversation

@morluto

@morluto morluto commented Aug 6, 2026

Copy link
Copy Markdown
Owner

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 a trusted parameter to SchemaRegistry.register() that skips the expensive meta-schema validation while still rejecting external references. Use it from register_model(), the only producer of Pydantic-generated schemas.

Performance

Time
Baseline 5.06s
With fix 4.64s
Improvement ~0.5s (8%)

The savings are smaller on warm caches (the lru_cache already 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
  • Runtime construction tests: 28 passed, 649 deselected

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.
@cursor

cursor Bot commented Aug 6, 2026

Copy link
Copy Markdown

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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/jacobian/schema_registry.py Outdated
@morluto
morluto merged commit c928039 into main Aug 6, 2026
@morluto
morluto deleted the agent/fix-schema-validation-bottleneck branch August 6, 2026 17:48

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +122 to +123
and model.model_config.get("json_schema_extra") is None
and _core_schema_uses_only_pydantic_defaults(model.__pydantic_core_schema__)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant