Skip to content

Commit 4db3178

Browse files
author
cellwebb
committed
fix(cli): load API keys from environment for model switching
• Add API key loading from environment variables when switching models • Validate presence of required API keys before attempting model switch • Display helpful error messages when environment variables are missing • Apply consistent API key handling in both model add wizard and switch commands • Prevent model switching failures due to missing authentication credentials
1 parent e471e8b commit 4db3178

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

src/clippy/cli/commands/model.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Model management command handlers for interactive CLI mode."""
22

3+
import os
34
from typing import Literal
45

56
from rich.console import Console
@@ -444,9 +445,18 @@ def _handle_model_add_wizard(agent: ClippyAgent, console: Console) -> CommandRes
444445
# Update the agent directly without setting as default
445446
model_config, provider_config = get_model_config(display_name)
446447
if model_config and provider_config:
448+
# Load API key from environment for the new provider
449+
api_key = None
450+
if provider_config.api_key_env:
451+
api_key = os.getenv(provider_config.api_key_env)
452+
if not api_key:
453+
console.print(f"[red]✗ {provider_config.api_key_env} not found[/red]")
454+
return "continue"
455+
447456
success, msg = agent.switch_model(
448457
model=model_config.model_id,
449458
base_url=provider_config.base_url,
459+
api_key=api_key,
450460
provider_config=provider_config,
451461
)
452462
if success:
@@ -562,9 +572,21 @@ def _handle_model_switch(agent: ClippyAgent, console: Console, name: str) -> Com
562572
# Get the model configuration and update the agent
563573
model_config, provider_config = get_model_config(name)
564574
if model_config and provider_config:
575+
# Load API key from environment for the new provider
576+
api_key = None
577+
if provider_config.api_key_env:
578+
api_key = os.getenv(provider_config.api_key_env)
579+
if not api_key:
580+
console.print(
581+
f"[red]✗ {provider_config.api_key_env} not found in environment[/red]"
582+
)
583+
console.print("[dim]Set the environment variable and try again[/dim]")
584+
return "continue"
585+
565586
success, msg = agent.switch_model(
566587
model=model_config.model_id,
567588
base_url=provider_config.base_url,
589+
api_key=api_key,
568590
provider_config=provider_config,
569591
)
570592
if success:

0 commit comments

Comments
 (0)