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: Handlebars prompt template doesn't deserialize JSON response when Content-Type includes charset #5127

Closed
vsomayaji opened this issue Feb 23, 2024 · 0 comments · Fixed by #5133
Assignees
Labels
bug Something isn't working .NET Issue or Pull requests regarding .NET code

Comments

@vsomayaji
Copy link

Describe the bug
If the Content-Type header of a REST API JSON response includes a charset (e.g. Content-Type: application/json; charset=utf-8), the Handlebars prompt template handles the response as a string instead of as deserialized JSON.

To Reproduce
The issue appears to be due to how the Content-Type is matched here:

if (resultAsObject is RestApiOperationResponse restApiOperationResponse)
{
// Deserialize any JSON content or return the content as a string
if (string.Equals(restApiOperationResponse.ContentType, "application/json", StringComparison.OrdinalIgnoreCase))
{
var parsedJson = JsonValue.Parse(restApiOperationResponse.Content.ToString());
return KernelHelpersUtils.DeserializeJsonNode(parsedJson);
}
return restApiOperationResponse.Content;
}

Expected behavior
The JSON response should be deserialized.

Platform

  • Language: C#
  • Source: Microsoft.SemanticKernel.PromptTemplates.Handlebars version 1.4.0
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code triage bug Something isn't working and removed triage labels Feb 23, 2024
github-merge-queue bot pushed a commit that referenced this issue Feb 23, 2024
### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

Resolves #5127: If the Content-Type header of a REST API JSON response
includes a charset (e.g. Content-Type: application/json; charset=utf-8),
the Handlebars prompt template handles the response as a string instead
of as deserialized JSON.

This PR changes the check from strict equality to a check that determine
if the content type simply contains `application/json`

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working .NET Issue or Pull requests regarding .NET code
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants