ChatResponseFormat.ForJsonSchema Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
| Name | Description |
|---|---|
| ForJsonSchema(JsonElement, String, String) |
Creates a ChatResponseFormatJson representing structured JSON data with the specified schema. |
| ForJsonSchema(Type, JsonSerializerOptions, String, String) |
Creates a ChatResponseFormatJson representing structured JSON data with a schema based on |
| ForJsonSchema<T>(JsonSerializerOptions, String, String) |
Creates a ChatResponseFormatJson representing structured JSON data with a schema based on |
ForJsonSchema(JsonElement, String, String)
- Source:
- ChatResponseFormat.cs
Creates a ChatResponseFormatJson representing structured JSON data with the specified schema.
public static Microsoft.Extensions.AI.ChatResponseFormatJson ForJsonSchema(System.Text.Json.JsonElement schema, string? schemaName = default, string? schemaDescription = default);
static member ForJsonSchema : System.Text.Json.JsonElement * string * string -> Microsoft.Extensions.AI.ChatResponseFormatJson
Public Shared Function ForJsonSchema (schema As JsonElement, Optional schemaName As String = Nothing, Optional schemaDescription As String = Nothing) As ChatResponseFormatJson
Parameters
- schema
- JsonElement
The JSON schema.
- schemaName
- String
An optional name of the schema. For example, if the schema represents a particular class, this could be the name of the class.
- schemaDescription
- String
An optional description of the schema.
Returns
The ChatResponseFormatJson instance.
Applies to
ForJsonSchema(Type, JsonSerializerOptions, String, String)
- Source:
- ChatResponseFormat.cs
Creates a ChatResponseFormatJson representing structured JSON data with a schema based on schemaType.
public static Microsoft.Extensions.AI.ChatResponseFormatJson ForJsonSchema(Type schemaType, System.Text.Json.JsonSerializerOptions? serializerOptions = default, string? schemaName = default, string? schemaDescription = default);
static member ForJsonSchema : Type * System.Text.Json.JsonSerializerOptions * string * string -> Microsoft.Extensions.AI.ChatResponseFormatJson
Public Shared Function ForJsonSchema (schemaType As Type, Optional serializerOptions As JsonSerializerOptions = Nothing, Optional schemaName As String = Nothing, Optional schemaDescription As String = Nothing) As ChatResponseFormatJson
Parameters
- serializerOptions
- JsonSerializerOptions
The JSON serialization options to use.
- schemaName
- String
An optional name of the schema. By default, this will be inferred from schemaType.
- schemaDescription
- String
An optional description of the schema. By default, this will be inferred from schemaType.
Returns
The ChatResponseFormatJson instance.
Exceptions
schemaType is null.
Remarks
Many AI services that support structured output require that the JSON schema have a top-level 'type=object'. If schemaType is a primitive type like String, Int32, or Boolean, or if it's a type that serializes as a JSON array, attempting to use the resulting schema with such services may fail. In such cases, consider instead using a schemaType that wraps the actual type in a class or struct so that it serializes as a JSON object with the original type as a property of that object.
Applies to
ForJsonSchema<T>(JsonSerializerOptions, String, String)
- Source:
- ChatResponseFormat.cs
Creates a ChatResponseFormatJson representing structured JSON data with a schema based on T.
public static Microsoft.Extensions.AI.ChatResponseFormatJson ForJsonSchema<T>(System.Text.Json.JsonSerializerOptions? serializerOptions = default, string? schemaName = default, string? schemaDescription = default);
static member ForJsonSchema : System.Text.Json.JsonSerializerOptions * string * string -> Microsoft.Extensions.AI.ChatResponseFormatJson
Public Shared Function ForJsonSchema(Of T) (Optional serializerOptions As JsonSerializerOptions = Nothing, Optional schemaName As String = Nothing, Optional schemaDescription As String = Nothing) As ChatResponseFormatJson
Type Parameters
- T
The type for which a schema should be exported and used as the response schema.
Parameters
- serializerOptions
- JsonSerializerOptions
The JSON serialization options to use.
- schemaName
- String
An optional name of the schema. By default, this will be inferred from T.
- schemaDescription
- String
An optional description of the schema. By default, this will be inferred from T.
Returns
The ChatResponseFormatJson instance.
Remarks
Many AI services that support structured output require that the JSON schema have a top-level 'type=object'. If T is a primitive type like String, Int32, or Boolean, or if it's a type that serializes as a JSON array, attempting to use the resulting schema with such services may fail. In such cases, consider instead using a T that wraps the actual type in a class or struct so that it serializes as a JSON object with the original type as a property of that object.