Skip to content

sdk-types: build models on first use (defer_build) - #41

Open
truham wants to merge 3 commits into
mainfrom
hamilton/sdk-types-defer-build
Open

truham wants to merge 3 commits into
mainfrom
hamilton/sdk-types-defer-build

Conversation

@truham

@truham truham commented Nov 13, 2026 •

Copy link
Copy Markdown

Importing turnkey_sdk_types builds all ~1,300 Pydantic models up front, which takes seconds and hundreds of MB. This sets defer_build on TurnkeyBaseModel so each model builds on first use.

Enabled on Pydantic 2.5+ only. On 2.1–2.2, deferred building crashes when a model is serialized before it's validated, and defer_build doesn't exist in 2.0. 2.3–2.4 are left off as a precaution. The pydantic>=2.0.0 requirement is unchanged.

Impact (import turnkey_http, Python 3.12, Pydantic 2.14, Apple M5 Max): 1.38s → 0.16s, 283MB → 59MB. Times vary by machine.

Testing: compared with and without the change across Python 3.10–3.14 and Pydantic 2.0–2.14 (not every pair). On 2.5+, schemas, dumps, validation errors and first-use behavior were identical across all models, and read-only API calls against a test org returned identical results.

New tests in packages/sdk-types/tests/test_defer_build.py, now run in CI:

  • every model builds;
  • models are unbuilt after import;
  • serializing before validating works;
  • dumps, validation errors and schemas match with deferral on and off for a sample of models.

🤖 Generated with Claude Code

truham and others added 3 commits November 12, 2026 18:40
Set Pydantic's defer_build on TurnkeyBaseModel so each generated model
builds its validator and serializer on first use instead of at import.
Only enabled on Pydantic 2.5+; older versions keep the current behavior.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Checks that every model builds, that serializing before validating works,
and that dumps, validation errors and schemas match with defer_build on
and off for a sample of models. CI runs only the sdk-types tests, which
need no credentials.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add a test that models are unbuilt after import, share one model-listing
helper, and document the sample size and how the test turns deferral off.
Tighten the changeset: lead with peak memory and note it depends on build
order, drop machine-specific timings, and remove the build-failure note
now covered by CI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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