{"record":{"id":"ae760ecb9d0796cf","repo":"crewAIInc/crewAI","slug":"client-not-initialized-ae760e","errorCode":null,"errorMessage":"Client not initialized","messagePattern":"Client not initialized","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"lib/crewai-tools/src/crewai_tools/tools/scrapegraph_scrape_tool/scrapegraph_scrape_tool.py","lineNumber":176,"sourceCode":"\n    def _run(\n        self,\n        **kwargs: Any,\n    ) -> Any:\n        website_url = kwargs.get(\"website_url\", self.website_url)\n        user_prompt = (\n            kwargs.get(\"user_prompt\", self.user_prompt)\n            or \"Extract the main content of the webpage\"\n        )\n\n        if not website_url:\n            raise ValueError(\"website_url is required\")\n\n        self._validate_url(website_url)\n\n        try:\n            if self._client is None:\n                raise RuntimeError(\"Client not initialized\")\n            return self._client.smartscraper(\n                website_url=website_url,\n                user_prompt=user_prompt,\n            )\n\n        except RateLimitError:\n            raise  # Re-raise rate limit errors\n        except Exception as e:\n            raise RuntimeError(f\"Scraping failed: {e!s}\") from e\n        finally:\n            # Always close the client\n            if self._client is not None:\n                self._client.close()\n","sourceCodeStart":158,"sourceCodeEnd":190,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/crewai-tools/src/crewai_tools/tools/scrapegraph_scrape_tool/scrapegraph_scrape_tool.py#L158-L190","documentation":"Raised as RuntimeError by ScrapegraphScrapeTool._run when self._client is None at call time. The client is created during tool initialization (model_post_init); if construction failed partway, the tool was built via __class__ deserialization that skips post-init, or the client was already closed by a previous run (the finally block closes it), _client will be None. Note: this check sits inside the try, so the message is re-wrapped only if not already a RuntimeError path — it propagates directly as 'Client not initialized'.","triggerScenarios":"Calling tool._run() after a previous invocation finished (the finally block calls self._client.close(), and depending on client implementation the reference may be invalidated); constructing the tool in a way that skips model_post_init; a partially failed __init__ that left _client unset.","commonSituations":"Reusing one ScrapegraphScrapeTool instance for multiple sequential calls in a long-lived crew; deserializing a tool from a config/dict; an exception during init that was swallowed by framework retry logic.","solutions":["Create a fresh ScrapegraphScrapeTool instance for each scraping session instead of reusing a spent one.","Verify the tool was constructed through its normal constructor (with api_key) so model_post_init ran and _client was set.","If reuse is required, re-initialize the client before the next call rather than calling _run on the stale instance."],"exampleFix":"# before\ntool = ScrapegraphScrapeTool(api_key=KEY)\nfor url in urls:\n    tool.run(website_url=url)  # client closed by prior run's finally block\n\n# after: fresh tool per call\nfor url in urls:\n    ScrapegraphScrapeTool(api_key=KEY).run(website_url=url)","handlingStrategy":"validation","validationCode":"def tool_ready(tool) -> bool:\n    client = getattr(tool, \"_client\", None)\n    return client is not None and hasattr(client, \"smartscraper\")","typeGuard":null,"tryCatchPattern":"try:\n    out = tool.run(website_url=url)\nexcept RuntimeError as e:\n    if \"Client not initialized\" in str(e):\n        tool = ScrapegraphScrapeTool(api_key=KEY)  # rebuild and retry once\n        out = tool.run(website_url=url)\n    else:\n        raise","preventionTips":["Prefer a fresh ScrapegraphScrapeTool per scraping session (the finally block closes the client).","Construct tools via their normal constructor so model_post_init runs.","Add a smoke-test scrape after tool creation to fail fast on broken init."],"tags":["lifecycle","scrapegraph","client-state","runtime-error"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}