Skip to content

Commit 832c81b

Browse files
committed
Add test that error messages are included in Faraday response body
1 parent 8fa2d37 commit 832c81b

File tree

2 files changed

+213
-1
lines changed

2 files changed

+213
-1
lines changed

spec/fixtures/cassettes/gpt-3_5-turbo_function_call_chat.yml

Lines changed: 165 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/openai/client/chat_spec.rb

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
let(:messages) { [{ role: "user", content: "Hello!" }] }
55
let(:stream) { false }
66
let(:response) do
7-
OpenAI::Client.new.chat(
7+
client = OpenAI::Client.new do |client|
8+
client.response :logger, ::Logger.new(STDOUT), bodies: true
9+
end
10+
11+
client.chat(
812
parameters: {
913
model: model,
1014
messages: messages,
15+
functions: functions,
1116
stream: stream
1217
}
1318
)
1419
end
20+
let(:functions) { [] }
1521
let(:content) { response.dig("choices", 0, "message", "content") }
1622
let(:cassette) { "#{model} #{'streamed' if stream} chat".downcase }
1723

@@ -24,6 +30,47 @@
2430
end
2531
end
2632

33+
context "with an invalid function call" do
34+
let(:cassette) { "#{model} function call chat".downcase }
35+
let(:messages) do
36+
[
37+
{
38+
"role"=>"function",
39+
# "name" => "function",
40+
"content"=>"function"
41+
}
42+
]
43+
end
44+
let(:functions) do
45+
[
46+
{
47+
"name"=>"function",
48+
"description"=>"function",
49+
"parameters"=>
50+
{
51+
"type"=>"object",
52+
"properties"=>{
53+
"user"=>{
54+
"type"=>"string",
55+
"description"=>"the full name of the user"
56+
}
57+
},
58+
}
59+
},
60+
]
61+
end
62+
63+
it "raises an error containing the reason" do
64+
VCR.use_cassette(cassette) do
65+
begin
66+
response
67+
rescue Faraday::Error => e
68+
expect(e.response.dig(:body, "error", "message")).to eq("Missing parameter 'name': messages with role 'function' must have a 'name'.")
69+
end
70+
end
71+
end
72+
end
73+
2774
describe "streaming" do
2875
let(:chunks) { [] }
2976
let(:stream) do

0 commit comments

Comments
 (0)