{"record":{"id":"19634eb2162f166e","repo":"JuliusBrussee/caveman","slug":"install-caveman-middleware-anthropic-to-use-the-anthropic","errorCode":null,"errorMessage":"Install caveman-middleware[anthropic] to use the Anthropic adapter","messagePattern":"Install caveman-middleware\\[anthropic\\] to use the Anthropic adapter","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"packages/middleware/python/caveman_middleware/anthropic.py","lineNumber":15,"sourceCode":"\"\"\"Anthropic 1.4 public per-attempt middleware and native tool runners.\"\"\"\nfrom __future__ import annotations\n\nimport functools\nimport asyncio\nimport json\nimport uuid\nfrom dataclasses import dataclass\nfrom urllib.parse import urlsplit\n\ntry:\n    from anthropic import Anthropic, AsyncAnthropic, Middleware, APIRequest, __version__\n    from anthropic.lib.tools import BetaBuiltinFunctionTool, BetaAsyncBuiltinFunctionTool\nexcept ModuleNotFoundError as error:\n    raise ImportError(\"Install caveman-middleware[anthropic] to use the Anthropic adapter\") from error\n\nfrom caveman_cloud.middleware import MiddlewareRuntime, AsyncMiddlewareRuntime\nfrom ._httpx2 import observe_response\nfrom ._native import NativeSession, owner, plain\nfrom ._versions import in_range\n\n\nclass CavemanAnthropicMiddleware(Middleware):\n    \"\"\"Append after the application's authorization and original-content guards.\"\"\"\n    def __init__(self, runtime, scope, *, _binding=None, _overhead=None, _logical_call_id=None, _is_registered=None):\n        self.session = NativeSession(runtime, scope, adapter_id=\"anthropic-sdk\", framework_version=\"1.4.0\", protocol=\"anthropic-messages\",\n                                     binding=_binding, overhead=_overhead, logical_call_id=_logical_call_id, is_registered=_is_registered,\n                                     passive_reason=None if in_range(__version__, \"1.4\", \"2\") else \"unsupported_version\")\n\n    @staticmethod\n    def eligible(request: APIRequest):\n        if request.method.upper() != \"POST\" or urlsplit(request.url).path not in (\"/v1/messages\", \"/messages\"):\n            return False","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/middleware/python/caveman_middleware/anthropic.py#L1-L33","documentation":"The Anthropic adapter is an optional extra: it imports the `anthropic` package (Anthropic, AsyncAnthropic, Middleware, etc.) at module import time. If the package is missing, the module raises ImportError telling you to install the caveman-middleware[anthropic] extra.","triggerScenarios":"Importing caveman_middleware.anthropic (or a module that imports it) without the anthropic dependency installed.","commonSituations":"Fresh environment installed with plain `pip install caveman-middleware` (no extras); CI image missing optional dependencies; switching code paths to Anthropic without updating requirements.","solutions":["Install the extra: pip install 'caveman-middleware[anthropic]'","Add the extra to your requirements/pyproject dependencies","If anthropic is present but fails to import for another reason, inspect the chained exception (__cause__) for the real ModuleNotFoundError"],"exampleFix":"// before\npip install caveman-middleware\n// after\npip install \"caveman-middleware[anthropic]\"","handlingStrategy":"fallback","validationCode":"try:\n    import anthropic  # noqa: F401\n    ANTHROPIC_AVAILABLE = True\nexcept ModuleNotFoundError:\n    ANTHROPIC_AVAILABLE = False","typeGuard":null,"tryCatchPattern":"try:\n    from caveman_middleware import anthropic as caveman_anthropic\nexcept ImportError as e:\n    if 'caveman-middleware[anthropic]' in str(e):\n        caveman_anthropic = None  # degrade to non-Anthropic path\n    else:\n        raise","preventionTips":["Install all adapter extras in every environment: pip install 'caveman-middleware[anthropic]'","Pin the extra in requirements/pyproject","Feature-flag Anthropic paths on dependency availability"],"tags":["python","import","optional-dependency","anthropic"],"backgroundTag":"missing-optional-dependency","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}