fix(cli): import yaml lazily so the CLI works without the yaml extra#4929
Merged
sobolevn merged 1 commit intoJul 15, 2026
Merged
Conversation
The module-level 'from yaml import dump' in litestar/cli/commands/schema.py
is imported eagerly via litestar.cli.main, so every CLI invocation failed
with ModuleNotFoundError when litestar was installed without the yaml extra
(pyyaml only comes in transitively with the standard extras via uvicorn).
Import yaml inside the YAML export branch and raise
MissingDependencyException('pyyaml', extra='yaml') if it is missing, matching
how other optional dependencies are handled.
Fixes litestar-org#4449
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4929 +/- ##
=======================================
Coverage 67.29% 67.30%
=======================================
Files 293 293
Lines 15228 15230 +2
Branches 1728 1728
=======================================
+ Hits 10248 10250 +2
Misses 4833 4833
Partials 147 147 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Description
Fixes the CLI dying at import time when litestar is installed without pyyaml:
litestar/cli/commands/schema.pyimported yaml at module level, and it is imported eagerly vialitestar.cli.main— so every CLI invocation failed. pyyaml only arrives transitively with thestandardextras (via uvicorn), while the docs promise the CLI and its hard dependencies are included by default.The yaml import now happens inside the YAML-export branch of
_generate_openapi_schema, raisingMissingDependencyException("pyyaml", extra="yaml")when missing — the same pattern used for other optional dependencies. Exporting to.json(and every other CLI command) no longer needs pyyaml; exporting to.yaml/.ymlgives an actionable install hint instead of a bare traceback.Verified in a venv without pyyaml:
litestar --helpfails onmainwith the reported traceback and works with this change. Addedtest_cli_importable_without_pyyaml(simulates pyyaml's absence viasys.modules), which fails onmain; the fulltest_schema_commands.pysuite passes (14 passed).Disclosure per the contribution guidelines: this fix was developed with AI assistance (Claude Code), with the behavior verified end-to-end locally as described above.
Closes
Closes #4449
📚 Documentation preview 📚: https://litestar-org.github.io/litestar-docs-preview/4929