Skip to content

sdk(python): adopt no-codegen-at-runtime + slim module runtime - #13593

Merged
tiborvass merged 6 commits into
dagger:mainfrom
eunomie:python-sdk-no-codegen-at-runtime
Jul 30, 2026
Merged

tiborvass merged 6 commits into
dagger:mainfrom
eunomie:python-sdk-no-codegen-at-runtime

Conversation

@eunomie

@eunomie eunomie commented Jul 8, 2026 •

Copy link
Copy Markdown
Member

Builds on #13598 (the config-format runtime-codegen gate), now merged into main. This PR adopts that rule for the Python SDK and slims the module runtime.

  1. Python SDK adopts the rule. The Python SDK declares moduleRuntime's introspectionJson argument optional. For dagger-module.toml modules the engine omits it, and the runtime container is built from the module's committed vendored sdk/ — no vendoring step, no codegen exec, no uv lock refresh. uv sync --frozen means a stale committed lockfile fails loudly instead of silently re-resolving. Missing generated files give a clear "run dagger generate" error.

  2. Drop the dead ModuleTypesExp path from the Python runtime.

  3. Tests. Python integration coverage for the adoption: missing-files error, legacy dagger.json keeps regenerating, and trusted-files happy paths for Go and Python.

@eunomie

eunomie commented Jul 8, 2026

Copy link
Copy Markdown
Member Author

@eunomie

eunomie commented Jul 8, 2026

Copy link
Copy Markdown
Member Author

Added a follow-up commit: sdk(python): leaner interpreter boot in module runtime containers.

Every module function call boots a fresh interpreter, so import cost is paid 2–3× per dagger call. Measured with python -X importtime: the module entrypoint import is ~301ms, of which ~126ms is beartype-decorating the 80 generated gen.py methods and ~72ms the provisioning package (unreachable inside a module container). Inside module containers (DAGGER_MODULE set) the decoration and the provisioning import are now skipped — outside modules nothing changes. Also drops a duplicated currentFunctionCall.parentName round trip.

Numbers: entrypoint import 301→165ms (−45%); end-to-end scenario (playground, dev engine): cold call 5613→5007ms, first call of a new function 2704→2450ms, fully-warm calls unchanged (their cost is the per-session module-loading protocol, everything engine-side is already cached — separate topic).

@eunomie
eunomie force-pushed the python-sdk-no-codegen-at-runtime branch from adfcda9 to 5d05854 Compare July 9, 2026 13:19
@eunomie eunomie changed the title Python SDK: no codegen at runtime Config format decides runtime codegen; Python SDK adopts it Jul 9, 2026
@eunomie
eunomie force-pushed the python-sdk-no-codegen-at-runtime branch from 5d05854 to a2718f7 Compare July 9, 2026 13:28
@eunomie
eunomie force-pushed the python-sdk-no-codegen-at-runtime branch from a2718f7 to c969be3 Compare July 9, 2026 15:15
@eunomie eunomie changed the title Config format decides runtime codegen; Python SDK adopts it sdk(python): adopt no-codegen-at-runtime + slim module runtime Jul 9, 2026
@eunomie
eunomie force-pushed the python-sdk-no-codegen-at-runtime branch 8 times, most recently from a49d4b4 to 500cd6c Compare July 13, 2026 16:27
@eunomie eunomie added this to the v1.0.0 milestone Jul 15, 2026
@eunomie
eunomie force-pushed the python-sdk-no-codegen-at-runtime branch 2 times, most recently from eabe3df to 12c084e Compare July 15, 2026 11:16
@eunomie
eunomie marked this pull request as ready for review July 15, 2026 13:31
@eunomie
eunomie requested review from a team as code owners July 15, 2026 13:31
@eunomie eunomie self-assigned this Jul 15, 2026
@eunomie
eunomie force-pushed the python-sdk-no-codegen-at-runtime branch 3 times, most recently from de313be to 7822a35 Compare July 16, 2026 06:30
Comment thread sdk/python/src/dagger/__init__.py Outdated
# Engine provisioning (doesn't make sense in modules: connections always come
# from the session environment there, so don't pay its import cost on every
# function call)
if not os.getenv("DAGGER_MODULE"):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In a module runtime this drops Connection, connection, Config, DownloadError, ProvisionError, SessionError from the dagger namespace. I checked the SDK's own runtime code doesn't reference any of them (only this file imports dagger.provisioning), so it's internally safe. Just confirming it's intended that user module code can no longer reference e.g. dagger.connection at top level — reasonable since you never provision inside a module, but a subtle removal worth a conscious call (and maybe a changelog line).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is it something we are using on other SDKs? To provision engines from inside of a module? If not, I think it's just fine to remove it

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This was part of the "leaner interpreter boot" commit, which has since been dropped from the PR — the small per-call win was not worth the behavior changes you flagged here. So this no longer applies. Thanks for the review!

