22// The .NET Foundation licenses this file to you under the MIT license.
33
44using System ;
5+ using System . ClientModel . Primitives ;
56using System . Collections . Generic ;
67using System . ComponentModel ;
78using System . Linq ;
@@ -22,6 +23,74 @@ public class OpenAIConversionTests
2223 "test_function" ,
2324 "A test function for conversion" ) ;
2425
26+ [ Fact ]
27+ public void AsOpenAIChatResponseFormat_HandlesVariousFormats ( )
28+ {
29+ Assert . Null ( MicrosoftExtensionsAIChatExtensions . AsOpenAIChatResponseFormat ( null ) ) ;
30+
31+ var text = MicrosoftExtensionsAIChatExtensions . AsOpenAIChatResponseFormat ( ChatResponseFormat . Text ) ;
32+ Assert . NotNull ( text ) ;
33+ Assert . Equal ( """{"type":"text"}""" , ( ( IJsonModel < OpenAI . Chat . ChatResponseFormat > ) text ) . Write ( ModelReaderWriterOptions . Json ) . ToString ( ) ) ;
34+
35+ var json = MicrosoftExtensionsAIChatExtensions . AsOpenAIChatResponseFormat ( ChatResponseFormat . Json ) ;
36+ Assert . NotNull ( json ) ;
37+ Assert . Equal ( """{"type":"json_object"}""" , ( ( IJsonModel < OpenAI . Chat . ChatResponseFormat > ) json ) . Write ( ModelReaderWriterOptions . Json ) . ToString ( ) ) ;
38+
39+ var jsonSchema = ChatResponseFormat . ForJsonSchema ( typeof ( int ) , schemaName : "my_schema" , schemaDescription : "A test schema" ) . AsOpenAIChatResponseFormat ( ) ;
40+ Assert . NotNull ( jsonSchema ) ;
41+ Assert . Equal ( """
42+ {"type":"json_schema","json_schema":{"description":"A test schema","name":"my_schema","schema":{
43+ "$schema": "https://json-schema.org/draft/2020-12/schema",
44+ "type": "integer"
45+ }}}
46+ """ , ( ( IJsonModel < OpenAI . Chat . ChatResponseFormat > ) jsonSchema ) . Write ( ModelReaderWriterOptions . Json ) . ToString ( ) ) ;
47+
48+ jsonSchema = ChatResponseFormat . ForJsonSchema ( typeof ( int ) , schemaName : "my_schema" , schemaDescription : "A test schema" ) . AsOpenAIChatResponseFormat (
49+ new ( ) { AdditionalProperties = new AdditionalPropertiesDictionary { [ "strictJsonSchema" ] = true } } ) ;
50+ Assert . NotNull ( jsonSchema ) ;
51+ Assert . Equal ( """
52+ {"type":"json_schema","json_schema":{"description":"A test schema","name":"my_schema","schema":{
53+ "$schema": "https://json-schema.org/draft/2020-12/schema",
54+ "type": "integer"
55+ },"strict":true}}
56+ """ , ( ( IJsonModel < OpenAI . Chat . ChatResponseFormat > ) jsonSchema ) . Write ( ModelReaderWriterOptions . Json ) . ToString ( ) ) ;
57+ }
58+
59+ [ Fact ]
60+ public void AsOpenAIResponseTextFormat_HandlesVariousFormats ( )
61+ {
62+ Assert . Null ( MicrosoftExtensionsAIResponsesExtensions . AsOpenAIResponseTextFormat ( null ) ) ;
63+
64+ var text = MicrosoftExtensionsAIResponsesExtensions . AsOpenAIResponseTextFormat ( ChatResponseFormat . Text ) ;
65+ Assert . NotNull ( text ) ;
66+ Assert . Equal ( ResponseTextFormatKind . Text , text . Kind ) ;
67+
68+ var json = MicrosoftExtensionsAIResponsesExtensions . AsOpenAIResponseTextFormat ( ChatResponseFormat . Json ) ;
69+ Assert . NotNull ( json ) ;
70+ Assert . Equal ( ResponseTextFormatKind . JsonObject , json . Kind ) ;
71+
72+ var jsonSchema = ChatResponseFormat . ForJsonSchema ( typeof ( int ) , schemaName : "my_schema" , schemaDescription : "A test schema" ) . AsOpenAIResponseTextFormat ( ) ;
73+ Assert . NotNull ( jsonSchema ) ;
74+ Assert . Equal ( ResponseTextFormatKind . JsonSchema , jsonSchema . Kind ) ;
75+ Assert . Equal ( """
76+ {"type":"json_schema","description":"A test schema","name":"my_schema","schema":{
77+ "$schema": "https://json-schema.org/draft/2020-12/schema",
78+ "type": "integer"
79+ }}
80+ """ , ( ( IJsonModel < ResponseTextFormat > ) jsonSchema ) . Write ( ModelReaderWriterOptions . Json ) . ToString ( ) ) ;
81+
82+ jsonSchema = ChatResponseFormat . ForJsonSchema ( typeof ( int ) , schemaName : "my_schema" , schemaDescription : "A test schema" ) . AsOpenAIResponseTextFormat (
83+ new ( ) { AdditionalProperties = new AdditionalPropertiesDictionary { [ "strictJsonSchema" ] = true } } ) ;
84+ Assert . NotNull ( jsonSchema ) ;
85+ Assert . Equal ( ResponseTextFormatKind . JsonSchema , jsonSchema . Kind ) ;
86+ Assert . Equal ( """
87+ {"type":"json_schema","description":"A test schema","name":"my_schema","schema":{
88+ "$schema": "https://json-schema.org/draft/2020-12/schema",
89+ "type": "integer"
90+ },"strict":true}
91+ """ , ( ( IJsonModel < ResponseTextFormat > ) jsonSchema ) . Write ( ModelReaderWriterOptions . Json ) . ToString ( ) ) ;
92+ }
93+
2594 [ Fact ]
2695 public void AsOpenAIChatTool_ProducesValidInstance ( )
2796 {
0 commit comments