Skip to content

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

Open
truham wants to merge 1 commit into
mainfrom
hamilton/sdk-types-defer-build
Open

truham wants to merge 1 commit 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. In testing, deferred building crashes when a model is serialized before it's validated in 2.1–2.2, and fails intermittently under concurrent first use in 2.3–2.4, so those versions keep the current behavior. defer_build doesn't exist in 2.0. The pydantic>=2.0.0 requirement is unchanged.

Impact (Python 3.12, Pydantic 2.14, import turnkey_http): 1.38s → 0.16s, 283MB → 59MB.

Testing: with vs. without the change on Python 3.10–3.14 × Pydantic 2.0–2.14. On 2.5+, schemas, dumps, validation errors and first-use behavior were identical across all models, with no new failures under concurrent use. The existing tests and live API calls also gave identical results.

🤖 Generated with Claude Code

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>

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