11package prompt
22
33import (
4+ "encoding/json"
45 "os"
56 "path/filepath"
67 "testing"
@@ -139,27 +140,35 @@ messages:
139140 require .Nil (t , promptFile .JsonSchema )
140141 })
141142
142- t .Run ("loads prompt file with responseFormat json_schema and jsonSchema" , func (t * testing.T ) {
143+ t .Run ("loads prompt file with responseFormat json_schema and jsonSchema as JSON string " , func (t * testing.T ) {
143144 const yamlBody = `
144- name: JSON Schema Response Format Test
145- description: Test with JSON schema response format
145+ name: JSON Schema String Format Test
146+ description: Test with JSON schema as JSON string
146147model: openai/gpt-4o
147148responseFormat: json_schema
148- jsonSchema:
149- name: person_info
150- strict: true
151- schema:
152- type: object
153- properties:
154- name:
155- type: string
156- description: The name of the person
157- age:
158- type: integer
159- description: The age of the person
160- required:
161- - name
162- additionalProperties: false
149+ jsonSchema: |-
150+ {
151+ "name": "describe_animal",
152+ "strict": true,
153+ "schema": {
154+ "type": "object",
155+ "properties": {
156+ "name": {
157+ "type": "string",
158+ "description": "The name of the animal"
159+ },
160+ "habitat": {
161+ "type": "string",
162+ "description": "The habitat the animal lives in"
163+ }
164+ },
165+ "additionalProperties": false,
166+ "required": [
167+ "name",
168+ "habitat"
169+ ]
170+ }
171+ }
163172messages:
164173 - role: user
165174 content: "Hello"
@@ -175,10 +184,26 @@ messages:
175184 require .NotNil (t , promptFile .ResponseFormat )
176185 require .Equal (t , "json_schema" , * promptFile .ResponseFormat )
177186 require .NotNil (t , promptFile .JsonSchema )
178- require .Equal (t , "person_info" , promptFile .JsonSchema .Name )
179- require .True (t , * promptFile .JsonSchema .Strict )
180- require .Contains (t , promptFile .JsonSchema .Schema , "type" )
181- require .Contains (t , promptFile .JsonSchema .Schema , "properties" )
187+
188+ // Verify the schema contents using the already parsed data
189+ schema := promptFile .JsonSchema .Parsed
190+ require .Equal (t , "describe_animal" , schema ["name" ])
191+ require .Equal (t , true , schema ["strict" ])
192+ require .Contains (t , schema , "schema" )
193+
194+ // Verify the nested schema structure
195+ nestedSchema := schema ["schema" ].(map [string ]interface {})
196+ require .Equal (t , "object" , nestedSchema ["type" ])
197+ require .Contains (t , nestedSchema , "properties" )
198+ require .Contains (t , nestedSchema , "required" )
199+
200+ properties := nestedSchema ["properties" ].(map [string ]interface {})
201+ require .Contains (t , properties , "name" )
202+ require .Contains (t , properties , "habitat" )
203+
204+ required := nestedSchema ["required" ].([]interface {})
205+ require .Contains (t , required , "name" )
206+ require .Contains (t , required , "habitat" )
182207 })
183208
184209 t .Run ("validates invalid responseFormat" , func (t * testing.T ) {
@@ -224,23 +249,32 @@ messages:
224249 })
225250
226251 t .Run ("BuildChatCompletionOptions includes responseFormat and jsonSchema" , func (t * testing.T ) {
252+ jsonSchemaStr := `{
253+ "name": "test_schema",
254+ "strict": true,
255+ "schema": {
256+ "type": "object",
257+ "properties": {
258+ "name": {
259+ "type": "string",
260+ "description": "The name"
261+ }
262+ },
263+ "required": ["name"]
264+ }
265+ }`
266+
227267 promptFile := & File {
228268 Model : "openai/gpt-4o" ,
229269 ResponseFormat : func () * string { s := "json_schema" ; return & s }(),
230- JsonSchema : & JsonSchema {
231- Name : "test_schema" ,
232- Strict : func () * bool { b := true ; return & b }(),
233- Schema : map [string ]interface {}{
234- "type" : "object" ,
235- "properties" : map [string ]interface {}{
236- "name" : map [string ]interface {}{
237- "type" : "string" ,
238- "description" : "The name" ,
239- },
240- },
241- "required" : []string {"name" },
242- },
243- },
270+ JsonSchema : func () * JsonSchema {
271+ js := & JsonSchema {Raw : jsonSchemaStr }
272+ err := json .Unmarshal ([]byte (jsonSchemaStr ), & js .Parsed )
273+ if err != nil {
274+ t .Fatal (err )
275+ }
276+ return js
277+ }(),
244278 }
245279
246280 messages := []azuremodels.ChatMessage {
0 commit comments