LCORE-1226: ability to export configuration schema#1055
Conversation
WalkthroughA new command-line option Changes
Sequence Diagram(s)sequenceDiagram
participant User as User
participant CLI as lightspeed_stack (CLI)
participant Dumper as schema_dumper
participant FS as Filesystem
User->>CLI: invoke with `-s/--dump-schema`
CLI->>Dumper: call dump_schema("schema.json")
Dumper->>Dumper: generate pydantic JSON schema\napply recursive_update
Dumper->>FS: write OpenAPI JSON to "schema.json"
FS-->>Dumper: write result
Dumper-->>CLI: return success/failure
CLI-->>User: log result and exit
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@src/utils/schema_dumper.py`:
- Around line 38-62: The dump_schema function may write None for
components.schemas if schemas.get("$defs") is missing and its docstring is too
terse; update dump_schema to use schemas.get("$defs", {}) so an empty dict is
written instead of None, expand the docstring to Google style including Args
(filename: str) and Raises (IOError/OSError for file operations and any
exceptions from models_json_schema), and extract the hardcoded "0.3.0" into a
named constant (e.g., SCHEMA_VERSION) placed in an appropriate constants module
and referenced from dump_schema; locate these changes around the dump_schema
function and usages of models_json_schema, Configuration, and recursive_update
to implement the fixes.
🧹 Nitpick comments (3)
src/lightspeed_stack.py (3)
18-18: Import style inconsistency with coding guidelines.The coding guidelines specify using absolute imports for internal modules (e.g.,
from authentication import get_auth_dependency). Consider using the absolute import form.Proposed fix
-from utils import schema_dumper +from utils.schema_dumper import dump_schemaThen update line 122:
- schema_dumper.dump_schema("schema.json") + dump_schema("schema.json")
28-42: Docstring missing documentation for the new-s/--dump-schemaoption.The function docstring lists the CLI options but doesn't include the newly added
-s/--dump-schemaflag.Proposed docstring update
def create_argument_parser() -> ArgumentParser: """Create and configure argument parser object. The parser includes these options: - -v / --verbose: enable verbose output - -d / --dump-configuration: dump the loaded configuration to JSON and exit + - -s / --dump-schema: dump configuration schema into OpenAPI-compatible file and exit - -c / --config: path to the configuration file (default "lightspeed-stack.yaml") - -g / --generate-llama-stack-configuration: generate a Llama Stack configuration from the service configuration - -i / --input-config-file: Llama Stack input configuration filename (default "run.yaml") - -o / --output-config-file: Llama Stack output configuration filename (default "run_.yaml") Returns: Configured ArgumentParser for parsing the service CLI options. """
79-96: Docstring formain()should document the new--dump-schemabehavior.The docstring describes behavior for
--dump-configurationand--generate-llama-stack-configurationbut doesn't mention--dump-schema.Proposed docstring update
Parses command-line arguments, loads the configured settings, and then: - If --dump-configuration is provided, writes the active configuration to configuration.json and exits (exits with status 1 on failure). + - If --dump-schema is provided, writes the configuration schema to + schema.json in OpenAPI-compatible format and exits (exits with status 1 on failure). - If --generate-llama-stack-configuration is provided, generates and stores the Llama Stack configuration to the specified output file and exits (exits with status 1 on failure).
| def dump_schema(filename: str) -> None: | ||
| """Dump the configuration schema into OpenAPI-compatible JSON file.""" | ||
| with open(filename, "w", encoding="utf-8") as fout: | ||
| # retrieve the schema | ||
| _, schemas = models_json_schema( | ||
| [(model, "validation") for model in [Configuration]], | ||
| ref_template="#/components/schemas/{model}", | ||
| ) | ||
|
|
||
| # fix the schema | ||
| schemas = recursive_update(schemas) | ||
|
|
||
| # add all required metadata | ||
| openapi_schema = { | ||
| "openapi": "3.0.0", | ||
| "info": { | ||
| "title": "Lightspeed Core Stack", | ||
| "version": "0.3.0", | ||
| }, | ||
| "components": { | ||
| "schemas": schemas.get("$defs"), | ||
| }, | ||
| "paths": {}, | ||
| } | ||
| json.dump(openapi_schema, fout, indent=4) |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# First, explore the project structure
echo "=== Project Structure ==="
git ls-files | head -20
# Check if constants.py exists
echo -e "\n=== Checking for constants.py ==="
find . -name "constants.py" -type f
# Check for any VERSION-related constants
echo -e "\n=== Searching for VERSION constants ==="
rg -n "^VERSION\s*=" --type py
# Look at the actual schema_dumper.py file
echo -e "\n=== Checking schema_dumper.py ==="
cat -n src/utils/schema_dumper.py
# Check for version strings in the codebase
echo -e "\n=== Searching for '0.3.0' ==="
rg "0\.3\.0" --type py
# Check imports in schema_dumper.py
echo -e "\n=== Checking models_json_schema ==="
rg -n "models_json_schema" --type py -B 2 -A 2Repository: lightspeed-core/lightspeed-stack
Length of output: 4358
🏁 Script executed:
#!/bin/bash
# Check what's in constants.py
echo "=== Contents of constants.py ==="
cat -n src/constants.py
# Check imports in schema_dumper.py to understand what's available
echo -e "\n=== Current imports in schema_dumper.py ==="
head -n 10 src/utils/schema_dumper.py
# Look at pyproject.toml or setup.py to see version definition
echo -e "\n=== Checking for version in pyproject.toml ==="
rg -n "version\s*=" . -t toml | head -10
# Check if there are any version-related patterns in the codebase
echo -e "\n=== Searching for __version__ ==="
rg -n "__version__" --type pyRepository: lightspeed-core/lightspeed-stack
Length of output: 7933
🏁 Script executed:
#!/bin/bash
# Check version.py
echo "=== Contents of version.py ==="
cat -n src/version.py
# Check if there's any schema version constant defined elsewhere
echo -e "\n=== Searching for schema version references ==="
rg -n "schema.*version\|protocol.*version" --type py -i
# Check how the codebase typically uses versions
echo -e "\n=== Checking version usage patterns ==="
rg -n "from version import\|from constants import" --type py | head -20
# Look at pydantic documentation or test to see what models_json_schema returns
echo -e "\n=== Checking if there are tests for dump_schema ==="
find . -name "*test*schema*dumper*" -o -name "*test*dump_schema*" 2>/dev/null
# Check if schemas.get("$defs") is used elsewhere
echo -e "\n=== Searching for schemas.get or \\\$defs references ==="
rg -n '\$defs|schemas\.get' --type pyRepository: lightspeed-core/lightspeed-stack
Length of output: 813
Address the potential None value from schemas.get("$defs") and expand the docstring per Google conventions.
schemas.get("$defs")could returnNoneif the key doesn't exist, which would set"schemas": Nonein the output. Useschemas.get("$defs", {})to provide a default empty dictionary.- Expand the docstring to include Args and Raises sections per Google Python conventions, as required by the coding guidelines.
- The hardcoded
"0.3.0"appears to be an intentional schema/OpenAPI version (separate from the app version inversion.py). If this should be a constant, defineSCHEMA_VERSIONor similar inconstants.pyrather than importing a non-existent VERSION constant.
Proposed improvements
def dump_schema(filename: str) -> None:
- """Dump the configuration schema into OpenAPI-compatible JSON file."""
+ """Dump the configuration schema into OpenAPI-compatible JSON file.
+
+ Args:
+ filename: Path to the output JSON file.
+
+ Raises:
+ IOError: If the file cannot be written.
+ """
with open(filename, "w", encoding="utf-8") as fout:
# retrieve the schema
_, schemas = models_json_schema(
[(model, "validation") for model in [Configuration]],
ref_template="#/components/schemas/{model}",
)
# fix the schema
schemas = recursive_update(schemas)
# add all required metadata
openapi_schema = {
"openapi": "3.0.0",
"info": {
"title": "Lightspeed Core Stack",
"version": "0.3.0",
},
"components": {
- "schemas": schemas.get("$defs"),
+ "schemas": schemas.get("$defs", {}),
},
"paths": {},
}
json.dump(openapi_schema, fout, indent=4)🤖 Prompt for AI Agents
In `@src/utils/schema_dumper.py` around lines 38 - 62, The dump_schema function
may write None for components.schemas if schemas.get("$defs") is missing and its
docstring is too terse; update dump_schema to use schemas.get("$defs", {}) so an
empty dict is written instead of None, expand the docstring to Google style
including Args (filename: str) and Raises (IOError/OSError for file operations
and any exceptions from models_json_schema), and extract the hardcoded "0.3.0"
into a named constant (e.g., SCHEMA_VERSION) placed in an appropriate constants
module and referenced from dump_schema; locate these changes around the
dump_schema function and usages of models_json_schema, Configuration, and
recursive_update to implement the fixes.
Description
LCORE-1226: ability to export configuration schema
Type of change
Tools used to create PR
Related Tickets & Documents
Summary by CodeRabbit
-s/--dump-schemacommand-line option to export the configuration schema in OpenAPI 3.0.0 format (JSON), enabling generation of schema documentation and API specifications alongside the existing configuration dump capability.✏️ Tip: You can customize this high-level summary in your review settings.