fix: lazy-import anthropic SDK; tolerate LLM adapter failure in dry-run mode

--dry-run was crashing with ModuleNotFoundError because:
1. adapters/llm/anthropic.py imported 'anthropic' at module level
2. TeamRunner.__init__ always built the LLM adapter regardless of dry_run flag

Fixes:
- Move 'import anthropic' inside AnthropicAdapter.__init__ (lazy import)
  so the module loads cleanly without the SDK installed
- In TeamRunner.__init__, wrap _build_llm in a try/except when dry_run=True
  so missing adapter deps are logged as warnings, not fatal errors

dry-run now works with no third-party packages installed.
This commit is contained in:
2026-03-16 01:03:17 -04:00
parent 7b1cf7315c
commit 45e3b7663e
2 changed files with 21 additions and 4 deletions

View File

@@ -10,8 +10,6 @@ from __future__ import annotations
import os import os
import anthropic
from adapters.base.llm import LLMAdapter from adapters.base.llm import LLMAdapter
@@ -57,6 +55,14 @@ class AnthropicAdapter(LLMAdapter):
ValueError ValueError
If ANTHROPIC_API_KEY is not set in the environment. If ANTHROPIC_API_KEY is not set in the environment.
""" """
try:
import anthropic as _anthropic
except ModuleNotFoundError as exc:
raise ImportError(
"The 'anthropic' package is required for AnthropicAdapter. "
"Install it with: pip install anthropic"
) from exc
self._config = config self._config = config
api_key = os.environ.get("ANTHROPIC_API_KEY") api_key = os.environ.get("ANTHROPIC_API_KEY")
if not api_key: if not api_key:
@@ -64,7 +70,7 @@ class AnthropicAdapter(LLMAdapter):
"ANTHROPIC_API_KEY environment variable is not set. " "ANTHROPIC_API_KEY environment variable is not set. "
"Export it before running the-agency." "Export it before running the-agency."
) )
self._client = anthropic.Anthropic(api_key=api_key) self._client = _anthropic.Anthropic(api_key=api_key)
self._models_cfg: dict = config.get("models", {}) self._models_cfg: dict = config.get("models", {})
self._default_max_tokens: int = self._models_cfg.get("default_max_tokens", 4096) self._default_max_tokens: int = self._models_cfg.get("default_max_tokens", 4096)
self._default_temperature: float = self._models_cfg.get("default_temperature", 0) self._default_temperature: float = self._models_cfg.get("default_temperature", 0)

View File

@@ -177,7 +177,18 @@ class TeamRunner:
adapter_cfg: dict = self._config.get("adapters", {}) adapter_cfg: dict = self._config.get("adapters", {})
runtime_cfg: dict = self._config.get("runtime", {}) runtime_cfg: dict = self._config.get("runtime", {})
if dry_run:
# In dry-run mode the LLM adapter is never actually called, so we
# tolerate missing dependencies (e.g. 'anthropic' SDK not installed).
try:
self._llm: LLMAdapter = self._build_llm(adapter_cfg.get("llm", "anthropic")) self._llm: LLMAdapter = self._build_llm(adapter_cfg.get("llm", "anthropic"))
except (ImportError, ValueError) as exc:
logger.warning(
"LLM adapter unavailable in dry-run mode (%s) — continuing.", exc
)
self._llm = None # type: ignore[assignment]
else:
self._llm = self._build_llm(adapter_cfg.get("llm", "anthropic"))
self._vcs: Optional[VCSAdapter] = self._build_optional( # type: ignore[assignment] self._vcs: Optional[VCSAdapter] = self._build_optional( # type: ignore[assignment]
_VCS_ADAPTERS, adapter_cfg.get("vcs"), "VCS" _VCS_ADAPTERS, adapter_cfg.get("vcs"), "VCS"
) )