Spaces:
Sleeping
Sleeping
Commit
Β·
4e20ec4
1
Parent(s):
7348bb7
π Add environment debug script
Browse filesHelps diagnose 'str expected, not NoneType' startup error:
β’ Checks all required environment variables
β’ Tests critical module imports
β’ Identifies missing OPENAI_API_KEY as root cause
β’ Provides specific HF Spaces setup instructions
Usage: python debug_env.py
- debug_env.py +185 -0
debug_env.py
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Environment Variables Debug Script for HF Spaces
|
| 4 |
+
Helps diagnose the 'str expected, not NoneType' error
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
import sys
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
|
| 11 |
+
def check_environment():
|
| 12 |
+
"""Check all required environment variables and configurations"""
|
| 13 |
+
print("π AgentGraph Environment Debug Report")
|
| 14 |
+
print("=" * 50)
|
| 15 |
+
print()
|
| 16 |
+
|
| 17 |
+
# Critical environment variables
|
| 18 |
+
env_vars = {
|
| 19 |
+
"OPENAI_API_KEY": "β REQUIRED - OpenAI API access",
|
| 20 |
+
"LANGFUSE_PUBLIC_KEY": "π‘ Optional - AI monitoring",
|
| 21 |
+
"LANGFUSE_SECRET_KEY": "π‘ Optional - AI monitoring",
|
| 22 |
+
"LANGFUSE_HOST": "π‘ Optional - AI monitoring",
|
| 23 |
+
"OPENAI_MODEL_NAME": "π‘ Optional - defaults to gpt-4o-mini",
|
| 24 |
+
"DB_URI": "π‘ Optional - defaults to SQLite",
|
| 25 |
+
"PYTHONPATH": "π§ System variable",
|
| 26 |
+
"HOME": "π§ System variable",
|
| 27 |
+
"PATH": "π§ System variable"
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
print("π Environment Variables Check:")
|
| 31 |
+
print("-" * 30)
|
| 32 |
+
missing_critical = []
|
| 33 |
+
|
| 34 |
+
for var, description in env_vars.items():
|
| 35 |
+
value = os.getenv(var)
|
| 36 |
+
if value is None:
|
| 37 |
+
status = "β NOT SET"
|
| 38 |
+
if "REQUIRED" in description:
|
| 39 |
+
missing_critical.append(var)
|
| 40 |
+
elif value == "":
|
| 41 |
+
status = "β οΈ EMPTY"
|
| 42 |
+
if "REQUIRED" in description:
|
| 43 |
+
missing_critical.append(var)
|
| 44 |
+
else:
|
| 45 |
+
# Mask sensitive values
|
| 46 |
+
if "KEY" in var or "SECRET" in var:
|
| 47 |
+
masked_value = f"{value[:8]}..." if len(value) > 8 else "***"
|
| 48 |
+
status = f"β
SET ({masked_value})"
|
| 49 |
+
else:
|
| 50 |
+
status = f"β
SET ({value[:50]}...)" if len(value) > 50 else f"β
SET ({value})"
|
| 51 |
+
|
| 52 |
+
print(f" {var:20} | {status:25} | {description}")
|
| 53 |
+
|
| 54 |
+
print()
|
| 55 |
+
|
| 56 |
+
# Check critical paths and files
|
| 57 |
+
print("π Critical Paths Check:")
|
| 58 |
+
print("-" * 25)
|
| 59 |
+
|
| 60 |
+
paths_to_check = [
|
| 61 |
+
("Current directory", Path.cwd()),
|
| 62 |
+
("utils/config.py", Path("utils/config.py")),
|
| 63 |
+
("backend/app.py", Path("backend/app.py")),
|
| 64 |
+
("main.py", Path("main.py")),
|
| 65 |
+
("pyproject.toml", Path("pyproject.toml"))
|
| 66 |
+
]
|
| 67 |
+
|
| 68 |
+
for name, path in paths_to_check:
|
| 69 |
+
if path.exists():
|
| 70 |
+
status = "β
EXISTS"
|
| 71 |
+
else:
|
| 72 |
+
status = "β MISSING"
|
| 73 |
+
print(f" {name:20} | {status:15} | {path}")
|
| 74 |
+
|
| 75 |
+
print()
|
| 76 |
+
|
| 77 |
+
# Python path check
|
| 78 |
+
print("π Python Environment:")
|
| 79 |
+
print("-" * 20)
|
| 80 |
+
print(f" Python version: {sys.version}")
|
| 81 |
+
print(f" Python path: {sys.executable}")
|
| 82 |
+
print(f" Working directory: {os.getcwd()}")
|
| 83 |
+
print(f" PYTHONPATH: {os.getenv('PYTHONPATH', 'NOT SET')}")
|
| 84 |
+
|
| 85 |
+
print()
|
| 86 |
+
|
| 87 |
+
# Try importing critical modules
|
| 88 |
+
print("π¦ Import Test:")
|
| 89 |
+
print("-" * 15)
|
| 90 |
+
|
| 91 |
+
modules_to_test = [
|
| 92 |
+
"utils.config",
|
| 93 |
+
"backend.app",
|
| 94 |
+
"fastapi",
|
| 95 |
+
"uvicorn",
|
| 96 |
+
"pydantic",
|
| 97 |
+
"sqlalchemy"
|
| 98 |
+
]
|
| 99 |
+
|
| 100 |
+
for module in modules_to_test:
|
| 101 |
+
try:
|
| 102 |
+
__import__(module)
|
| 103 |
+
print(f" {module:20} | β
OK")
|
| 104 |
+
except ImportError as e:
|
| 105 |
+
print(f" {module:20} | β FAILED: {str(e)}")
|
| 106 |
+
except Exception as e:
|
| 107 |
+
print(f" {module:20} | β οΈ ERROR: {str(e)}")
|
| 108 |
+
|
| 109 |
+
print()
|
| 110 |
+
|
| 111 |
+
# Summary
|
| 112 |
+
print("π Summary:")
|
| 113 |
+
print("-" * 10)
|
| 114 |
+
|
| 115 |
+
if missing_critical:
|
| 116 |
+
print(f"β CRITICAL: Missing required environment variables: {', '.join(missing_critical)}")
|
| 117 |
+
print("π§ Fix: Set these variables in HF Spaces Settings > Environment variables")
|
| 118 |
+
return False
|
| 119 |
+
else:
|
| 120 |
+
print("β
All critical environment variables are set")
|
| 121 |
+
|
| 122 |
+
# Try to identify the specific error
|
| 123 |
+
print()
|
| 124 |
+
print("π Specific Error Analysis:")
|
| 125 |
+
print("The 'str expected, not NoneType' error suggests:")
|
| 126 |
+
print("1. A string parameter is being passed None instead of a string")
|
| 127 |
+
print("2. Most likely in configuration or initialization code")
|
| 128 |
+
print("3. Check Pydantic model validation or string concatenation")
|
| 129 |
+
|
| 130 |
+
return True
|
| 131 |
+
|
| 132 |
+
def test_config_import():
|
| 133 |
+
"""Test importing and using the config module"""
|
| 134 |
+
print()
|
| 135 |
+
print("π§ͺ Config Module Test:")
|
| 136 |
+
print("-" * 20)
|
| 137 |
+
|
| 138 |
+
try:
|
| 139 |
+
from utils.config import OPENAI_API_KEY, validate_config
|
| 140 |
+
|
| 141 |
+
print(f"OPENAI_API_KEY loaded: {'β
YES' if OPENAI_API_KEY else 'β NO'}")
|
| 142 |
+
|
| 143 |
+
# Test validation
|
| 144 |
+
is_valid = validate_config()
|
| 145 |
+
print(f"Config validation: {'β
PASSED' if is_valid else 'β FAILED'}")
|
| 146 |
+
|
| 147 |
+
return is_valid
|
| 148 |
+
|
| 149 |
+
except Exception as e:
|
| 150 |
+
print(f"β Config import failed: {str(e)}")
|
| 151 |
+
return False
|
| 152 |
+
|
| 153 |
+
if __name__ == "__main__":
|
| 154 |
+
print("Starting environment debug check...")
|
| 155 |
+
print()
|
| 156 |
+
|
| 157 |
+
# Basic environment check
|
| 158 |
+
env_ok = check_environment()
|
| 159 |
+
|
| 160 |
+
# Config test
|
| 161 |
+
config_ok = test_config_import()
|
| 162 |
+
|
| 163 |
+
print()
|
| 164 |
+
print("π― RECOMMENDATIONS:")
|
| 165 |
+
print("=" * 20)
|
| 166 |
+
|
| 167 |
+
if not env_ok:
|
| 168 |
+
print("1. β Set missing OPENAI_API_KEY in HF Spaces environment variables")
|
| 169 |
+
print("2. π Go to: https://huggingface.co/spaces/holistic-ai/AgentGraph/settings")
|
| 170 |
+
print("3. π Add: OPENAI_API_KEY = your_openai_api_key")
|
| 171 |
+
|
| 172 |
+
if not config_ok:
|
| 173 |
+
print("4. π§ Check utils/config.py for import issues")
|
| 174 |
+
print("5. π Verify Python dependencies are installed correctly")
|
| 175 |
+
|
| 176 |
+
if env_ok and config_ok:
|
| 177 |
+
print("β
Environment looks good!")
|
| 178 |
+
print("π The 'str expected, not NoneType' error may be in:")
|
| 179 |
+
print(" β’ Pydantic model validation")
|
| 180 |
+
print(" β’ String formatting/concatenation")
|
| 181 |
+
print(" β’ Database connection string")
|
| 182 |
+
print(" β’ FastAPI configuration")
|
| 183 |
+
|
| 184 |
+
print()
|
| 185 |
+
print("π Run this script in HF Spaces to get the exact error location!")
|