From daee44d5835b8b01a0c7180d5f792ba56d56fafa Mon Sep 17 00:00:00 2001
From: westey <164392973+westey-m@users.noreply.github.com>
Date: Thu, 18 Sep 2025 16:22:06 +0100
Subject: [PATCH] .NET: Add provider sample for OpenAI Assistants (#812)
* Add provider sample for OpenAI Assistants
* Suggestions from pr
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Add deprecation warnings for OpenAI Assistants
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
---
dotnet/agent-framework-dotnet.slnx | 1 +
.../Agent_With_OpenAIAssistants.csproj | 16 +++++++
.../Agent_With_OpenAIAssistants/Program.cs | 42 +++++++++++++++++++
.../Agent_With_OpenAIAssistants/README.md | 16 +++++++
.../GettingStarted/AgentProviders/README.md | 1 +
.../AssistantClientResultExtensions.cs | 2 +-
6 files changed, 77 insertions(+), 1 deletion(-)
create mode 100644 dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIAssistants/Agent_With_OpenAIAssistants.csproj
create mode 100644 dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIAssistants/Program.cs
create mode 100644 dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIAssistants/README.md
diff --git a/dotnet/agent-framework-dotnet.slnx b/dotnet/agent-framework-dotnet.slnx
index e662856548..c9d8067e2d 100644
--- a/dotnet/agent-framework-dotnet.slnx
+++ b/dotnet/agent-framework-dotnet.slnx
@@ -33,6 +33,7 @@
+
diff --git a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIAssistants/Agent_With_OpenAIAssistants.csproj b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIAssistants/Agent_With_OpenAIAssistants.csproj
new file mode 100644
index 0000000000..27d3e4077f
--- /dev/null
+++ b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIAssistants/Agent_With_OpenAIAssistants.csproj
@@ -0,0 +1,16 @@
+
+
+
+ Exe
+ net9.0
+
+ enable
+ disable
+
+
+
+
+
+
+
+
diff --git a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIAssistants/Program.cs b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIAssistants/Program.cs
new file mode 100644
index 0000000000..989c5663ac
--- /dev/null
+++ b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIAssistants/Program.cs
@@ -0,0 +1,42 @@
+// Copyright (c) Microsoft. All rights reserved.
+
+// This sample shows how to create and use a simple AI agent with OpenAI Assistants as the backend.
+
+// WARNING: The Assistants API is deprecated and will be shut down.
+// For more information see the OpenAI documentation: https://platform.openai.com/docs/assistants/migration
+
+#pragma warning disable OPENAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
+
+using System;
+using Microsoft.Extensions.AI.Agents;
+using OpenAI;
+
+var apiKey = Environment.GetEnvironmentVariable("OPENAI_APIKEY") ?? throw new InvalidOperationException("OPENAI_APIKEY is not set.");
+var mmodel = Environment.GetEnvironmentVariable("OPENAI_MODEL") ?? "gpt-4o-mini";
+
+const string JokerName = "Joker";
+const string JokerInstructions = "You are good at telling jokes.";
+
+// Get a client to create/retrieve server side agents with.
+var assistantClient = new OpenAIClient(apiKey).GetAssistantClient();
+
+// You can create a server side assistant with the OpenAI SDK.
+var createResult = await assistantClient.CreateAssistantAsync(mmodel, new() { Name = JokerName, Instructions = JokerInstructions });
+
+// You can retrieve an already created server side assistant as an AIAgent.
+AIAgent agent1 = await assistantClient.GetAIAgentAsync(createResult.Value.Id);
+
+// You can also create a server side assistant and return it as an AIAgent directly.
+AIAgent agent2 = await assistantClient.CreateAIAgentAsync(
+ model: mmodel,
+ name: JokerName,
+ instructions: JokerInstructions);
+
+// You can invoke the agent like any other AIAgent.
+AgentThread thread = agent1.GetNewThread();
+Console.WriteLine(await agent1.RunAsync("Tell me a joke about a pirate.", thread));
+
+// Cleanup for sample purposes.
+await assistantClient.DeleteThreadAsync(thread.ConversationId);
+await assistantClient.DeleteAssistantAsync(agent1.Id);
+await assistantClient.DeleteAssistantAsync(agent2.Id);
diff --git a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIAssistants/README.md b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIAssistants/README.md
new file mode 100644
index 0000000000..22a4bae18c
--- /dev/null
+++ b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIAssistants/README.md
@@ -0,0 +1,16 @@
+# Prerequisites
+
+WARNING: The Assistants API is deprecated and will be shut down.
+For more information see the OpenAI documentation: https://platform.openai.com/docs/assistants/migration
+
+Before you begin, ensure you have the following prerequisites:
+
+- .NET 8.0 SDK or later
+- OpenAI API key
+
+Set the following environment variables:
+
+```powershell
+$env:OPENAI_APIKEY="*****" # Replace with your OpenAI API key
+$env:OPENAI_MODEL="gpt-4o-mini" # Optional, defaults to gpt-4o-mini
+```
diff --git a/dotnet/samples/GettingStarted/AgentProviders/README.md b/dotnet/samples/GettingStarted/AgentProviders/README.md
index 0b5fbf1a74..aabf587785 100644
--- a/dotnet/samples/GettingStarted/AgentProviders/README.md
+++ b/dotnet/samples/GettingStarted/AgentProviders/README.md
@@ -21,6 +21,7 @@ See the README.md for each sample for the prerequisites for that sample.
|[Creating an AIAgent with a custom implementation](./Agent_With_CustomImplementation/)|This sample demonstrates how to create an AIAgent with a custom implementation|
|[Creating an AIAgent with Ollama](./Agent_With_Ollama/)|This sample demonstrates how to create an AIAgent using Ollama as the underlying inference service|
|[Creating an AIAgent with ONNX](./Agent_With_ONNX/)|This sample demonstrates how to create an AIAgent using ONNX as the underlying inference service|
+|[Creating an AIAgent with OpenAI Assistants](./Agent_With_OpenAIAssistants/)|This sample demonstrates how to create an AIAgent using OpenAI Assistants as the underlying inference service.WARNING: The Assistants API is deprecated and will be shut down. For more information see the OpenAI documentation: https://platform.openai.com/docs/assistants/migration|
|[Creating an AIAgent with OpenAI ChatCompletion](./Agent_With_OpenAIChatCompletion/)|This sample demonstrates how to create an AIAgent using OpenAI ChatCompletion as the underlying inference service|
|[Creating an AIAgent with OpenAI Responses](./Agent_With_OpenAIResponses/)|This sample demonstrates how to create an AIAgent using OpenAI Responses as the underlying inference service|
diff --git a/dotnet/src/Microsoft.Extensions.AI.Agents.OpenAI/Extensions/AssistantClientResultExtensions.cs b/dotnet/src/Microsoft.Extensions.AI.Agents.OpenAI/Extensions/AssistantClientResultExtensions.cs
index 82db6f781b..79bd755ce4 100644
--- a/dotnet/src/Microsoft.Extensions.AI.Agents.OpenAI/Extensions/AssistantClientResultExtensions.cs
+++ b/dotnet/src/Microsoft.Extensions.AI.Agents.OpenAI/Extensions/AssistantClientResultExtensions.cs
@@ -25,7 +25,7 @@ public static class AssistantExtensions
throw new ArgumentNullException(nameof(assistantClientResult));
}
- return AsAIAgent(assistantClientResult, assistantClient, chatOptions);
+ return AsAIAgent(assistantClientResult.Value, assistantClient, chatOptions);
}
///