feat: detect API keys from env vars and pflow settings#8
Conversation
Previously, model suggestions only worked when API keys were configured via `llm keys set`. Keys in environment variables or pflow settings were not detected. Changes: - Add inject_settings_env_vars() to inject pflow settings keys into os.environ at startup, allowing the llm library to find them - Add _has_provider_key() to check multiple sources: env vars, pflow settings, and llm CLI keys - Update _detect_default_model() to use new multi-source detection - Add model suggestion with detected default when unknown model error - Fix trace to correctly show success=false when node returns "error" - Include original exception details in LLM error messages Priority order: 1. Environment variables (user's actual env, never overwritten) 2. pflow settings (~/.pflow/settings.json env section) 3. llm CLI keys (existing behavior) Fixes #7
Changes based on PR #8 review: 1. Convert to lazy imports in new functions - `_has_provider_key()`: Now uses `from pflow.core.settings import SettingsManager` inside the function instead of module-level import - `inject_settings_env_vars()`: Same lazy import pattern - Prevents tight coupling and potential circular dependency issues 2. Fix test isolation - Added cleanup in `test_injects_keys_from_settings` to remove injected env vars after assertion - Updated patch locations from `pflow.core.llm_config.SettingsManager` to `pflow.core.settings.SettingsManager` to match lazy import behavior 3. Add missing test for PYTEST_CURRENT_TEST guard - New test `test_skipped_in_pytest_environment` verifies that injection is properly skipped when running in pytest - Ensures SettingsManager is never instantiated when guard triggers Technical notes: - Patch location must match where import happens (source module), not where it's used, when using lazy imports - pytest sets PYTEST_CURRENT_TEST during test execution, after fixtures run, requiring patch.dict approach instead of monkeypatch.delenv in fixture
Code Review: Robust LLM Provider DetectionThis PR implements multi-source API key detection to improve model suggestions and enable API keys stored in pflow settings. Overall, the implementation is solid and well-tested. Here's my detailed feedback: ✅ Critical — must fix before mergeNone identified. The implementation follows security best practices and handles edge cases well.
|
Summary
Enhances LLM provider detection to check multiple sources for API keys, not just the
llmCLI. This makes model suggestions and actual LLM calls work reliably for all users regardless of how they configured their API keys.Fixes #7
Changes
Environment Injection
inject_settings_env_vars()to inject API keys from~/.pflow/settings.jsonintoos.environat startupmain.py) and MCP server (mcp_server/main.py) before any LLM operationsMulti-Source Detection
_has_provider_key()to check multiple sources in order:os.environ)settings.jsonenv section)_has_llm_key()fallback)PROVIDER_ENV_VARSmapping for each provider's environment variable namesError Message Improvements
success=falsewhen node returns "error" actionFiles Changed
src/pflow/core/llm_config.pysrc/pflow/cli/main.pyinject_settings_env_vars()at startupsrc/pflow/mcp_server/main.pyinject_settings_env_vars()at startupsrc/pflow/nodes/llm/llm.pytests/test_core/test_llm_config_provider_detection.py.taskmaster/bugfix/bugfix-log.mdCreated Docs
scratchpads/robust-provider-detection/implementation-spec.md- Comprehensive implementation specTesting
Manual Verification