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: Bug: ResponseFormat using typeof() yields invalid schema #10485

Open
brandonh-msft opened this issue Feb 11, 2025 · 3 comments
Open

.Net: Bug: ResponseFormat using typeof() yields invalid schema #10485

brandonh-msft opened this issue Feb 11, 2025 · 3 comments
Assignees
Labels
bug Something isn't working .NET Issue or Pull requests regarding .NET code

Comments

@brandonh-msft
Copy link

Describe the bug
I'm using ReponseFormat on a PromptExecutionSettings object. In my scenario I am setting it to typeof(Model[]) where Model is a record type with a simple structure, and I want to return an array of them as the LLM's response.

When the prompt executes, an error is thrown:

Microsoft.SemanticKernel.HttpOperationException: HTTP 400 (invalid_request_error: invalid_value) Parameter: response_format.json_schema.name  Invalid 'response_format.json_schema.name': string does not match pattern. Expected a string that matches the pattern '^[a-zA-Z0-9_-]+$'.  ---> System.ClientModel.ClientResultException: HTTP 400 (invalid_request_error: invalid_value) Parameter: response_format.json_schema.name  Invalid 'response_format.json_schema.name': string does not match pattern. Expected a string that matches the pattern '^[a-zA-Z0-9_-]+$'.    at Azure.AI.OpenAI.ClientPipelineExtensions.ProcessMessageAsync(
...

To Reproduce
Steps to reproduce the behavior:

  1. Set ResponseFormat to a typeof() expression on an OpenAIPromptExecutionSettings object
  2. Pass that settings obj as KernelArguments to an InvokePromptAsync() call

Expected behavior
The type should be schematized and sent along validly to the Azure OpenAI resource

Screenshots
If applicable, add screenshots to help explain your problem.

Platform

  • Language: C#
  • Source: Microsoft.SemanticKernel 1.36.1
  • AI model: Azure OpenAI:GPT-4o
  • IDE: Visual Studio
  • OS: Windows
@brandonh-msft brandonh-msft added the bug Something isn't working label Feb 11, 2025
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code triage labels Feb 11, 2025
@github-actions github-actions bot changed the title Bug: ResponseFormat using typeof() yields invalid schema .Net: Bug: ResponseFormat using typeof() yields invalid schema Feb 11, 2025
@markwallace-microsoft markwallace-microsoft moved this to Sprint: Planned in Semantic Kernel Feb 11, 2025
@markwallace-microsoft markwallace-microsoft moved this from Sprint: Planned to Bug in Semantic Kernel Feb 11, 2025
@dmytrostruk
Copy link
Member

@brandonh-msft The reason why you are getting the error you mentioned is because type Model[] generates a name that is not acceptable by Azure/OpenAI API as JSON schema name. But even if we fix the name generation and send something like Collection_Model, it still won't work, because Azure/OpenAI API doesn't support collection as response type. You will receive following error:

schema must be a JSON Schema of 'type: "object"', got 'type: "array"

So, if you want a collection as response, you can wrap it in class/struct, like we did in following example:

/// <summary>Movie result struct that will be used as desired chat completion response format (structured output).</summary>
private struct MovieResult
{
public List<Movie> Movies { get; set; }
}

And then set it as response format:

// Specify response format by setting Type object in prompt execution settings.
var executionSettings = new OpenAIPromptExecutionSettings
{
ResponseFormat = typeof(MovieResult)
};

Please let me know if that will work for you, thanks!

@dmytrostruk dmytrostruk moved this from Bug to Sprint: In Review in Semantic Kernel Feb 12, 2025
@brandonh-msft
Copy link
Author

yup that's what I ended up doing, thanks.

am hopeful we can avoid creating bad names, though, even if user does provide wrong types? just a thought.

@dmytrostruk
Copy link
Member

am hopeful we can avoid creating bad names, though, even if user does provide wrong types? just a thought.

I think we can fix it at some point, in case if collections will be supported by Azure/OpenAI API in the future. As for now, I will deprioritize this task, since the case is not supported.

@dmytrostruk dmytrostruk moved this from Sprint: In Review to Bug in Semantic Kernel Feb 12, 2025
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
Status: Bug
Development

No branches or pull requests

4 participants