Microsoft Dev Blogs

Using JSON Schema for Structured Output in .NET for OpenAI Models

thumbnail
  • Using JSON Schema for Structured Output in .NET for OpenAI Models:

    • Demonstrated implementing a JSON Schema-based structured output using .NET version of Semantic Kernel for OpenAI models.
    • JSON Schema ensures well-structured responses, follows specified format, and easily deserialized by the system.
  • Supported Models for Structured Outputs:

    • Azure OpenAI: Access to or later The API version
    • OpenAI model
  • Structured Outputs with .NET Version of Semantic Kernel:

    • Approach 1: Providing JSON Schema to OpenAI model by initializing object and setting it in property
      • Example: Getting top 10 movies of all time from the model and specifying JSON Schema of the desired response
      • Result: Defined JSON Schema ensures consistent response format across all AI model responses
    • Approach 2: Defining desired response format using C# class or structure and setting its type directly in .NET
      • Example: Defining response format using C# class or structure and using the same type to deserialize model response
      • Result: Easy access to properties in the code with a predefined response format
  • Limitations:

    • Some keywords from JSON Schema are not supported in OpenAI Structured Outputs yet
  • Conclusion:

    • Using JSON Schema and Semantic Kernel framework allows control over AI-generated response format for structured, predictable, and easy-to-use outputs
    • Reach out for questions or feedback through Semantic Kernel GitHub Discussion Channel
    • Support Semantic Kernel by giving a star on GitHub if you've enjoyed using it