Comment thread sdk/python/src/dagger/client/_guards.py Outdated
conf=BeartypeConf(
violation_param_type=TypeError,
violation_verbosity=BeartypeViolationVerbosity.MINIMAL,
if os.getenv("DAGGER_MODULE"):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice boot-time win. One DX note: with decoration off, a Python-level type mismatch on a generated client call now surfaces as an engine/API error rather than a local TypeError. The golden traceback is updated so this is clearly intended — worth a changelog mention alongside the codegen rule, since it (and the provisioning change) are user-observable in modules.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This was part of the "leaner interpreter boot" commit, which has since been dropped from the PR — the small per-call win was not worth the behavior changes you flagged here. So this no longer applies. Thanks for the review!

TomChv
TomChv previously approved these changes Jul 16, 2026

@TomChv TomChv left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I left 2 design comment caught from claude but I'll approve the PR since I tested manually and it works as expected.

Manual test I did using a dev engine and beta.6 + python-sdk on main.

  • Install the python sdk
  • Create a module hello
  • Run generate -> the skip correctly work and so my generation is completed
  • dagger api functions
  • dagger api call --help (quite fast btw that's pleasant)

⚠️ No Entrypoint is generated yet so we expect the code to work without regenerating when we update the module's code

  • add function to the code and run dagger api call <my-mod> --help
  • call the function I just added
  • delete the sdk dir and do api call -> fails as expected, the error message is clear: run dagger generate
  • dagger generate work after and the module is back to normal 🚀

Multi module test

  • creating 2 python module and run generate -> work
  • delete sdk from one module and run api functions -> fails, should we still allow loading the working module or it is expected?
  • Run generate after -> work
  • Add 1 module as a dependency of the other and run generate: FAIL -> click to go to the repro message

@TomChv
TomChv dismissed their stale review July 16, 2026 12:17

Actually I may have found a bug!

@TomChv

TomChv commented Jul 16, 2026 •

Copy link
Copy Markdown
Member

I found a bug, here's the repro:

# Init 2 local python module
dagger sdk install python
dagger module init python hey
dagger module init python hello
dagger generate

# Add code to each module (whatever functions)

# Add one module has a dependency of the other
cd .dagger/modules/hello
dagger module deps add ../hey

# Regenerate to get new bindings
dagger generate # THIS FAILS

The generate will fail, see trace: https://dagger.cloud/Quartz/traces/9d328197968d11ae724ba374f9f5f3b9?listen=8fa3bceb980e10e8
I don't know if that's a python or engine issue

EDIT: this bug also happen with #13621 so I assume it's an engine issue

@eunomie

eunomie commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

@TomChv could you explain a bit more this one please?

delete sdk from one module and run api functions -> fails, should we still allow loading the working module or it is expected?

If the thing is "when a module can't be loaded dagger api functions fails, then yes it's expected because dagger api functions is loading the workspace, and dagger api functions MY_MODULE is also loading everything, until #13574 is merged

@TomChv

TomChv commented Jul 16, 2026

Copy link
Copy Markdown
Member

If the thing is "when a module can't be loaded dagger api functions fails, then yes it's expected

Yeah that's it!

@eunomie

eunomie commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

I found a bug, here's the repro:

Got it, it's the python-sdk not up to date, fixed by sdk-sdk polyfill. I'm fixing it

@eunomie
eunomie force-pushed the python-sdk-no-codegen-at-runtime branch from 7822a35 to d91c655 Compare July 16, 2026 13:49
@TomChv

TomChv commented Jul 22, 2026

Copy link
Copy Markdown
Member

We should wait for #13706 to be complete before merging this PR so we have a nice DX.

@grouville

grouville commented Jul 23, 2026 •

Copy link
Copy Markdown
Member

@TomChv @eunomie I tried to verify the claim in the PR body that uv sync --frozen makes a stale committed lockfile fail loudly. It seems this is not actually what uv does.

I added an integration test that starts from a real generated uv.lock, changes only pyproject.toml, and then loads the module through the native no-codegen path. With --frozen, the module still runs and returns hello: uv skips the freshness check and uses the old lock as-is. With --locked, the same repro stops with The lockfile at uv.lock needs to be updated, without modifying the lock.

I also wanted to make sure --locked wouldn't add a registry call on every module load. With the exact uv version from this PR (0.10.3), the equivalent check works in a Docker container with networking disabled:

docker run --rm --network none [...] uv lock --check --offline
Resolved 3 packages in 0.52ms

So the freshness check is local. This doesn't make installation offline—both modes can still download missing locked packages—but --locked itself doesn't add a network requirement.

There is a small performance cost because --frozen skips this check entirely. On the same 16-package project, with a warm cache and no network, the median no-op sync was 13.08ms with --frozen and 14.12ms with --locked: roughly a 1ms tradeoff for detecting stale generated state. Clean environment creation and package installation were much larger and noisy enough that the flag made no meaningful difference there. On an unchanged Dagger input, the sync execution can also be served from Dagger's cache rather than running uv again.

I pushed the fix and the test in 842b27eca2. I think this is the behavior we already intended and described in the PR; the uv flag was just not enforcing it.

@grouville
grouville force-pushed the python-sdk-no-codegen-at-runtime branch 2 times, most recently from 348fd72 to 842b27e Compare July 23, 2026 01:29
@tiborvass
tiborvass force-pushed the python-sdk-no-codegen-at-runtime branch from 842b27e to c277ecf Compare July 29, 2026 16:52
eunomie and others added 5 commits July 30, 2026 09:24
The engine detects the moduleTypes SDK capability by exact GraphQL field
name (core/sdk/module.go sdk.funcs["moduleTypes"]); this method renders
as moduleTypesExp, so the engine has never matched it and the function is
unreachable from module loading. Type discovery for Python goes through
the runtime container (moduleDefViaRuntime) and keeps doing so.

Signed-off-by: Yves Brissaud <[email protected]>
…is skipped

When the engine omits introspectionJson from moduleRuntime
(dagger-module.toml modules never run codegen at runtime), build the
runtime container straight from the committed context directory: keep
the committed vendored sdk/ (instead of stripping and re-vendoring it),
verify the generated files are present with an actionable error, skip
the codegen exec and the uv lock refresh, and sync dependencies with
--frozen so a stale committed lockfile fails loudly instead of being
silently re-resolved.

The codegen path (dagger develop / generate and legacy dagger.json
modules) is unchanged.

Signed-off-by: Yves Brissaud <[email protected]>
Python analogs of the Go RuntimeCodegen tests: a dagger-module.toml
module with nothing committed fails with the dagger-generate error, a
legacy dagger.json module keeps regenerating at runtime, and the happy
path serves calls from the materialized files (dropping the committed
vendored sdk makes the same call fail, proving the committed files are
what runs). Also adds the Go trusted-files happy-path test.

Signed-off-by: Yves Brissaud <[email protected]>
The trusted no-codegen path is meant to fail when the committed uv.lock no
longer matches pyproject.toml. However, uv sync --frozen skips that freshness
check and uses the stale lock as-is.

Use --locked so uv validates the committed lock without updating it. Add an
integration test that generates a valid lock, changes only pyproject.toml,
then proves the native no-codegen path rejects the stale generated state.

The test also uses a native workspace fixture rather than the legacy config,
which would regenerate the lock and hide this behavior.

Signed-off-by: Guillaume de Rouville <[email protected]>
@tiborvass
tiborvass force-pushed the python-sdk-no-codegen-at-runtime branch from c277ecf to caf5e78 Compare July 30, 2026 16:25
Signed-off-by: Tibor Vass <[email protected]>
@tiborvass
tiborvass force-pushed the python-sdk-no-codegen-at-runtime branch from e898b4a to 5d02a72 Compare July 30, 2026 16:53
@tiborvass
tiborvass merged commit 7abf363 into dagger:main Jul 30, 2026
87 checks passed
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.

4 participants