{"record":{"id":"2ff2137827623704","repo":"headroomlabs-ai/headroom","slug":"any-llm-sdk-is-required-for-anyllmbackend-install","errorCode":null,"errorMessage":"any-llm-sdk is required for AnyLLMBackend. Install with: pip install 'any-llm-sdk[all]'","messagePattern":"any-llm-sdk is required for AnyLLMBackend\\. Install with: pip install 'any-llm-sdk\\[all\\]'","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"headroom/backends/anyllm.py","lineNumber":76,"sourceCode":"            return \"auto\"\n        if choice_type == \"any\":\n            return \"required\"\n        if choice_type == \"tool\":\n            return {\"type\": \"function\", \"function\": {\"name\": choice.get(\"name\", \"\")}}\n    return \"auto\"\n\n\nclass AnyLLMBackend(Backend):\n    \"\"\"Backend using any-llm for multi-provider support.\"\"\"\n\n    def __init__(\n        self,\n        provider: str = \"openai\",\n        api_key: str | None = None,\n        api_base: str | None = None,\n    ):\n        if not ANYLLM_AVAILABLE:\n            raise ImportError(\n                \"any-llm-sdk is required for AnyLLMBackend. \"\n                \"Install with: pip install 'any-llm-sdk[all]'\"\n            )\n\n        self.provider = provider.lower()\n        # Normalize empty-string overrides (e.g. an env var set to \"\") to None\n        # so provider defaults stay active instead of forwarding a blank value.\n        self.api_key = api_key or None\n        self.api_base = api_base or None\n\n        # Create the AnyLLM instance once and reuse. api_key/api_base are only\n        # forwarded when set so providers keep their own env-var defaults\n        # (e.g. OPENAI_API_KEY / OPENAI_BASE_URL) otherwise.\n        create_kwargs: dict[str, Any] = {}\n        if self.api_key is not None:\n            create_kwargs[\"api_key\"] = self.api_key\n        if self.api_base is not None:\n            create_kwargs[\"api_base\"] = self.api_base","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/headroomlabs-ai/headroom/blob/322425c43bffde1ed0b64fecf3cf5951565dd82b/headroom/backends/anyllm.py#L58-L94","documentation":"AnyLLMBackend.__init__ raises ImportError when the optional any-llm-sdk dependency is not installed (ANYLLM_AVAILABLE is falsy at import time of the module). The constructor fails fast instead of deferring to a confusing ModuleNotFoundError at first request. The message includes the exact pip extra needed.","triggerScenarios":"Instantiating AnyLLMBackend(provider=..., api_key=..., api_base=...) in an environment where 'import any_llm' failed — headroom-ai installed without the any-llm extra, or a slim Docker image.","commonSituations":"Using the multi-provider backend in the default slim Docker image, a venv where only 'headroom-ai' base deps were installed, or a lockfile that dropped the optional dependency after a refactor.","solutions":["Install the SDK with all provider extras: pip install 'any-llm-sdk[all]'.","If you know the single provider you need, install its narrower extra (e.g. 'any-llm-sdk[anthropic]') to keep images slim.","In Dockerfiles, add the install to the same layer as headroom-ai so the dependency is not stripped by a later slim stage.","Verify before constructing: python -c \"import any_llm\" should succeed."],"exampleFix":"# before\nbackend = AnyLLMBackend(provider=\"anthropic\")  # ImportError in slim env\n\n# after\n# requirements.txt:\n#   any-llm-sdk[all]\nbackend = AnyLLMBackend(provider=\"anthropic\")","handlingStrategy":"validation","validationCode":"def anyllm_available() -> bool:\n    try:\n        import any_llm  # noqa: F401\n        return True\n    except ImportError:\n        return False\n\nif not anyllm_available():\n    raise SystemExit(\"AnyLLMBackend requires: pip install 'any-llm-sdk[all]'\")","typeGuard":null,"tryCatchPattern":"try:\n    backend = AnyLLMBackend(provider=\"openai\")\nexcept ImportError as e:\n    raise SystemExit(f\"missing optional dependency: {e}; add any-llm-sdk[all] to the image\") from e","preventionTips":["Pin optional backend deps in the same lockfile/requirements set as headroom-ai.","Add a container healthcheck that instantiates each configured backend at startup.","Distinguish ImportError for missing optional deps from real code bugs by checking the message."],"tags":["python","import","optional-dependency","pip","backend"],"backgroundTag":null,"analyzedSha":"322425c43bffde1ed0b64fecf3cf5951565dd82b","analyzedAt":"2026-08-15T01:03:05.481Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}