From 5bc9fdc06b345d76da89bf9d18595907929f4103 Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Tue, 6 May 2025 15:34:30 -0300 Subject: [PATCH] FIX: Return structured output on non-streaming mode (#1318) --- lib/completions/endpoints/base.rb | 21 +++----------------- lib/completions/endpoints/canned_response.rb | 1 + 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/lib/completions/endpoints/base.rb b/lib/completions/endpoints/base.rb index 7f79a819..3a12c4c7 100644 --- a/lib/completions/endpoints/base.rb +++ b/lib/completions/endpoints/base.rb @@ -114,8 +114,7 @@ module DiscourseAi model_params[:response_format].dig(:json_schema, :schema, :properties) if schema_properties.present? - structured_output = - DiscourseAi::Completions::StructuredOutput.new(schema_properties) + structured_output = DiscourseAi::Completions::StructuredOutput.new(schema_properties) end end @@ -430,23 +429,9 @@ module DiscourseAi response_data.reject!(&:blank?) if structured_output.present? - has_string_response = false + response_data.each { |data| structured_output << data if data.is_a?(String) } - response_data = - response_data.reduce([]) do |memo, data| - if data.is_a?(String) - structured_output << data - has_string_response = true - next(memo) - else - memo << data - end - - memo - end - - # We only include the structured output if there was actually a structured response - response_data << structured_output if has_string_response + return structured_output end # this is to keep stuff backwards compatible diff --git a/lib/completions/endpoints/canned_response.rb b/lib/completions/endpoints/canned_response.rb index b4dafc74..be156aea 100644 --- a/lib/completions/endpoints/canned_response.rb +++ b/lib/completions/endpoints/canned_response.rb @@ -68,6 +68,7 @@ module DiscourseAi end response = response.first if response.is_a?(Array) && response.length == 1 + response end