{"record":{"id":"1d55f2c39b9a7042","repo":"microsoft/semantic-kernel","slug":"aicontextproviders-with-aifunctions-are-not-suppor-1d55f2","errorCode":null,"errorMessage":"AIContextProviders with AIFunctions are not supported when Agent UseImmutableKernel setting is false.","messagePattern":"AIContextProviders with AIFunctions are not supported when Agent UseImmutableKernel setting is false\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Agents/OpenAI/OpenAIAssistantAgent.cs","lineNumber":155,"sourceCode":"        var internalOptions = options?.RunCreationOptions ?? (string.IsNullOrWhiteSpace(options?.AdditionalInstructions) ? null : new RunCreationOptions()\n        {\n            AdditionalInstructions = options?.AdditionalInstructions,\n        });\n\n        Kernel kernel = this.GetKernel(options);\n#pragma warning disable SKEXP0110, SKEXP0130 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.\n        if (this.UseImmutableKernel)\n        {\n            kernel = kernel.Clone();\n        }\n\n        // Get the context contributions from the AIContextProviders.\n        AIContext providersContext = await openAIAssistantAgentThread.AIContextProviders.ModelInvokingAsync(messages, cancellationToken).ConfigureAwait(false);\n\n        // Check for compatibility AIContextProviders and the UseImmutableKernel setting.\n        if (providersContext.AIFunctions is { Count: > 0 } && !this.UseImmutableKernel)\n        {\n            throw new InvalidOperationException(\"AIContextProviders with AIFunctions are not supported when Agent UseImmutableKernel setting is false.\");\n        }\n\n        kernel.Plugins.AddFromAIContext(providersContext, \"Tools\");\n#pragma warning restore SKEXP0110, SKEXP0130 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.\n\n        using var activity = ModelDiagnostics.StartAgentInvocationActivity(this.Id, this.GetDisplayName(), this.Description, kernel, messages);\n        List<ChatMessageContent>? chatMessageContents = activity is not null ? [] : null;\n\n        // Notify the thread of new messages and return them to the caller.\n        await foreach (var result in InternalInvokeAsync().ConfigureAwait(false))\n        {\n            yield return new(result, openAIAssistantAgentThread);\n            chatMessageContents?.Add(result);\n        }\n\n        activity?.SetAgentResponse(chatMessageContents);\n\n        async IAsyncEnumerable<ChatMessageContent> InternalInvokeAsync()","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/OpenAI/OpenAIAssistantAgent.cs#L137-L173","documentation":"In the streaming invoke path, if AIContextProviders contribute AIFunctions (Count > 0) while the agent's UseImmutableKernel is false, an InvalidOperationException is thrown. The functions would be added as plugins to a shared, mutable kernel, which is unsafe, so the combination is forbidden.","triggerScenarios":"Configuring an OpenAIAssistantAgent with one or more AIContextProvider instances that register AIFunctions, while leaving UseImmutableKernel at its default (false), then invoking the agent.","commonSituations":"Adding function-providing context providers without enabling immutable kernel cloning; assuming plugins can safely mutate the shared kernel.","solutions":["Set agent.UseImmutableKernel = true so the kernel is cloned per invocation before adding provider functions.","Or remove the AIFunction-providing AIContextProviders if you must keep UseImmutableKernel false.","Register the needed functions as normal kernel plugins instead of via AIContextProviders if cloning is undesirable."],"exampleFix":"// before\nvar agent = new OpenAIAssistantAgent(...) { /* UseImmutableKernel default false */ };\nthread.AIContextProviders.Add(providerWithFunctions);\n// after\nvar agent = new OpenAIAssistantAgent(...) { UseImmutableKernel = true };\nthread.AIContextProviders.Add(providerWithFunctions);","handlingStrategy":"validation","validationCode":"bool providersHaveFunctions = thread.AIContextProviders\n    .Any(p => p.GetFunctionsAsync(default).GetAwaiter().GetResult()?.Any() == true);\nif (providersHaveFunctions && !agent.UseImmutableKernel)\n    throw new InvalidOperationException(\"Enable UseImmutableKernel when providers contribute AIFunctions.\");","typeGuard":null,"tryCatchPattern":"try { await foreach (var r in agent.InvokeStreamingAsync(thread)) { } }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"AIContextProviders with AIFunctions\")) {\n    agent.UseImmutableKernel = true;\n}","preventionTips":["Set UseImmutableKernel = true whenever you use function-providing AIContextProviders.","Prefer registering shared tools on the kernel rather than via providers when cloning is unwanted.","Document the dependency between providers-with-functions and immutable kernel."],"tags":["openai","agent","configuration","ai-context"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}