feat: support {env:VAR} interpolation in settings - #241
Open
gaojunran wants to merge 1 commit into
Open
Conversation
Allow any string value in global_settings.yml and project settings.yml
to reference environment variables via {env:VAR_NAME} placeholders.
Placeholders are expanded at load time, before dataclass construction,
so every config field (model name, API keys, base URLs, patterns, etc.)
can use them. Unset variables are replaced with empty strings.
This lets users keep secrets out of config files without relying solely
on shell environment inheritance.
Member
|
thanks a lot @gaojunran for the PR! @georgeh0 wdyt? |
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.
Summary
Allow any string value in
global_settings.ymland projectsettings.ymlto reference environment variables via{env:VAR_NAME}placeholders, expanded at load time.Motivation
Currently,
envsvalues and all other config fields are treated as literal strings. The only way to keep secrets out of config files is shell environment inheritance. This works forenvs(which are injected intoos.environ), but not for other fields likeembedding.modelorOPENAI_BASE_URLwhen you want to compose a value from an env var without putting the full literal in the file.This PR adds a simple
{env:VAR_NAME}interpolation syntax that works on every string value in both global and project settings.Usage
global_settings.ymland project-levelsettings.ymlImplementation
A single recursive function
_expand_env_placeholders()insettings.pywalks the parsed YAML dict/list/str tree and replaces{env:VAR}in strings via regex. It is called inload_user_settings()andload_project_settings()right afteryaml.safe_load()and before dataclass construction — one line change at each call site, keeping the logic centralized.Changes
src/cocoindex_code/settings.py: add_expand_env_placeholders(), call it in bothload_user_settingsandload_project_settingstests/test_settings.py: 6 new test cases covering envs interpolation, partial strings, model name interpolation, unset var behavior, save/load round-trip, and project settings interpolationREADME.md: document the feature with examplesTest plan
cocoindexruntime dependency not available in this environment)uv run pytest tests/test_settings.pypasses in a full dev environmentuv run ruff check .anduv run mypy .pass