Skip to content
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

.Net: Expose response and response content headers #10065

Conversation

SergeyMenshykh
Copy link
Member

Motivation, Context and Description

Today, it's difficult to access response and response content headers returned by OpenAPI plugins. This PR exposes the Headers property, allowing easy access to both response and response content headers.

Closes - #9986

@SergeyMenshykh SergeyMenshykh added .NET Issue or Pull requests regarding .NET code openapi Issues related to the OpenAPI function importer labels Jan 6, 2025
@SergeyMenshykh SergeyMenshykh self-assigned this Jan 6, 2025
@SergeyMenshykh SergeyMenshykh requested a review from a team as a code owner January 6, 2025 10:41
/// A dictionary containing the headers of the HTTP response message, including content headers.
/// The dictionary keys are header names, and the values are collections of header values.
/// </returns>
private static Dictionary<string, IEnumerable<string>>? GetResponseHeaders(HttpResponseMessage responseMessage)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May consider a ReadOnlyDictionary or the original HttpResponseHeaders if it is accessible?

Suggested change
private static Dictionary<string, IEnumerable<string>>? GetResponseHeaders(HttpResponseMessage responseMessage)
private static ReadOnlyDictionary<string, IEnumerable<string>>? GetResponseHeaders(HttpResponseMessage responseMessage)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, is there a reason for this dictionary to be mutable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having it mutable will allow users to add new headers or remove existing ones if needed.

/// </summary>
[Experimental("SKEXP0040")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public IDictionary<string, IEnumerable<string>>? Headers { get; set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public IDictionary<string, IEnumerable<string>>? Headers { get; set; }
public IReadOnlyDictionary<string, IEnumerable<string>>? Headers { get; set; }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be possible to remove an existing header from the dictionary in auto function invocation filters or add a new one if needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For response? Not sure if I'm following, isn't this after the effect (request)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, in response, yes, after the effect. The auto function invocation filters allow you to mutate the function invocation result in the way that is required for your scenario. Some headers may not need to be sent to the AI model, some may need to be added if they were not in the response, and some may need to be normalized or sanitized, etc.

// Add the content headers as well.
foreach (var item_ in responseMessage.Content.Headers)
{
headers[item_.Key] = item_.Value;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a conflict with the key names?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, it's possible, but it should not be the case because response and content represent different types of headers that should not have the same names. For the unusual scenarios where it might be the case, we can introduce RestApiOperationResponseResultFactory that will have access to the HttpResponseMessage and will allow customization of functionality for reading headers.

/// A dictionary containing the headers of the HTTP response message, including content headers.
/// The dictionary keys are header names, and the values are collections of header values.
/// </returns>
private static Dictionary<string, IEnumerable<string>>? GetResponseHeaders(HttpResponseMessage responseMessage)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, is there a reason for this dictionary to be mutable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kernel.core .NET Issue or Pull requests regarding .NET code openapi Issues related to the OpenAPI function importer
Projects
Status: Sprint: Done
Development

Successfully merging this pull request may close these issues.

3 participants