{"record":{"id":"d60731baaaec8c8e","repo":"infiniflow/ragflow","slug":"provider-not-initialized-call-initialize-first","errorCode":null,"errorMessage":"Provider not initialized. Call initialize() first.","messagePattern":"Provider not initialized\\. Call initialize\\(\\) first\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"agent/sandbox/providers/aliyun_codeinterpreter.py","lineNumber":144,"sourceCode":"        except Exception as e:\n            logger.error(f\"Aliyun Code Interpreter: Initialization failed - {str(e)}\")\n            return False\n\n    def create_instance(self, template: str = \"python\") -> SandboxInstance:\n        \"\"\"\n        Create a new sandbox instance in Aliyun Code Interpreter.\n\n        Args:\n            template: Programming language (python, javascript)\n\n        Returns:\n            SandboxInstance object\n\n        Raises:\n            RuntimeError: If instance creation fails\n        \"\"\"\n        if not self._initialized or not self._config:\n            raise RuntimeError(\"Provider not initialized. Call initialize() first.\")\n\n        # Normalize language\n        language = self._normalize_language(template)\n\n        try:\n            # Get or create template\n            if self.template_name:\n                # Use existing template\n                template_name = self.template_name\n            else:\n                # Try to get default template, or create one if it doesn't exist\n                default_template_name = f\"ragflow-{language}-default\"\n                try:\n                    # Check if template exists\n                    Template.get_by_name(default_template_name, config=self._config)\n                    template_name = default_template_name\n                except Exception:\n                    # Create default template if it doesn't exist","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/infiniflow/ragflow/blob/554fb1133ac3861732235ad9c377eb5e0a770665/agent/sandbox/providers/aliyun_codeinterpreter.py#L126-L162","documentation":"Guard in AliyunCodeInterpreterProvider.create_instance: the provider was used before a successful `initialize()` (which sets `_initialized` and `_config`). Every public provider method enforces this precondition, so calling any of them on a fresh or failed-to-initialize instance raises this RuntimeError.","triggerScenarios":"Instantiating the provider and immediately calling create_instance(); initialize() raised earlier (bad credentials/region) and the exception was swallowed, leaving `_initialized` False; reusing a provider after some reset path.","commonSituations":"Missing or malformed Aliyun sandbox config (AK/SK, region, account id) so initialize failed silently upstream; wiring code that caches a provider object but skips the init step on cache hit paths; tests constructing the provider directly.","solutions":["Call and await/check `provider.initialize(config)` before create_instance, and treat a False/exception result as fatal rather than continuing.","Inspect why initialize failed (credentials, region, endpoint) — the provider stays uninitialized after any init error.","Gate calls behind `health_check()` if the base class exposes it, or assert `_initialized` in debug builds."],"exampleFix":"# before\nprovider = AliyunCodeInterpreterProvider(...)\ninstance = provider.create_instance(\"python\")\n\n# after\nprovider = AliyunCodeInterpreterProvider(...)\nif not provider.initialize(config):\n    raise RuntimeError(\"Aliyun sandbox provider failed to initialize; check credentials/region\")\ninstance = provider.create_instance(\"python\")","handlingStrategy":"validation","validationCode":"if not provider.initialize(config):\n    raise RuntimeError(\"Aliyun provider init failed; refusing to create instances\")\n# only now:\ninstance = provider.create_instance(\"python\")","typeGuard":"def is_ready(p) -> bool:\n    \"\"\"True when the provider completed initialize() and holds a config.\"\"\"\n    return bool(getattr(p, \"_initialized\", False)) and getattr(p, \"_config\", None) is not None","tryCatchPattern":"try:\n    provider.create_instance(\"python\")\nexcept RuntimeError as e:\n    if \"not initialized\" in str(e):\n        provider.initialize(config)\n        instance = provider.create_instance(\"python\")\n    else:\n        raise","preventionTips":["Centralize provider construction: one factory that initializes and returns only ready providers.","Never swallow initialize() exceptions.","Re-initialize providers after process restarts or config reloads."],"tags":["provider","lifecycle","aliyun","initialization"],"backgroundTag":null,"analyzedSha":"554fb1133ac3861732235ad9c377eb5e0a770665","analyzedAt":"2026-08-15T09:20:16.380Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}