From 6ea3232c8bb51605a0f6016436fdb7ef7c3353c7 Mon Sep 17 00:00:00 2001 From: Maxime Rivest Date: Thu, 15 Jan 2026 18:50:16 -0500 Subject: [PATCH] fix: lazy load dspy for faster CLI help MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move the dspy_cli.server.runner import from module level to inside the serve() function. This avoids loading dspy (~6s) when running `dspy-cli --help`. Before: 5.79s After: 0.10s Also update test to patch at source module instead of local name. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/dspy_cli/commands/serve.py | 3 ++- tests/test_commands_smoke.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/dspy_cli/commands/serve.py b/src/dspy_cli/commands/serve.py index 0a5ff8f..9c45798 100644 --- a/src/dspy_cli/commands/serve.py +++ b/src/dspy_cli/commands/serve.py @@ -9,7 +9,6 @@ import click -from dspy_cli.server.runner import main as runner_main from dspy_cli.utils.venv import ( detect_venv_python, has_package, @@ -118,6 +117,7 @@ def serve(port, host, logs_dir, reload, save_openapi, openapi_format, python, sy dspy-cli serve --python /path/to/venv/bin/python """ if system: + from dspy_cli.server.runner import main as runner_main runner_main( port=port, host=host, @@ -195,6 +195,7 @@ def serve(port, host, logs_dir, reload, save_openapi, openapi_format, python, sy _exec_clean(target_python, args) else: + from dspy_cli.server.runner import main as runner_main runner_main( port=port, host=host, diff --git a/tests/test_commands_smoke.py b/tests/test_commands_smoke.py index 636774f..d508541 100644 --- a/tests/test_commands_smoke.py +++ b/tests/test_commands_smoke.py @@ -96,7 +96,7 @@ def test_cli_e2e_smoke(runner, tmp_cwd, monkeypatch): def fake_runner_main(**kwargs): calls.update(kwargs) - monkeypatch.setattr("dspy_cli.commands.serve.runner_main", fake_runner_main) + monkeypatch.setattr("dspy_cli.server.runner.main", fake_runner_main) res = runner.invoke( main,