-
Notifications
You must be signed in to change notification settings - Fork 1.4k
.NET: Add AsIChatClientWithStoredOutputDisabled for ProjectResponsesClient #4911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rogerbarreto
merged 2 commits into
microsoft:main
from
rogerbarreto:issues/use-serverstore-middleware
Mar 26, 2026
+230
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
dotnet/src/Microsoft.Agents.AI.AzureAI/ProjectResponsesClientExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
| using Microsoft.Extensions.AI; | ||
| using Microsoft.Shared.DiagnosticIds; | ||
| using Microsoft.Shared.Diagnostics; | ||
| using OpenAI.Responses; | ||
|
|
||
| namespace Azure.AI.Extensions.OpenAI; | ||
|
|
||
| /// <summary> | ||
| /// Provides extension methods for <see cref="ProjectResponsesClient"/> | ||
| /// to simplify the creation of AI agents that work with Azure AI services. | ||
| /// </summary> | ||
| [Experimental(DiagnosticIds.Experiments.AIOpenAIResponses)] | ||
| public static class ProjectResponsesClientExtensions | ||
| { | ||
| /// <summary> | ||
| /// Gets an <see cref="IChatClient"/> for use with this <see cref="ProjectResponsesClient"/> that does not store responses for later retrieval. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This corresponds to setting the "store" property in the JSON representation to false. | ||
| /// </remarks> | ||
| /// <param name="responseClient">The client.</param> | ||
| /// <param name="deploymentName">Optional deployment name (model) to use for requests.</param> | ||
| /// <param name="includeReasoningEncryptedContent"> | ||
| /// Includes an encrypted version of reasoning tokens in reasoning item outputs. | ||
| /// This enables reasoning items to be used in multi-turn conversations when using the Responses API statelessly | ||
| /// (like when the store parameter is set to false, or when an organization is enrolled in the zero data retention program). | ||
| /// Defaults to <see langword="true"/>. | ||
| /// </param> | ||
| /// <returns>An <see cref="IChatClient"/> that can be used to converse via the <see cref="ProjectResponsesClient"/> that does not store responses for later retrieval.</returns> | ||
| /// <exception cref="ArgumentNullException"><paramref name="responseClient"/> is <see langword="null"/>.</exception> | ||
| [Experimental(DiagnosticIds.Experiments.AgentsAIExperiments)] | ||
| public static IChatClient AsIChatClientWithStoredOutputDisabled(this ProjectResponsesClient responseClient, string? deploymentName = null, bool includeReasoningEncryptedContent = true) | ||
rogerbarreto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| return Throw.IfNull(responseClient) | ||
| .AsIChatClient(deploymentName) | ||
| .AsBuilder() | ||
| .ConfigureOptions(x => | ||
| { | ||
| var previousFactory = x.RawRepresentationFactory; | ||
| x.RawRepresentationFactory = state => | ||
| { | ||
| var responseOptions = previousFactory?.Invoke(state) as CreateResponseOptions ?? new CreateResponseOptions(); | ||
|
|
||
| responseOptions.StoredOutputEnabled = false; | ||
|
|
||
| if (includeReasoningEncryptedContent && | ||
| !responseOptions.IncludedProperties.Contains(IncludedResponseProperty.ReasoningEncryptedContent)) | ||
| { | ||
| responseOptions.IncludedProperties.Add(IncludedResponseProperty.ReasoningEncryptedContent); | ||
| } | ||
|
|
||
| return responseOptions; | ||
| }; | ||
| }) | ||
| .Build(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
dotnet/tests/Microsoft.Agents.AI.AzureAI.UnitTests/ProjectResponsesClientExtensionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| using System.Reflection; | ||
| using Azure.AI.Extensions.OpenAI; | ||
| using Microsoft.Extensions.AI; | ||
| using OpenAI.Responses; | ||
|
|
||
| namespace Microsoft.Agents.AI.AzureAI.UnitTests; | ||
|
|
||
| /// <summary> | ||
| /// Unit tests for the <see cref="ProjectResponsesClientExtensions"/> class. | ||
| /// </summary> | ||
| public sealed class ProjectResponsesClientExtensionsTests | ||
| { | ||
| private static ProjectResponsesClient CreateTestClient() | ||
| { | ||
| return new ProjectResponsesClient(new FakeAuthenticationTokenProvider()); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verify that AsIChatClientWithStoredOutputDisabled throws ArgumentNullException when client is null. | ||
| /// </summary> | ||
| [Fact] | ||
| public void AsIChatClientWithStoredOutputDisabled_WithNullClient_ThrowsArgumentNullException() | ||
| { | ||
| // Act & Assert | ||
| var exception = Assert.Throws<ArgumentNullException>(() => | ||
| ((ProjectResponsesClient)null!).AsIChatClientWithStoredOutputDisabled()); | ||
|
|
||
| Assert.Equal("responseClient", exception.ParamName); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verify that AsIChatClientWithStoredOutputDisabled wraps the original ProjectResponsesClient, | ||
| /// which remains accessible via the service chain. | ||
| /// </summary> | ||
| [Fact] | ||
| public void AsIChatClientWithStoredOutputDisabled_InnerResponsesClientIsAccessible() | ||
| { | ||
| // Arrange | ||
| var responseClient = CreateTestClient(); | ||
|
|
||
| // Act | ||
| var chatClient = responseClient.AsIChatClientWithStoredOutputDisabled(); | ||
|
|
||
| // Assert - the inner ProjectResponsesClient should be accessible via GetService | ||
| var innerClient = chatClient.GetService<ResponsesClient>(); | ||
| Assert.NotNull(innerClient); | ||
| Assert.Same(responseClient, innerClient); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verify that AsIChatClientWithStoredOutputDisabled with includeReasoningEncryptedContent false | ||
| /// wraps the original ProjectResponsesClient, which remains accessible via the service chain. | ||
| /// </summary> | ||
| [Fact] | ||
| public void AsIChatClientWithStoredOutputDisabled_WithIncludeReasoningFalse_InnerResponsesClientIsAccessible() | ||
| { | ||
| // Arrange | ||
| var responseClient = CreateTestClient(); | ||
|
|
||
| // Act | ||
| var chatClient = responseClient.AsIChatClientWithStoredOutputDisabled(includeReasoningEncryptedContent: false); | ||
|
|
||
| // Assert - the inner ProjectResponsesClient should be accessible via GetService | ||
| var innerClient = chatClient.GetService<ResponsesClient>(); | ||
| Assert.NotNull(innerClient); | ||
| Assert.Same(responseClient, innerClient); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verify that AsIChatClientWithStoredOutputDisabled with default parameter (includeReasoningEncryptedContent = true) | ||
| /// configures StoredOutputEnabled to false and includes ReasoningEncryptedContent in IncludedProperties. | ||
| /// </summary> | ||
| [Fact] | ||
| public void AsIChatClientWithStoredOutputDisabled_Default_ConfiguresStoredOutputDisabledWithReasoningEncryptedContent() | ||
| { | ||
| // Arrange | ||
| var responseClient = CreateTestClient(); | ||
|
|
||
| // Act | ||
| var chatClient = responseClient.AsIChatClientWithStoredOutputDisabled(); | ||
|
|
||
| // Assert | ||
| var createResponseOptions = GetCreateResponseOptionsFromPipeline(chatClient); | ||
| Assert.NotNull(createResponseOptions); | ||
| Assert.False(createResponseOptions.StoredOutputEnabled); | ||
| Assert.Contains(IncludedResponseProperty.ReasoningEncryptedContent, createResponseOptions.IncludedProperties); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verify that AsIChatClientWithStoredOutputDisabled with includeReasoningEncryptedContent explicitly set to true | ||
| /// configures StoredOutputEnabled to false and includes ReasoningEncryptedContent in IncludedProperties. | ||
| /// </summary> | ||
| [Fact] | ||
| public void AsIChatClientWithStoredOutputDisabled_WithIncludeReasoningTrue_ConfiguresStoredOutputDisabledWithReasoningEncryptedContent() | ||
| { | ||
| // Arrange | ||
| var responseClient = CreateTestClient(); | ||
|
|
||
| // Act | ||
| var chatClient = responseClient.AsIChatClientWithStoredOutputDisabled(includeReasoningEncryptedContent: true); | ||
|
|
||
| // Assert | ||
| var createResponseOptions = GetCreateResponseOptionsFromPipeline(chatClient); | ||
| Assert.NotNull(createResponseOptions); | ||
| Assert.False(createResponseOptions.StoredOutputEnabled); | ||
| Assert.Contains(IncludedResponseProperty.ReasoningEncryptedContent, createResponseOptions.IncludedProperties); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verify that AsIChatClientWithStoredOutputDisabled with includeReasoningEncryptedContent set to false | ||
| /// configures StoredOutputEnabled to false and does not include ReasoningEncryptedContent in IncludedProperties. | ||
| /// </summary> | ||
| [Fact] | ||
| public void AsIChatClientWithStoredOutputDisabled_WithIncludeReasoningFalse_ConfiguresStoredOutputDisabledWithoutReasoningEncryptedContent() | ||
| { | ||
| // Arrange | ||
| var responseClient = CreateTestClient(); | ||
|
|
||
| // Act | ||
| var chatClient = responseClient.AsIChatClientWithStoredOutputDisabled(includeReasoningEncryptedContent: false); | ||
|
|
||
| // Assert | ||
| var createResponseOptions = GetCreateResponseOptionsFromPipeline(chatClient); | ||
| Assert.NotNull(createResponseOptions); | ||
| Assert.False(createResponseOptions.StoredOutputEnabled); | ||
| Assert.DoesNotContain(IncludedResponseProperty.ReasoningEncryptedContent, createResponseOptions.IncludedProperties); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verify that AsIChatClientWithStoredOutputDisabled works with an optional deployment name. | ||
| /// </summary> | ||
| [Fact] | ||
| public void AsIChatClientWithStoredOutputDisabled_WithDeploymentName_ConfiguresStoredOutputDisabled() | ||
| { | ||
| // Arrange | ||
| var responseClient = CreateTestClient(); | ||
|
|
||
| // Act | ||
| var chatClient = responseClient.AsIChatClientWithStoredOutputDisabled(deploymentName: "my-deployment"); | ||
|
|
||
| // Assert | ||
| var createResponseOptions = GetCreateResponseOptionsFromPipeline(chatClient); | ||
| Assert.NotNull(createResponseOptions); | ||
| Assert.False(createResponseOptions.StoredOutputEnabled); | ||
| Assert.Contains(IncludedResponseProperty.ReasoningEncryptedContent, createResponseOptions.IncludedProperties); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Extracts the <see cref="CreateResponseOptions"/> produced by the ConfigureOptions pipeline | ||
| /// by using reflection to access the configure action and invoking it on a test <see cref="ChatOptions"/>. | ||
| /// </summary> | ||
| private static CreateResponseOptions? GetCreateResponseOptionsFromPipeline(IChatClient chatClient) | ||
| { | ||
| var configureField = chatClient.GetType().GetField("_configureOptions", BindingFlags.NonPublic | BindingFlags.Instance); | ||
| Assert.NotNull(configureField); | ||
|
|
||
| var configureAction = configureField.GetValue(chatClient) as Action<ChatOptions>; | ||
| Assert.NotNull(configureAction); | ||
|
|
||
| var options = new ChatOptions(); | ||
| configureAction(options); | ||
|
|
||
| Assert.NotNull(options.RawRepresentationFactory); | ||
| return options.RawRepresentationFactory(chatClient) as CreateResponseOptions; | ||
| } | ||
rogerbarreto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.