This commit implements a clean architecture where environment variables are only read, never set, ensuring compatibility with other ComfyUI packages. The changes: - Remove all code that sets global environment variables - Clean up config.yaml to remove API key placeholders - Add documentation for safe environment handling - Add comprehensive tests to verify non-polluting behavior - Ensure backward compatibility for other packages The commit includes detailed docs explaining the clean architecture and safety guarantees for package compatibility.
6.9 KiB
Executable File
Environment Configuration for ComfyUI_LiteLLM
This document explains how to configure environment variables and API keys for ComfyUI_LiteLLM.
Important: This addon follows best practices and never pollutes the global environment. It only reads environment variables and won't interfere with other ComfyUI custom node packages.
Quick Setup
For Production ComfyUI Use
Set environment variables at the system level:
export OPENAI_API_KEY="your-openai-api-key"
export KLUSTER_API_KEY="your-kluster-api-key"
For Development/Testing Only
-
Copy the example environment file:
cp .env.example .env -
Edit
.envwith your actual API keys:nano .env # or use your preferred editor -
Set your API keys in the
.envfile:KLUSTER_API_KEY=your-actual-kluster-api-key-here OPENAI_API_KEY=your-actual-openai-api-key-here
Note: The .env file is only used when system environment variables are not available. This makes it perfect for testing without affecting production.
Environment Variables
API Keys (Set at System Level for Production)
OPENAI_API_KEY- OpenAI API keyANTHROPIC_API_KEY- Anthropic (Claude) API keyCOHERE_API_KEY- Cohere API keyKLUSTER_API_KEY- Your Kluster.ai API key
API Base URLs (Configured in config.yaml)
KLUSTER_BASE_URL- Kluster.ai API base URL (default: https://api.kluster.ai/v1)OPENAI_BASE_URL- OpenAI API base URL (default: https://api.openai.com/v1)
Test Configuration (Development Only)
RUN_INTEGRATION_TESTS- Set totrueto run integration tests (default: false)
Development Settings (Development Only)
DEBUG- Enable debug mode (default: false)LOG_LEVEL- Set logging level (default: INFO)
Security Best Practices
✅ DO
- Use system environment variables for production API keys
- Keep your
.envfile local and never commit it (development only) - Use the
.env.examplefile as a template - Rotate API keys regularly
- Use different keys for development and production
- Trust that this addon won't interfere with other ComfyUI packages
❌ DON'T
- Hardcode API keys in source code
- Put API keys in config.yaml files
- Commit
.envfiles to version control - Share API keys in plain text
- Use production keys for testing
- Worry about environment pollution - this addon is clean!
File Structure
ComfyUI_LiteLLM/
├── .env.example # Template for environment variables (dev/test only)
├── .env # Your actual environment variables (dev/test only, gitignored)
├── .gitignore # Ensures .env files are not committed
├── config.yaml # Non-sensitive configuration (base URLs, paths)
└── utils/
└── env_config.py # Safe environment reading utility
Architecture
This addon uses a clean, non-polluting architecture:
- System environment: Primary source for API keys in production
.envfile: Fallback for development/testing onlyconfig.yaml: Non-sensitive settings only (never API keys)- No global pollution: Never sets or modifies environment variables
Usage in Code
Loading Environment Variables
from utils.env_config import get_env_var, get_api_config
# Get a single environment variable
api_key = get_env_var('KLUSTER_API_KEY', required=True)
# Get all API configuration
config = get_api_config()
kluster_key = config['kluster']['api_key']
Kluster.ai Configuration
from utils.env_config import get_kluster_config
try:
config = get_kluster_config()
# Use config['api_key'], config['base_url'], config['model']
except ValueError as e:
print(f"Kluster configuration error: {e}")
Testing
Running Tests with Environment Variables
# Set environment variables and run tests
export KLUSTER_API_KEY="your-key-here"
python -m pytest tests/
# Or use .env file (recommended)
echo "KLUSTER_API_KEY=your-key-here" > .env
python -m pytest tests/
Skipping Tests Without API Keys
Tests will automatically skip if required API keys are not configured:
SKIPPED [1] Kluster API key not configured. Set KLUSTER_API_KEY environment variable.
Integration Tests
Set RUN_INTEGRATION_TESTS=true in your .env file to enable tests that make real API calls:
RUN_INTEGRATION_TESTS=true
KLUSTER_API_KEY=your-actual-key
Troubleshooting
Common Issues
-
"Kluster API key not configured"
- Ensure
KLUSTER_API_KEYis set in your environment or.envfile - Check that the
.envfile is in the project root directory
- Ensure
-
"Tests are skipped"
- This is normal behavior when API keys are not configured
- Add the required API keys to run integration tests
-
"Environment file not found"
- Create a
.envfile based on.env.example - Ensure the file is in the correct location (project root)
- Create a
Debug Environment Loading
from utils.env_config import load_env_file
import os
# Check what's loaded from .env file
env_vars = load_env_file()
print("Loaded from .env:", env_vars)
# Check what's in the actual environment
print("KLUSTER_API_KEY in env:", os.environ.get('KLUSTER_API_KEY'))
Migration from Hardcoded Keys
If you're migrating from an older version with hardcoded API keys:
-
Find hardcoded keys in your code:
grep -r "46b1a513-72b6-4549-aac3-0d2f8ae16db9" . -
Replace with environment variables:
# Old (insecure) api_key = "46b1a513-72b6-4549-aac3-0d2f8ae16db9" # New (secure) api_key = os.environ.get('KLUSTER_API_KEY') -
Add keys to .env file:
KLUSTER_API_KEY=46b1a513-72b6-4549-aac3-0d2f8ae16db9 -
Test the migration:
python -m pytest tests/ -v
Production Deployment
For production environments:
- Set system environment variables - Don't rely on
.envfiles in production - Use secrets management - Tools like HashiCorp Vault, AWS Secrets Manager, etc.
- Monitor key usage - Set up alerts for API key usage and errors
- Implement key rotation - Regularly rotate API keys
- Trust the clean architecture - This addon won't interfere with other packages
Example production setup:
# Set environment variables in your deployment/container
export KLUSTER_API_KEY="prod-key-here"
export OPENAI_API_KEY="prod-openai-key"
# Other ComfyUI packages can safely use the same environment variables
Compatibility with Other ComfyUI Packages
This addon is designed to be a good citizen in the ComfyUI ecosystem:
- ✅ Never modifies global environment variables
- ✅ Reads from system environment first,
.envsecond - ✅ Won't override API keys that other packages expect
- ✅ Uses namespaced internal variables when needed
- ✅ Safe to use alongside any other ComfyUI custom nodes
You can safely install this alongside other packages that use OpenAI, Anthropic, or other APIs without any conflicts.