FIX: Return structured output on non-streaming mode (#1318)

This commit is contained in:
Roman Rizzi 2025-05-06 15:34:30 -03:00 committed by GitHub
parent adc2716cec
commit 5bc9fdc06b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 18 deletions

View File

@ -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

View File

@ -68,6 +68,7 @@ module DiscourseAi
end
response = response.first if response.is_a?(Array) && response.length == 1
response
end