Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2.2k
.NET: OpenAI Responses: suppress 'model' parameter if it is used for agent#2003
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
Closed
Reuben Bond (ReubenBond)
wants to merge
4
commits into
microsoft:main
from
ReubenBond:fix/oai-devui-model
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2 ...ples/GettingStarted/Workflows/_Foundational/07_MixedWorkflowAgentsAndExecutors/Program.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
2 changes: 1 addition & 1 deletion
2 dotnet/samples/GettingStarted/Workflows/_Foundational/08_WriterCriticWorkflow/Program.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
4 changes: 4 additions & 0 deletions
4 dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/AIAgentResponseExecutor.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
2 changes: 1 addition & 1 deletion
2 dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/AgentRunResponseExtensions.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
79 changes: 49 additions & 30 deletions
79 dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/HostedAgentResponseExecutor.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
11 changes: 11 additions & 0 deletions
11 dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/IResponseExecutor.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
11 changes: 11 additions & 0 deletions
11 dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/IResponsesService.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
34 changes: 16 additions & 18 deletions
34 dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/InMemoryResponsesService.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 |
|---|---|---|
| @@ -147,18 +147,27 @@ public InMemoryResponsesService(IResponseExecutor executor, InMemoryStorageOptio | ||
| this._conversationStorage = conversationStorage; | ||
| } | ||
| public async Task<Response> CreateResponseAsync( | ||
| public async ValueTask<ResponseError?> ValidateRequestAsync( | ||
| CreateResponse request, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| ValidateRequest(request); | ||
| // Validate agent resolution early for HostedAgentResponseExecutor | ||
| if (this._executor is HostedAgentResponseExecutor hostedExecutor) | ||
| if (request.Conversation is not null && !string.IsNullOrEmpty(request.Conversation.Id) && | ||
| !string.IsNullOrEmpty(request.PreviousResponseId)) | ||
| { | ||
| hostedExecutor.ValidateAgent(request); | ||
| return new ResponseError | ||
| { | ||
| Code = "invalid_request", | ||
| Message = "Mutually exclusive parameters: 'conversation' and 'previous_response_id'. Ensure you are only providing one of: 'previous_response_id' or 'conversation'." | ||
| }; | ||
| } | ||
| return await this._executor.ValidateRequestAsync(request, cancellationToken).ConfigureAwait(false); | ||
| } | ||
| public async Task<Response> CreateResponseAsync( | ||
| CreateResponse request, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| if (request.Stream == true) | ||
| { | ||
| throw new InvalidOperationException("Cannot create a streaming response using CreateResponseAsync. Use CreateResponseStreamingAsync instead."); | ||
| @@ -189,8 +198,6 @@ public async IAsyncEnumerable<StreamingResponseEvent> CreateResponseStreamingAsy | ||
| CreateResponse request, | ||
| [EnumeratorCancellation] CancellationToken cancellationToken = default) | ||
| { | ||
| ValidateRequest(request); | ||
| if (request.Stream == false) | ||
| { | ||
| throw new InvalidOperationException("Cannot create a non-streaming response using CreateResponseStreamingAsync. Use CreateResponseAsync instead."); | ||
| @@ -342,15 +349,6 @@ public Task<ListResponse<ItemResource>> ListResponseInputItemsAsync( | ||
| }); | ||
| } | ||
| private static void ValidateRequest(CreateResponse request) | ||
| { | ||
| if (request.Conversation is not null && !string.IsNullOrEmpty(request.Conversation.Id) && | ||
| !string.IsNullOrEmpty(request.PreviousResponseId)) | ||
| { | ||
| throw new InvalidOperationException("Mutually exclusive parameters: 'conversation' and 'previous_response_id'. Ensure you are only providing one of: 'previous_response_id' or 'conversation'."); | ||
| } | ||
| } | ||
| private ResponseState InitializeResponse(string responseId, CreateResponse request) | ||
| { | ||
| var metadata = request.Metadata ?? []; | ||
| @@ -371,7 +369,7 @@ private ResponseState InitializeResponse(string responseId, CreateResponse reque | ||
| MaxOutputTokens = request.MaxOutputTokens, | ||
| MaxToolCalls = request.MaxToolCalls, | ||
| Metadata = metadata, | ||
| Model = request.Model ?? "default", | ||
| Model = request.Model, | ||
ReubenBond marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Output = [], | ||
| ParallelToolCalls = request.ParallelToolCalls ?? true, | ||
| PreviousResponseId = request.PreviousResponseId, | ||
66 changes: 30 additions & 36 deletions
66 dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/ResponsesHttpHandler.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 |
|---|---|---|
| @@ -34,6 +34,21 @@ public async Task<IResult> CreateResponseAsync( | ||
| [FromQuery] bool? stream, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| // Validate the request first | ||
| ResponseError? validationError = await this._responsesService.ValidateRequestAsync(request, cancellationToken).ConfigureAwait(false); | ||
| if (validationError is not null) | ||
| { | ||
| return Results.BadRequest(new ErrorResponse | ||
| { | ||
| Error = new ErrorDetails | ||
| { | ||
| Message = validationError.Message, | ||
| Type = "invalid_request_error", | ||
| Code = validationError.Code | ||
| } | ||
| }); | ||
| } | ||
| try | ||
| { | ||
| // Handle streaming vs non-streaming | ||
| @@ -55,45 +70,24 @@ public async Task<IResult> CreateResponseAsync( | ||
| request, | ||
| cancellationToken: cancellationToken).ConfigureAwait(false); | ||
| return Results.Ok(response); | ||
| } | ||
| catch (InvalidOperationException ex) when (ex.Message.Contains("Mutually exclusive")) | ||
| { | ||
| // Return OpenAI-style error for mutual exclusivity violations | ||
| return Results.BadRequest(new ErrorResponse | ||
| return response.Status switch | ||
| { | ||
| Error = new ErrorDetails | ||
| { | ||
| Message = ex.Message, | ||
| Type = "invalid_request_error", | ||
| Code = "mutually_exclusive_parameters" | ||
| } | ||
| }); | ||
| } | ||
| catch (InvalidOperationException ex) when (ex.Message.Contains("not found") || ex.Message.Contains("does not exist")) | ||
| { | ||
| // Return OpenAI-style error for not found errors | ||
| return Results.NotFound(new ErrorResponse | ||
| { | ||
| Error = new ErrorDetails | ||
| { | ||
| Message = ex.Message, | ||
| Type = "invalid_request_error" | ||
| } | ||
| }); | ||
| ResponseStatus.Failed when response.Error is { } error => Results.Problem( | ||
| detail: error.Message, | ||
| statusCode: StatusCodes.Status500InternalServerError, | ||
| title: error.Code ?? "Internal Server Error"), | ||
| ResponseStatus.Failed => Results.Problem(), | ||
| ResponseStatus.Queued => Results.Accepted(value: response), | ||
| _ => Results.Ok(response) | ||
| }; | ||
| } | ||
| catch (InvalidOperationException ex) when (ex.Message.Contains("No 'agent.name' or 'model' specified")) | ||
| catch (Exception ex) | ||
| { | ||
| // Return OpenAI-style error for missing required parameters | ||
| return Results.BadRequest(new ErrorResponse | ||
| { | ||
| Error = new ErrorDetails | ||
| { | ||
| Message = ex.Message, | ||
| Type = "invalid_request_error", | ||
| Code = "missing_required_parameter" | ||
| } | ||
| }); | ||
| // Return InternalServerError for unexpected exceptions | ||
| return Results.Problem( | ||
| detail: ex.Message, | ||
| statusCode: StatusCodes.Status500InternalServerError, | ||
| title: "Internal Server Error"); | ||
| } | ||
ReubenBond marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
2 changes: 1 addition & 1 deletion
2 ...soft.Agents.AI.Hosting.OpenAI.UnitTests/OpenAIResponsesAgentResolutionIntegrationTests.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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remind me why DevUI doesn't just always send it in an agent property rather than using model for that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DevUI uses the official OpenAI SDK, which doesn't have the 'agent' property. I want to introduce a change there to use the 'metadata' dictionary instead of 'model', but I want to un-block Jeff Handley (@jeffhandley) etc asap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it does now have JSON Patch support, so you can get/set whatever properties you want to a request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do all of the OpenAI SDKs support setting arbitrary properties in requests & responses?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have an alternate PR here that uses metadata.entity_id instead: #1984