Repository navigation
Improve performance of GenerateSchema.generate_schema() dispatching - #13614
Merged
Merged
Conversation
Further improvements by doing a single `get_origin()` call early, and deciding whether to find a `__get_pydantic_core_schema__()` method based on the presence/absence of the origin. Misc improvements around this logic included too.
Deploying pydantic-docs with
|
| Latest commit: |
029c106
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://25f693db.pydantic-docs.pages.dev |
| Branch Preview URL: | https://vp-generate-schema-perf.pydantic-docs.pages.dev |
Merging this PR will improve performance by 8.11%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ⚡ | test_simple_model_schema_lots_of_fields_generation |
14.6 ms | 12.7 ms | +15.33% |
| ⚡ | test_complex_model_schema_generation |
1.1 ms | 1 ms | +13.36% |
| ⚡ | test_interconnected_models |
5.6 ms | 5.1 ms | +9.31% |
| ⚡ | test_lots_of_models_with_lots_of_fields |
1.8 s | 1.7 s | +8.37% |
| ⚡ | test_tagged_union_with_callable_discriminator_schema_generation |
1,044.8 µs | 964.7 µs | +8.31% |
| ⚡ | test_fastapi_startup_perf |
85.7 ms | 79.8 ms | +7.38% |
| ⚡ | test_recursive_model_schema_generation |
728.8 µs | 685.4 µs | +6.34% |
| ⚡ | test_schema_build |
2.4 ms | 2.3 ms | +6.2% |
| ⚡ | test_nested_recursive_model_schema_generation |
1.3 ms | 1.2 ms | +6.15% |
| ⚡ | test_nested_recursive_generic_model_schema_generation |
1.2 ms | 1.1 ms | +5.82% |
| ⚡ | test_model_subclass |
2.9 ms | 2.7 ms | +5.66% |
| ⚡ | test_recursive_discriminated_union_with_base_model |
1.3 ms | 1.2 ms | +5.57% |
Tip
Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.
Comparing vp/generate-schema-perf (029c106) with main (796b6d8)
Footnotes
-
163 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩
Contributor
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
jimmyjbling
pushed a commit
to jimmyjbling/pydantic
that referenced
this pull request
Oct 23, 2026
Since pydantic#13614, `generate_schema()` and `_apply_annotations.inner_handler()` compute `origin = get_origin(obj)` once and pass it to `_generate_schema_inner()`. When `obj` is `Self`, the substitution with the type on `model_type_stack` happens inside `_generate_schema_inner()`, so the pre-computed origin (`None`) is stale. For a parametrized generic dataclass such as `Node[int]`, `match_type()` then takes the plain-type branch, where `dataclasses.is_dataclass()` is false for the generic alias, and raises `PydanticSchemaGenerationError`. Recompute the origin after substituting `Self`.
1 task done
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Further improvements by doing a single
get_origin()call early, and deciding whether to find a__get_pydantic_core_schema__()method based on the presence/absence of the origin.Misc improvements around this logic included too.
Change Summary
Related issue number
Checklist