discourse-ai/spec/lib/completions/structured_output_spec.rb
Roman Rizzi ff2e18f9ca
FIX: Structured output discrepancies. (#1340)
This change fixes two bugs and adds a safeguard.

The first issue is that the schema Gemini expected differed from the one sent, resulting in 400 errors when performing completions.

The second issue was that creating a new persona won't define a method
for `response_format`. This has to be explicitly defined when we wrap it inside the Persona class. Also, There was a mismatch between the default value and what we stored in the DB. Some parts of the code expected symbols as keys and others as strings.

Finally, we add a safeguard when, even if asked to, the model refuses to reply with a valid JSON. In this case, we are making a best-effort to recover and stream the raw response.
2025-05-15 11:32:10 -03:00

84 lines
2.5 KiB
Ruby

# frozen_string_literal: true
RSpec.describe DiscourseAi::Completions::StructuredOutput do
subject(:structured_output) do
described_class.new(
{
message: {
type: "string",
},
bool: {
type: "boolean",
},
number: {
type: "integer",
},
status: {
type: "string",
},
},
)
end
describe "Parsing structured output on the fly" do
it "acts as a buffer for an streamed JSON" do
chunks = [
+"{\"message\": \"Line 1\\n",
+"Line 2\\n",
+"Line 3\", ",
+"\"bool\": true,",
+"\"number\": 4",
+"2,",
+"\"status\": \"o",
+"\\\"k\\\"\"}",
]
structured_output << chunks[0]
expect(structured_output.read_buffered_property(:message)).to eq("Line 1\n")
structured_output << chunks[1]
expect(structured_output.read_buffered_property(:message)).to eq("Line 2\n")
structured_output << chunks[2]
expect(structured_output.read_buffered_property(:message)).to eq("Line 3")
structured_output << chunks[3]
expect(structured_output.read_buffered_property(:bool)).to eq(true)
# Waiting for number to be fully buffered.
structured_output << chunks[4]
expect(structured_output.read_buffered_property(:bool)).to eq(true)
expect(structured_output.read_buffered_property(:number)).to be_nil
structured_output << chunks[5]
expect(structured_output.read_buffered_property(:number)).to eq(42)
structured_output << chunks[6]
expect(structured_output.read_buffered_property(:number)).to eq(42)
expect(structured_output.read_buffered_property(:bool)).to eq(true)
expect(structured_output.read_buffered_property(:status)).to eq("o")
structured_output << chunks[7]
expect(structured_output.read_buffered_property(:status)).to eq("\"k\"")
# No partial string left to read.
expect(structured_output.read_buffered_property(:status)).to eq("")
end
end
describe "dealing with non-JSON responses" do
it "treat it as plain text once we determined it's invalid JSON" do
chunks = [+"I'm not", +"a", +"JSON :)"]
structured_output << chunks[0]
expect(structured_output.read_buffered_property(nil)).to eq("I'm not")
structured_output << chunks[1]
expect(structured_output.read_buffered_property(nil)).to eq("a")
structured_output << chunks[2]
expect(structured_output.read_buffered_property(nil)).to eq("JSON :)")
end
end
end