Awesome Reviewers

Reviewer · Performance Optimization

Defer heavy imports lazily

Expensive or optional dependencies (e.g. `litellm`, `mcp`) must not be imported at module/package load time if they add significant startup cost (import time, network calls, etc.). Measure and verify the impact (e.g. `import swarms` dropped from 1.15s to 0.63s and stopped fetching litellm's model-cost map over the network once these imports were deferred).

Instruction

Expensive or optional dependencies (e.g. litellm, mcp) must not be imported at module/package load time if they add significant startup cost (import time, network calls, etc.). Measure and verify the impact (e.g. import swarms dropped from 1.15s to 0.63s and stopped fetching litellm’s model-cost map over the network once these imports were deferred).

Apply the right deferral pattern for the situation:

  • For a dependency used in only one or a few functions, import it locally inside the function body rather than at module top-level.
  • For names used only in type annotations, import them under if TYPE_CHECKING: so they never execute at runtime.
  • For a dependency used across many call sites in a module (and potentially patched in tests), bind it lazily into module-level globals on first use, so existing call sites and test patches keep working unchanged:
litellm = None
completion = None

def _bind_litellm() -> None:
    global litellm, completion
    if litellm is None:
        import litellm as _litellm
        litellm = _litellm
        completion = _litellm.completion

Always leave a short comment at the deferred import explaining why (e.g. # Deferred: litellm must not load at import swarms time (#1754)) so the intent is clear to future readers and static analyzers. Note that static analysis tools unfamiliar with an environment’s installed packages may flag these as unresolved imports after being moved into function bodies — this is a tooling/environment limitation, not a defect, and should not be “fixed” by reverting the deferral.

More generally, avoid doing unnecessary work on hot/common paths: computations needed only for a specific optional feature (e.g. checkpointing) should be guarded by the same condition that uses them, not executed unconditionally on every iteration.</description> </invoke>