@@ -52,37 +52,27 @@ local status, response = client:create_response(
5252)
5353
5454if status ~= 200 then
55- print (" Error:" , status )
56- print (cjson .encode (response ))
55+ io.stderr : write (" Error: " .. status .. " \n " )
56+ io.stderr : write (cjson .encode (response ) .. " \n " )
5757 os.exit (1 )
5858end
5959
60- print (" Response ID:" , response .id )
61- print (" Raw output:" )
62-
6360-- Extract the text content from the response
6461for _ , output_item in ipairs (response .output or {}) do
6562 if output_item .content then
6663 for _ , content_item in ipairs (output_item .content ) do
6764 if content_item .type == " output_text" and content_item .text then
6865 print (content_item .text )
69-
70- -- Parse and display the structured data
71- local parsed = cjson .decode (content_item .text )
72- print (" \n Parsed structured data:" )
73- print (" Name:" , parsed .name )
74- print (" Age:" , parsed .age )
75- print (" Occupation:" , parsed .occupation )
76- print (" Skills:" , table.concat (parsed .skills , " , " ))
77- print (" Employed:" , parsed .is_employed and " Yes" or " No" )
66+ -- Verify it parses as valid JSON
67+ assert (cjson .decode (content_item .text ), " Failed to parse JSON" )
7868 end
7969 end
8070 end
8171end
8272
83- print (" \n --- Using ResponsesChatSession with structured output ---\n " )
73+ io.stderr : write (" \n --- Using ResponsesChatSession with structured output ---\n \n" )
8474
85- -- You can also use structured outputs with the chat session by passing options to create_response
75+ -- You can also use structured outputs with the chat session
8676local session = client :new_response_chat_session ()
8777
8878local recipe_schema = {
@@ -118,29 +108,15 @@ local recipe_schema = {
118108 }
119109}
120110
121- -- Use create_response directly to pass the text format option
122111local recipe_response , err = session :create_response (" Give me a recipe for chocolate chip cookies" , {
123112 text = { format = recipe_schema }
124113})
125114
126115if recipe_response then
127- print (" Recipe response:" )
128116 print (recipe_response .output_text )
129-
130- local recipe = cjson .decode (recipe_response .output_text )
131- print (" \n Formatted recipe:" )
132- print (" Dish:" , recipe .dish_name )
133- print (" Prep time:" , recipe .prep_time_minutes , " minutes" )
134- print (" Cook time:" , recipe .cook_time_minutes , " minutes" )
135- print (" Servings:" , recipe .servings )
136- print (" \n Ingredients:" )
137- for _ , ing in ipairs (recipe .ingredients ) do
138- print (" -" , ing .amount , ing .item )
139- end
140- print (" \n Steps:" )
141- for i , step in ipairs (recipe .steps ) do
142- print (string.format (" %d. %s" , i , step ))
143- end
117+ -- Verify it parses as valid JSON
118+ assert (cjson .decode (recipe_response .output_text ), " Failed to parse JSON" )
144119else
145- print (" Error:" , err )
120+ io.stderr :write (" Error: " .. tostring (err ) .. " \n " )
121+ os.exit (1 )
146122end
0 commit comments