You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: OpenAI chat completion with Json output format
3
+
description: Auzre/OpenAI chat completion supports json format with schema.
4
+
date: 2024-12-11
5
+
tags: [ ".NET", "AI", "Semantic Kernel" ]
6
+
---
7
+
8
+
# {{title}}
9
+
10
+
*{{date | readableDate("LLLL yyyy")}}*
11
+
12
+
I can't recall how many times I've tried to convince an LLM to return JSON so that I could perform API calls based on natural language inputs from users. Recently, I discovered that this functionality is natively supported by the [Semantic Kernel](https://github.com/microsoft/semantic-kernel) and Microsoft AI Extension Library. It is officially documented by the OpenAI API [here](https://platform.openai.com/docs/guides/structured-outputs). Note that this feature is only available in the latest large language models from GPT-4o/o1 and later. If you are using Azure OpenAI, ensure you have the supported versions when deploying models.
13
+
14
+
## Chat completion
15
+
[Semantic Kernel](https://github.com/microsoft/semantic-kernel) supports JSON output formatting in the ResponseFormat property from PromptExecutionSettings, as shown in the code below:
16
+
17
+
```csharp
18
+
// Configure Azure/OpenAI and semantic kernel first.
JSON schema can be automatically generated using Microsoft.Extensions.AI.AIJsonUtilities, which is referenced from [Semantic Kernel](https://github.com/microsoft/semantic-kernel).
46
+
47
+
```csharp
48
+
publicsealedclassCalendarEvent
49
+
{
50
+
[Description("Name of the event")]
51
+
publicrequiredstringName { get; init; }
52
+
53
+
[Description("Day of the event")]
54
+
publicrequiredstringDay { get; init; }
55
+
56
+
[Description("List of participants of the event")]
0 commit comments