Epic: Feature Flags Migration
Epic: Feature Flags Migration
Goal: Replace the existing feature-flag system with a lightweight, pydantic-settings-based approach that we fully control — making it easier to migrate HTML pages and deprecate the old internal-tooling dependency.
Context
Current System (Infogami)
Right now Open Library's feature flags live in conf/openlibrary.yml under a top-level features: key. Each flag is evaluated per-request via infogami.utils.features:
| Flag | Config Value | Effective Gate | Used Where |
|---|---|---|---|
debug |
enabled |
always on | Python: features.is_enabled("debug") |
dev |
enabled |
always on | Templates + Python ("dev" in ctx.features) |
history_v2 |
admin |
admin users only | Templates ("history_v2" in ctx.features) |
lists |
enabled |
always on | Templates + Python ("lists" in ctx.features) |
merge-authors |
{filter: usergroup, usergroup: /usergroup/librarians} |
librarians + admins (code fallback) | Templates + Python |
publishers |
enabled |
always on | Templates + Python ("publishers" in ctx.features) |
recentchanges_v2 |
enabled |
always on | Python: features.is_enabled("recentchanges_v2") |
stats |
enabled |
always on | Template only ("stats" in ctx.features) |
stats-header |
enabled |
always on | Python: "stats-header" in web.ctx.features |
superfast |
enabled |
always on | Template only ("superfast" in ctx.features) |
undo |
enabled |
always on | Python: features.is_enabled("undo") |
How the old system works:
- At startup,
infogami.utils.delegatecallsfeatures.set_feature_flags(config.get("features", {}))— stores the raw YAML dict asfeature_flags. - Per request,
features.loadhook()runsfind_enabled_features()which evaluates each flag's filter spec (string like"enabled"→filter_enabled→ alwaysTrue; dict like{filter: "usergroup", ...}→filter_usergroup→ checks group membership) and writes the resulting set intoweb.ctx.featuresandcontext.features. - Templates check membership:
$if "lists" in ctx.features: - Python code uses either
features.is_enabled("name")or"name" in web.ctx.features. web.ctx.featuresdefaults to[](set insetup_context_defaults()inopenlibrary/plugins/openlibrary/code.py:1145).
Dev config file: conf/openlibrary.yml (in-repo, used by Docker).
Prod configs: Managed per-environment (not in this repo).
New System (Example Code)
The following is example code — not committed, not wired into the application. No production code imports from openlibrary.core.features. Treat it as a reference for the pattern, not as work already done.
openlibrary/core/features.py
"""Pydantic-settings based feature flags.
POC for replacing the legacy ``infogami.utils.features`` module. Features
are loaded from the ``features:`` section of a YAML file (typically
``openlibrary.yml``) using explicit pydantic-settings fields.
A single process-wide ``features`` instance is loaded at import time from
the path given by the ``OL_FEATURES_YAML_PATH`` environment variable
(falling back to ``conf/openlibrary.yml``). Access flags via dot notation,
which gives full IDE autocomplete and type checking::
from openlibrary.core.features import features
if features.debug:
...
"""
from __future__ import annotations
import os
from pathlib import Path
import yaml
from pydantic_settings import BaseSettings
class Features(BaseSettings):
"""OpenLibrary feature flags.
Each field is a boolean flag. Override values by passing them as kwargs
or setting the corresponding ``OL_FEATURE_<NAME>`` environment variable.
Unknown keys in the YAML (e.g. admin/usergroup flags from the legacy
config) are silently ignored.
"""
model_config = {"extra": "ignore"}
debug: bool = False
dev: bool = False
lists: bool = True
publishers: bool = False
recentchanges_v2: bool = False
stats: bool = True
stats_header: bool = False
superfast: bool = False
undo: bool = True
# loops: bool
@classmethod
def from_yaml(cls, path: Path | str) -> Features:
"""Load feature flags from the ``features:`` section of a YAML file.
YAML keys are expected to match field names (kebab-case keys like
``stats-header`` are normalized to snake_case ``stats_header``).
Values should be native YAML booleans (``true``/``false``).
"""
data = yaml.safe_load(Path(path).read_text()) or {}
features_dict = data.get("features") or {}
normalized = {key.replace("-", "_"): bool(value) for key, value in features_dict.items()}
return cls(**normalized)
def _load_features() -> Features:
"""Load the process-wide Features instance from the configured YAML file."""
path = os.environ.get("OL_FEATURES_YAML_PATH", "conf/openlibrary.yml")
return Features.from_yaml(path)
# Process-wide singleton -- loaded once at import time.
# Access flags via dot notation: ``features.debug``, ``features.stats_header``.
features: Features = _load_features() # type: ignore[assignment]
openlibrary/tests/core/test_features.py
"""Tests for the pydantic-settings based feature flag POC."""
from __future__ import annotations
from pathlib import Path
from textwrap import dedent
import pytest
from openlibrary.core import features as features_module
from openlibrary.core.features import Features
def _write_test_config(tmp_path: Path, body: str) -> Path:
"""Write a test openlibrary.yml and assign it to the module-level
``features`` instance. Returns the config path.
"""
config = tmp_path / "openlibrary.yml"
config.write_text(dedent(body))
features_module.features = Features.from_yaml(config)
return config
@pytest.fixture(autouse=True)
def _reset_features_to_defaults(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
"""Ensure each test starts with a known-good in-memory ``features``
instance and no stray env var pointing at a real config file.
"""
monkeypatch.delenv("OL_FEATURES_YAML_PATH", raising=False)
_write_test_config(
tmp_path,
"""\
features:
debug: false
dev: false
lists: true
publishers: false
recentchanges_v2: false
stats: true
stats-header: false
superfast: false
undo: true
""",
)
class TestDefaults:
def test_default_values(self):
f = Features()
assert f.debug is False
assert f.dev is False
assert f.lists is True
assert f.publishers is False
assert f.recentchanges_v2 is False
assert f.stats is True
assert f.stats_header is False
assert f.superfast is False
assert f.undo is True
def test_kwargs_override_defaults(self):
f = Features(debug=True, lists=False)
assert f.debug is True
assert f.lists is False
class TestFromYaml:
def test_loads_enabled_flags(self, tmp_path: Path):
config = tmp_path / "openlibrary.yml"
config.write_text(
dedent(
"""\
features:
debug: true
dev: true
lists: true
publishers: true
recentchanges_v2: true
stats: true
stats-header: true
superfast: true
undo: true
"""
)
)
f = Features.from_yaml(config)
assert f.debug is True
assert f.stats_header is True # kebab-case YAML key normalized
def test_unset_flags_use_defaults(self, tmp_path: Path):
config = tmp_path / "openlibrary.yml"
config.write_text("features:\n debug: true\n")
f = Features.from_yaml(config)
assert f.debug is True
assert f.lists is True # default
assert f.undo is True # default
def test_ignores_non_features_keys(self, tmp_path: Path):
config = tmp_path / "openlibrary.yml"
config.write_text(
dedent(
"""\
site: openlibrary.org
features:
debug: true
"""
)
)
f = Features.from_yaml(config)
assert f.debug is True
assert "site" not in Features.model_fields
def test_missing_features_section_uses_defaults(self, tmp_path: Path):
config = tmp_path / "openlibrary.yml"
config.write_text("site: openlibrary.org\n")
f = Features.from_yaml(config)
assert f.debug is False
assert f.lists is True
class TestModuleInstance:
def test_features_is_a_features_instance(self):
assert isinstance(features_module.features, Features)
def test_features_singleton_is_shared(self):
assert features_module.features is features_module.features
def test_dot_notation_autocomplete_friendly(self):
# The whole point of the refactor: callers should get full type
# checking and IDE autocomplete via dot notation, not a stringly-typed
# ``is_enabled("flag_name")`` helper.
assert features_module.features.debug is False
assert features_module.features.lists is True
assert features_module.features.stats_header is False
assert features_module.features.undo is True
def test_reflects_yaml_reload(self, tmp_path: Path):
_write_test_config(tmp_path, "features:\n debug: true\n")
assert features_module.features.debug is True
Phase 1 — Python-only Feature Flag (Stats)
Scope: Pick the simplest existing flag — the stats flag, which is only used in Python code and requires no user-group logic.
Proposed approach (following the example code above):
- Set up a pydantic-settings config reader that loads a YAML file
- Define the
statsflag in the new config - Provide a clean import path so Python code can check it directly
- Wire the existing stats flag usage paths to the new source
- Remove the old stats flag definition
Wiring targets:
"stats" in ctx.featuresinopenlibrary/templates/site/footer.html:16"stats-header" in web.ctx.featuresinopenlibrary/plugins/openlibrary/stats.py:68
Success criteria:
- Stats flag setting is read from YAML via pydantic-settings
- No templates or user-group checks involved
- Old internal-tooling-generated flag is gone for this feature
Phase 2 — Boolean Feature Flag in HTML Templates
Scope: Pick a flag that is only used in HTML templates where the check is a simple enabled/disabled boolean.
Candidate flags for Phase 2 (boolean-only, no group gating):
| Flag | Template(s) | Check Pattern |
|---|---|---|
superfast |
viewpage.html:22, lib/history.html:4 |
"superfast" in ctx.features |
undo |
recentchanges/header.html:15 |
"undo" in ctx.features |
lists |
lists/widget.html:4, recentchanges/index.html:32, type/edition/view.html:535, type/user/view.html:12,33,65, type/author/view.html:178 |
"lists" in ctx.features |
publishers |
type/edition/view.html:266, publishers/view.html:89 |
"publishers" in ctx.features |
dev |
login.html:11, site/head.html:89, home/index.html:38 |
"dev" in ctx.features |
These are all simple membership checks — no user-group logic involved. The dev flag is also checked in Python (openlibrary/plugins/openlibrary/code.py:369, openlibrary/plugins/openlibrary/home.py:72,112, openlibrary/core/admin.py:91, openlibrary/plugins/openlibrary/deprecated_handler.py:18).
Steps:
- Make the new pydantic-settings config available in template context (e.g. add to
context.defaultsand inject there, or use a template global) - Update the chosen flag's template usage to read from the new source
- Remove the old flag definition
Success criteria:
- A template can check a boolean feature flag from the new config without touching user-group logic
- Clear pattern established for converting template-only flags
Phase 3 — Research & Decision: Do We Need User-Group Feature Flags?
Question: Should the new system support librarian / admin / super_librarian style feature gating at all?
Group-Based Flags Audit
Two flags in conf/openlibrary.yml use group-based gating:
| Flag | Config Gate | Code Fallback | Duplicates Permission Logic? |
|---|---|---|---|
history_v2 |
admin |
None (templates only) | Needs investigation — no code-level permission check exists |
merge-authors |
usergroup: /usergroup/librarians |
user.is_admin() in both GET and POST handlers (merge_authors.py:215,319) |
Yes — code already checks admin status independently |
Analysis:
merge-authors: The config restricts to librarians, but the code already allows any admin as a fallback. The usergroup filter is redundant. Dropping the group gating and relying on the code-level check would work — the code is the right place for this permission.history_v2: Only used in two templates. Under the old system, non-admin users never see it inctx.features. If we drop group gating, we need to either:- Make it a simple boolean and let it be visible to all (if appropriate for the feature), or
- Add an explicit admin check in the template
Ray's recommendation (unchanged):
- Avoid group-based feature flags in the config layer
- Keep permission logic in one place (the code) rather than splitting it between code and config
- User-group flags are unnecessary overhead
Possible outcomes:
- Option A (Recommended): Drop user groups entirely; feature flags become simple booleans
- Option B: Keep minimal group support but only for flags that genuinely need it
- Option C: Status quo — replicate the old group system in the new layer
Deliverable: A brief decision doc (can live in this note) with a recommendation and a migration plan for any affected flags.
Phase 4 — Migrate All Remaining Feature Flags
Scope: Move every remaining flag to the new pydantic-settings system.
- Convert boolean-only flags using the Phase 2 pattern
- Handle any group-based flags using the Phase 3 decision
- Clean up the old config-generator dependency once all flags are migrated
Success criteria:
- No feature flags read from the old internal-tooling-generated config
- Every HTML template using a feature flag reads from the new source
- Old feature-flag system can be fully retired
Settled Questions
- [x] Where does the current config file live? —
conf/openlibrary.ymlin the repo; per-environment configs managed outside the repo. - [x] What does the YAML schema look like? — Top-level
features:key with flag-name → filter-spec mappings. Simple flags use"enabled"/"disabled"strings; group-gated flags use"admin"or a dict{filter: "usergroup", usergroup: "/usergroup/librarians"}. See table in Context section above. - [x] How does the current app surface feature flags into templates? —
infogami.utils.features.loadhook()runs per-request, evaluates filter specs, and stores the enabled set inweb.ctx.features(andcontext.features). Templates check membership via"flag_name" in ctx.features. - [x] Does the new system need environment-aware overrides (e.g. dev vs. prod)? — No, because configs are already per-environment (different
openlibrary.ymlper deployment). TheOL_FEATURES_YAML_PATHenv var is sufficient.
This is a well-structured epic. A few findings from the codebase that may help contributors get started:
Context
-
openlibrary/core/features.pydoes not yet exist in master — the example code in this issue is not committed. Phase 1 contributors start from scratch using that code as a reference. -
pydantic-settingsis a new dependency —requirements.txtcurrently haspydantic==2.13.4only.pydantic_settings.BaseSettingslives in a separate package (pydantic-settings) that must be added torequirements.txtbefore the example code can run. This is a Phase 1 prerequisite. -
Direct predecessor: #12882 ("Audit Feature Flag Usage") produced a full per-flag inventory. Two cleanup PRs from that audit have already merged: #12883 (replaced
debugin the work_search template withquery_param) and #12888 (removed 6 entirely unused flags). Both are prerequisites to this migration and are already done. -
Four Python files import
infogami.utils.features— these are the Python-side wiring targets that must be updated as flags move to the new system:openlibrary/plugins/openlibrary/code.py,openlibrary/plugins/upstream/utils.py,openlibrary/plugins/upstream/recentchanges.py,openlibrary/admin/utils.py. -
Phase 2 note on
history_v2: The issue lists it as "admin-gated" and notes no code-level permission check exists. Confirmed — the only references are intemplates/history/comment.html:5andtemplates/lib/history.html:4. If the new system drops group gating (Phase 3 Option A), this flag would become visible to all users, which may need explicit admin-check logic in the templates.
Full triage checklist (maintainers / Richy)
- [x] Triage — correct labels applied or removed
- [x] Problem / opportunity — problem is clear, audience identified, actionable
- [x] Scope — issue is well-scoped; phased breakdown isolates complexity
- [x] Justification — infogami dependency removal and IDE-friendly dot notation are clear benefits
- [x] Success & testing criteria — each phase defines explicit success criteria
- [x] Proposal & breakdown — concrete phased plan with example code and wiring targets
- [ ] Risks, concerns, open questions —
pydantic-settingsnew dependency not flagged inline;history_v2group-gating removal may require template logic changes - [x] Richy context — relevant code, commits, and predecessor issues reviewed
- [x] References — config file, template paths, and Python call sites all documented inline
[!NOTE] This comment was automatically generated by Richy, via PAM, Open Library's Project AI Manager, on behalf of @mekarpeles. Richy is designed to provide status visibility, perform basic project management functions and relevant codebase research, and provide actionable feedback so contributors aren't left waiting.
Thanks @RayBB for the detailed description, will be starting with phase 1 as per the provided references!
We must be careful so that as we do this migration we don't assume things are set in the same way in local and in testing/prod.
Here's all the local variables and how they differ across environments:
| Feature | Local | Testing | Production | Diff between environments? |
|---|---|---|---|---|
| debug | enabled | — | filter: queryparamname: debugvalue: "XXXX" | Yes — all differ |
| dev | enabled | — | — | Yes — local only |
| history_v2 | admin | admin | filter: usergroupusergroup: /usergroup/librarians | Yes — production differs |
| lists | enabled | enabled | enabled | No |
| merge-authors | filter: usergroupusergroup: /usergroup/librarians | filter: usergroupusergroup: /usergroup/librarians | filter: usergroupusergroup: /usergroup/librarians | No |
| publishers | enabled | enabled | enabled | No |
| recentchanges_v2 | enabled | enabled | enabled | No |
| stats | enabled | enabled | enabled | No |
| stats-header | enabled | enabled | enabled | No |
| superfast | enabled | enabled | enabled | No |
| undo | enabled | filter: usergroupusergroup: /usergroup/super-librarians | filter: usergroupusergroup: /usergroup/super-librarians | Yes — local differs |
For all features (not just local) see: https://github.com/internetarchive/openlibrary/issues/13022#issuecomment-4797121185
Thanks @RayBB, stats/stats-header are uniform across all envs, so based on the findings should we narrow Features class to only define stats and stats_header for Phase 1 (adding fields as each flag is migrated)? That way missing dev and dict-valued undo/debug in prod YAML don't crash at import time. Does this approach sound good?
That's a good idea. I just pushed that up so I can deploy to testing.
I think a good next step to investigate actually is if we can get rid of the merge_authors feature flag and just use is_librarian (or similar) instead. That can be a good independent PR. Probably the same with the publishers feature flag (separate PR).
The merge-authors flag is redundant. The YAML flag {filter: usergroup, usergroup: /usergroup/librarians} duplicates user.is_librarian() - both check /usergroup/librarians.
FastAPI already ignores the flag (uses LibrarianDep). The web.py handlers and templates also have role-based fallbacks. I will go ahead and open a PR for this!
Will followup with the investigation on publishers feature flag!
@Sanket17052006 go ahead on your investigation with publishers and then a separate one for lists (which might be harder to grep for).
@Sanket17052006 want to give a swing at:
lists- remove everywhererecentchanges_v2- remove everywheresuperfast- remove everywhere These have been stable defaults for a loooong time and I don't think they provide any value at all to us. Each should be a separate PR.
After those we'll have to think more critically about how we want to handle usergroups.
@RayBB the lists flag removal PR #13087 is ready. recentchanges_v2 and superfast are on my queue next.
@Sanket17052006 good job with this.
I think next you could open a PR to the infogami codebase to do something similar for removing recentchanges_v2 - https://github.com/internetarchive/infogami/pull/283
Then replace features.is_enabled("undo") and "undo" in ctx.features with an explicit user.is_super_librarian() check. It is matching what prod already enforces. This eliminates the flag without changing behavior.
Next, I think we should replace dev with OLEnv.LOCAL_DEV or similar. The reason being is it's not really a feature at all it's just a flag that says what environment it's in. This should simplify our needs here. Open to your thoughts on this one.
✅ history_v2 we can replace with is_librarian_or_higher. I'm unsure if we really need to keep this to librarian+ but rather than wait for a discussion lets just go forward with the change.
It's starting to look more and more like you're right. The feature flags will have very little use and I think that's a good win that simplifies things.
So I think the above is 4 PRs you can work through. How does that sound?
Thanks @RayBB, the plan makes sense to me rather than migrating the flags to a new system we can eliminate them completely that would simplify our codebase with no behavioral change! Will get started with the cleanups!
@Sanket17052006 wanna replace features.is_enabled("undo") and "undo" in ctx.features with an explicit user.is_super_librarian() check. It is matching what prod already enforces. This eliminates the flag without changing behavior?
Yes the branch for undo feature flag is already done on my local I'll just will rebase and open the PR.
Closing this as done because the only thing left is the cleanup of the config, we have a separate issue for that and I'll take care of it with drini because it needs staff.
@Sanket17052006 thanks for your awesome work on this!