Skip to content

fix(settings): defer models import to prevent NameError after model config migration#1295

Open
Krashnicov wants to merge 1 commit intoagent0ai:developmentfrom
Deimos-AI:pr/fix-settings-deferred-import
Open

fix(settings): defer models import to prevent NameError after model config migration#1295
Krashnicov wants to merge 1 commit intoagent0ai:developmentfrom
Deimos-AI:pr/fix-settings-deferred-import

Conversation

@Krashnicov
Copy link

Problem

After the _model_config plugin refactor (PR #1262), the top-level import models in helpers/settings.py creates a circular import risk. Three functions still reference the models module directly:

  • convert_out() — calls models.get_api_key() via _get_api_key_field()
  • _get_api_key_field() — calls models.get_api_key(provider)
  • _load_sensitive_settings() — calls models.get_api_key(provider)

If the top-level import is removed (as would be natural during further cleanup), these functions crash with NameError: name models is not defined.

Even with the current code, the circular dependency between settings.py and models.py can cause import-order issues in certain initialization sequences.

Fix

Move import models from the module top-level into each function body that uses it, following the established pattern for circular dependency avoidance already used elsewhere in the codebase (e.g., from helpers import projects inside functions).

Changes

  • helpers/settings.py: Remove top-level import models, add deferred import models in convert_out(), _get_api_key_field(), and _load_sensitive_settings()

Testing

  • Verified all three functions work correctly with deferred import
  • No circular import errors on startup
  • Settings page loads and displays API key fields correctly

…eError

Three functions in helpers/settings.py reference the `models` module:
- convert_out()
- _get_api_key_field()
- _load_sensitive_settings()

The top-level `import models` creates a circular import risk between
settings.py and models.py. When model configuration is moved to the
_model_config plugin (PR agent0ai#1262) and the top-level import is eventually
removed, these functions would crash with NameError.

Fix: move the import into each function body, following the established
pattern for circular dependency avoidance in the codebase.
Krashnicov added a commit to Deimos-AI/agent-zero-deimos that referenced this pull request Mar 20, 2026
Add @extensible decorator to get_api_key() in models.py, enabling
plugins to intercept or replace the API key resolution logic for
any LLM provider.

Currently, get_api_key() resolves API keys exclusively from OS
environment variables via os.getenv(). This means external secrets
backends (OpenBao/Vault, AWS Secrets Manager, Azure Key Vault, etc.)
cannot provide API keys without injecting them into the process
environment.

With @extensible, a plugin can intercept the start hook to resolve
API keys from any backend, falling through to the default dotenv
behaviour when no plugin is active.

No behavioural change for existing users — the decorator only adds
extension hooks around the existing function.

Note: the companion fix for the circular import this previously
required (deferred import in settings.py) is submitted separately
in PR agent0ai#1295.
Krashnicov added a commit to Deimos-AI/agent-zero-deimos that referenced this pull request Mar 26, 2026
Add @extensible decorator to get_api_key() in models.py, enabling
plugins to intercept or replace the API key resolution logic for
any LLM provider.

Currently, get_api_key() resolves API keys exclusively from OS
environment variables via os.getenv(). This means external secrets
backends (OpenBao/Vault, AWS Secrets Manager, Azure Key Vault, etc.)
cannot provide API keys without injecting them into the process
environment.

With @extensible, a plugin can intercept the start hook to resolve
API keys from any backend, falling through to the default dotenv
behaviour when no plugin is active.

No behavioural change for existing users — the decorator only adds
extension hooks around the existing function.

Note: the companion fix for the circular import this previously
required (deferred import in settings.py) is submitted separately
in PR agent0ai#1295.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant