File size: 5,958 Bytes
4e20ec4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python3
"""
Environment Variables Debug Script for HF Spaces
Helps diagnose the 'str expected, not NoneType' error
"""

import os
import sys
from pathlib import Path

def check_environment():
    """Check all required environment variables and configurations"""
    print("πŸ” AgentGraph Environment Debug Report")
    print("=" * 50)
    print()
    
    # Critical environment variables
    env_vars = {
        "OPENAI_API_KEY": "❌ REQUIRED - OpenAI API access",
        "LANGFUSE_PUBLIC_KEY": "🟑 Optional - AI monitoring",
        "LANGFUSE_SECRET_KEY": "🟑 Optional - AI monitoring", 
        "LANGFUSE_HOST": "🟑 Optional - AI monitoring",
        "OPENAI_MODEL_NAME": "🟑 Optional - defaults to gpt-4o-mini",
        "DB_URI": "🟑 Optional - defaults to SQLite",
        "PYTHONPATH": "πŸ”§ System variable",
        "HOME": "πŸ”§ System variable",
        "PATH": "πŸ”§ System variable"
    }
    
    print("πŸ“‹ Environment Variables Check:")
    print("-" * 30)
    missing_critical = []
    
    for var, description in env_vars.items():
        value = os.getenv(var)
        if value is None:
            status = "❌ NOT SET"
            if "REQUIRED" in description:
                missing_critical.append(var)
        elif value == "":
            status = "⚠️ EMPTY"
            if "REQUIRED" in description:
                missing_critical.append(var)
        else:
            # Mask sensitive values
            if "KEY" in var or "SECRET" in var:
                masked_value = f"{value[:8]}..." if len(value) > 8 else "***"
                status = f"βœ… SET ({masked_value})"
            else:
                status = f"βœ… SET ({value[:50]}...)" if len(value) > 50 else f"βœ… SET ({value})"
        
        print(f"  {var:20} | {status:25} | {description}")
    
    print()
    
    # Check critical paths and files
    print("πŸ“ Critical Paths Check:")
    print("-" * 25)
    
    paths_to_check = [
        ("Current directory", Path.cwd()),
        ("utils/config.py", Path("utils/config.py")),
        ("backend/app.py", Path("backend/app.py")),
        ("main.py", Path("main.py")),
        ("pyproject.toml", Path("pyproject.toml"))
    ]
    
    for name, path in paths_to_check:
        if path.exists():
            status = "βœ… EXISTS"
        else:
            status = "❌ MISSING"
        print(f"  {name:20} | {status:15} | {path}")
    
    print()
    
    # Python path check
    print("🐍 Python Environment:")
    print("-" * 20)
    print(f"  Python version: {sys.version}")
    print(f"  Python path: {sys.executable}")
    print(f"  Working directory: {os.getcwd()}")
    print(f"  PYTHONPATH: {os.getenv('PYTHONPATH', 'NOT SET')}")
    
    print()
    
    # Try importing critical modules
    print("πŸ“¦ Import Test:")
    print("-" * 15)
    
    modules_to_test = [
        "utils.config",
        "backend.app", 
        "fastapi",
        "uvicorn",
        "pydantic",
        "sqlalchemy"
    ]
    
    for module in modules_to_test:
        try:
            __import__(module)
            print(f"  {module:20} | βœ… OK")
        except ImportError as e:
            print(f"  {module:20} | ❌ FAILED: {str(e)}")
        except Exception as e:
            print(f"  {module:20} | ⚠️ ERROR: {str(e)}")
    
    print()
    
    # Summary
    print("πŸ“Š Summary:")
    print("-" * 10)
    
    if missing_critical:
        print(f"❌ CRITICAL: Missing required environment variables: {', '.join(missing_critical)}")
        print("πŸ”§ Fix: Set these variables in HF Spaces Settings > Environment variables")
        return False
    else:
        print("βœ… All critical environment variables are set")
        
        # Try to identify the specific error
        print()
        print("πŸ” Specific Error Analysis:")
        print("The 'str expected, not NoneType' error suggests:")
        print("1. A string parameter is being passed None instead of a string")
        print("2. Most likely in configuration or initialization code")
        print("3. Check Pydantic model validation or string concatenation")
        
        return True

def test_config_import():
    """Test importing and using the config module"""
    print()
    print("πŸ§ͺ Config Module Test:")
    print("-" * 20)
    
    try:
        from utils.config import OPENAI_API_KEY, validate_config
        
        print(f"OPENAI_API_KEY loaded: {'βœ… YES' if OPENAI_API_KEY else '❌ NO'}")
        
        # Test validation
        is_valid = validate_config()
        print(f"Config validation: {'βœ… PASSED' if is_valid else '❌ FAILED'}")
        
        return is_valid
        
    except Exception as e:
        print(f"❌ Config import failed: {str(e)}")
        return False

if __name__ == "__main__":
    print("Starting environment debug check...")
    print()
    
    # Basic environment check
    env_ok = check_environment()
    
    # Config test
    config_ok = test_config_import()
    
    print()
    print("🎯 RECOMMENDATIONS:")
    print("=" * 20)
    
    if not env_ok:
        print("1. ❌ Set missing OPENAI_API_KEY in HF Spaces environment variables")
        print("2. πŸ”— Go to: https://huggingface.co/spaces/holistic-ai/AgentGraph/settings")
        print("3. πŸ“ Add: OPENAI_API_KEY = your_openai_api_key")
    
    if not config_ok:
        print("4. πŸ”§ Check utils/config.py for import issues")
        print("5. 🐍 Verify Python dependencies are installed correctly")
    
    if env_ok and config_ok:
        print("βœ… Environment looks good!")
        print("πŸ” The 'str expected, not NoneType' error may be in:")
        print("   β€’ Pydantic model validation")
        print("   β€’ String formatting/concatenation")
        print("   β€’ Database connection string")
        print("   β€’ FastAPI configuration")
        
    print()
    print("πŸ“Š Run this script in HF Spaces to get the exact error location!")