{"record":{"id":"45cd3df64bee0956","repo":"microsoft/autogen","slug":"interactiveservice-is-not-initialized","errorCode":null,"errorMessage":"InteractiveService is not initialized.","messagePattern":"InteractiveService is not initialized\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.DotnetInteractive/DotnetInteractiveFunction.cs","lineNumber":77,"sourceCode":"            var document = new InteractiveDocument();\n\n            using var stream = File.OpenWrite(_notebookPath);\n            Notebook.Write(document, stream, this._kernelInfoCollection);\n            stream.Flush();\n            stream.Dispose();\n        }\n    }\n\n    /// <summary>\n    /// Run existing dotnet code from message. Don't modify the code, run it as is.\n    /// </summary>\n    /// <param name=\"code\">code.</param>\n    [Function]\n    public async Task<string> RunCode(string code)\n    {\n        if (this._interactiveService == null)\n        {\n            throw new ArgumentException(\"InteractiveService is not initialized.\");\n        }\n\n        var result = await this._interactiveService.SubmitCSharpCodeAsync(code, default);\n        if (result != null)\n        {\n            // if result contains Error, return entire message\n            if (result.StartsWith(\"Error:\"))\n            {\n                return result;\n            }\n\n            // add cell if _notebookPath is not null\n            if (this._notebookPath != null)\n            {\n                await AddCellAsync(code, \"csharp\");\n            }\n\n            // if result is over 100 characters, only return the first 100 characters.","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.DotnetInteractive/DotnetInteractiveFunction.cs#L59-L95","documentation":"Thrown by DotnetInteractiveFunction.RunCode when its internal _interactiveService field is null. The function was constructed without an InteractiveService, so there is no kernel to submit the C# code to, and it throws ArgumentException('InteractiveService is not initialized.').","triggerScenarios":"Creating DotnetInteractiveFunction via a constructor/path that leaves _interactiveService null, then invoking the RunCode tool (directly or through an LLM function call).","commonSituations":"Partially initializing the function object (e.g. for testing), dependency-injection misconfiguration, or constructing the function before the interactive session is available and forgetting to attach it.","solutions":["Construct DotnetInteractiveFunction with a live InteractiveService (or however the public API requires it) before registering it as a tool","Call and await service.StartAsync(workingDirectory) first so the kernel is actually running","Verify the service is non-null and started in a startup check before the agent loop begins"],"exampleFix":"// before: var func = new DotnetInteractiveFunction(); // no service\n// after:\nvar service = new InteractiveService(installDir);\nawait service.StartAsync(workDir);\nvar func = new DotnetInteractiveFunction(service);","handlingStrategy":"validation","validationCode":"// Verify the function is backed by a running service before the agent loop starts\nif (dotnetInteractiveFunction is null || !IsServiceAttached(dotnetInteractiveFunction))\n    throw new InvalidOperationException(\"DotnetInteractiveFunction lacks an InteractiveService\");\n// simplest robust check: construct it yourself from a started service","typeGuard":null,"tryCatchPattern":"try { var result = await func.RunCode(code); }\ncatch (ArgumentException e) when (e.Message == \"InteractiveService is not initialized.\")\n{ /* construct function with a started InteractiveService and retry */ }","preventionTips":["Always build DotnetInteractiveFunction from a started InteractiveService","Assert Kernel != null at startup"],"tags":["autogen","dotnet-interactive","initialization"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}