NAVADA Claude commited on
Commit
15cc492
·
1 Parent(s): c6ce1a6

Add graceful handling for missing OpenAI API key

Browse files

- Wrap OpenAI client initialization in try-catch block
- Provide clear error message and solution steps for HF Spaces
- Prevents application crash with helpful user guidance
- Users can now see exactly how to configure API key

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (1) hide show
  1. app.py +11 -2
app.py CHANGED
@@ -562,8 +562,17 @@ if api_key:
562
  langsmith_client = None
563
  print("INFO: LangSmith tracing disabled (no API key)")
564
  else:
565
- client = OpenAI() # Will use default OPENAI_API_KEY from environment
566
- langsmith_client = None
 
 
 
 
 
 
 
 
 
567
 
568
  # =============================
569
  # LANGSMITH THREAD MANAGEMENT
 
562
  langsmith_client = None
563
  print("INFO: LangSmith tracing disabled (no API key)")
564
  else:
565
+ # Try to create client with environment variable, but handle missing key gracefully
566
+ try:
567
+ client = OpenAI() # Will use default OPENAI_API_KEY from environment
568
+ langsmith_client = None
569
+ except Exception as e:
570
+ print(f"ERROR: OpenAI client initialization failed: {e}")
571
+ print("SOLUTION: Please set the OPENAI_API_KEY environment variable in your Hugging Face Space settings")
572
+ print("1. Go to your Space settings")
573
+ print("2. Add OPENAI_API_KEY as an environment variable")
574
+ print("3. Restart the Space")
575
+ raise SystemExit("Application cannot start without valid OpenAI API key")
576
 
577
  # =============================
578
  # LANGSMITH THREAD MANAGEMENT