Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/cai/agents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,20 @@ def get_available_agents() -> Dict[str, Agent]: # pylint: disable=R0912 # noqa
agent_name = attr_name
if agent_name not in agents_to_display:
agents_to_display[agent_name] = attr
except (ImportError, AttributeError):
pass
except Exception as e:
# Handle missing API key errors gracefully
error_str = str(e)
module_short_name = name.split('.')[-1]

if "api_key" in error_str and "client option" in error_str:
print(f"\n[!] Warning: Agent '{module_short_name}' skipped: Missing OpenAI API Key.")
print(" Set the OPENAI_API_KEY environment variable to enable it.")
elif isinstance(e, (ImportError, AttributeError)):
# Ignore standard import errors
pass
else:
# Log other unexpected errors
print(f"Error importing {module_short_name}: {e}")

# Also check the patterns subdirectory
patterns_path = os.path.join(os.path.dirname(__file__), "patterns")
Expand All @@ -119,10 +131,19 @@ def get_available_agents() -> Dict[str, Agent]: # pylint: disable=R0912 # noqa
agent_name = attr_name
if agent_name not in agents_to_display:
agents_to_display[agent_name] = attr
except (ImportError, AttributeError) as e:
except Exception as e:
# Extract module name from the full import path
module_short_name = name.split('.')[-1]
print(f"Error importing {module_short_name}: {e}")

error_str = str(e)
if "api_key" in error_str and "client option" in error_str:
print(f"\n[!] Warning: Pattern '{module_short_name}' skipped: Missing OpenAI API Key.")
print(" Set the OPENAI_API_KEY environment variable to enable it.")
elif isinstance(e, (ImportError, AttributeError)):
# Ignore standard import errors, or print if critical
pass
else:
print(f"Error importing {module_short_name}: {e}")

# Add all patterns (parallel, swarm, etc.) as pseudo-agents
from cai.agents.patterns import PATTERNS
Expand Down Expand Up @@ -172,7 +193,8 @@ def get_agent_module(agent_name: str) -> str:
# Try both with and without _agent suffix
if (attr_name == agent_name) and isinstance(getattr(module, attr_name), Agent):
return name
except (ImportError, AttributeError):
except (ImportError, AttributeError, Exception):
# If a module fails to load (e.g. API key missing), just skip it
pass

# Also check the patterns subdirectory
Expand All @@ -186,7 +208,7 @@ def get_agent_module(agent_name: str) -> str:
# Try both with and without _agent suffix
if (attr_name == agent_name) and isinstance(getattr(module, attr_name), Agent):
return name
except (ImportError, AttributeError):
except (ImportError, AttributeError, Exception):
pass

return "unknown"
Expand Down