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: Fixed JSON schema name in Structured Outputs for generic types #9490

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

dmytrostruk
Copy link
Member

Motivation and Context

Resolves: #9416

When C# type is passed as a response format to OpenAI chat completion service, we use the type name as JSON schema name. When the type is generic, its name will contain characters that are not allowed in JSON schema name according to OpenAI API. This PR fixes the issue by replacing invalid characters with empty string.

Contribution Checklist

@dmytrostruk dmytrostruk self-assigned this Oct 31, 2024
@dmytrostruk dmytrostruk requested a review from a team as a code owner October 31, 2024 18:07
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel labels Oct 31, 2024
return ChatResponseFormat.CreateJsonSchemaFormat(type.Name, schemaBinaryData, jsonSchemaIsStrict: true);
var typeName = StructuredOutputsJsonSchemaNameRegex().Replace(type.Name, string.Empty);

return ChatResponseFormat.CreateJsonSchemaFormat(typeName, schemaBinaryData, jsonSchemaIsStrict: true);
Copy link
Contributor

Choose a reason for hiding this comment

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

Just wondering, does the type name even matter at all, e.g. can we not just always pass some static value like "SKType"?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good question! Based on my testing, JSON schema format name property is not used by the model to determine the nature of the data. For example, if I'm requesting a collection of animals, but my schema has name birds, the model result will include different animals, not just birds. On the other hand, JSON schema format description property is used by the model.

I couldn't find more information about name property in OpenAI documentation, but all of the examples of Structured Outputs functionality use the name that is related to actual format - math_reasoning, get_weather, ui and so on. I think there may be a case when JSON schema format is cached on OpenAI side by name or something like that.

In all cases, I've decided to follow the same approach in our code as presented in OpenAI examples. I also checked OpenAI/SK Python SDKs, and it appeared they use the same approach, so we are also consistent here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kernel Issues or pull requests impacting the core kernel .NET Issue or Pull requests regarding .NET code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HTTP 400 error "Invalid 'response_format.json_schema.name'" when generic type is used for ResponseFormat
3 participants