Show debug info in chat when API key not found
Browse filesCo-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
app.py
CHANGED
|
@@ -551,24 +551,25 @@ STAGE_FOCUS = {
|
|
| 551 |
def get_client():
|
| 552 |
"""Initialize Anthropic client - try multiple possible secret names"""
|
| 553 |
api_key = (
|
|
|
|
| 554 |
os.environ.get("anthropic_key") or
|
| 555 |
os.environ.get("ANTHROPIC_KEY") or
|
| 556 |
-
os.environ.get("ANTHROPIC_API_KEY") or
|
| 557 |
os.environ.get("anthropic_api_key")
|
| 558 |
)
|
| 559 |
if not api_key:
|
| 560 |
-
|
| 561 |
-
relevant_vars = [k for k in os.environ.keys() if 'key' in k.lower() or 'api' in k.lower() or 'anthropic' in k.lower()]
|
| 562 |
-
raise ValueError(
|
| 563 |
-
f"Anthropic API key not found. Available relevant env vars: {relevant_vars}. "
|
| 564 |
-
"Please add a secret named ANTHROPIC_API_KEY in Space Settings > Repository secrets."
|
| 565 |
-
)
|
| 566 |
return anthropic.Anthropic(api_key=api_key)
|
| 567 |
|
| 568 |
def call_claude(system_prompt, messages, context=""):
|
| 569 |
"""Call Claude API with given prompts"""
|
| 570 |
client = get_client()
|
| 571 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 572 |
if context:
|
| 573 |
user_content = f"{context}\n\nUser message: {messages[-1]['content']}" if messages else context
|
| 574 |
else:
|
|
|
|
| 551 |
def get_client():
|
| 552 |
"""Initialize Anthropic client - try multiple possible secret names"""
|
| 553 |
api_key = (
|
| 554 |
+
os.environ.get("ANTHROPIC_API_KEY") or
|
| 555 |
os.environ.get("anthropic_key") or
|
| 556 |
os.environ.get("ANTHROPIC_KEY") or
|
|
|
|
| 557 |
os.environ.get("anthropic_api_key")
|
| 558 |
)
|
| 559 |
if not api_key:
|
| 560 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 561 |
return anthropic.Anthropic(api_key=api_key)
|
| 562 |
|
| 563 |
def call_claude(system_prompt, messages, context=""):
|
| 564 |
"""Call Claude API with given prompts"""
|
| 565 |
client = get_client()
|
| 566 |
|
| 567 |
+
if client is None:
|
| 568 |
+
# Show helpful message about which env vars we checked
|
| 569 |
+
checked = ["ANTHROPIC_API_KEY", "anthropic_key", "ANTHROPIC_KEY", "anthropic_api_key"]
|
| 570 |
+
available = [k for k in os.environ.keys() if 'key' in k.lower() or 'api' in k.lower() or 'anthrop' in k.lower()]
|
| 571 |
+
return f"API key not found. Checked: {checked}. Found env vars containing 'key'/'api'/'anthrop': {available}. Please add ANTHROPIC_API_KEY in Space Settings > Repository secrets, then restart the Space."
|
| 572 |
+
|
| 573 |
if context:
|
| 574 |
user_content = f"{context}\n\nUser message: {messages[-1]['content']}" if messages else context
|
| 575 |
else:
|