Skip to content

Commit a201208

Browse files
samuelrehstainless-app[bot]
authored andcommitted
fix: use APIStatusError.for instead of APIError.for in stream.rb (#194)
The APIError class doesn't have a 'for' class method, but APIStatusError does. This fixes a NoMethodError that would occur when streaming encounters an error. test: add test for streaming error handling with APIStatusError.for This test verifies that streaming error responses properly use APIStatusError.for instead of the non-existent APIError.for method.
1 parent dfa70b0 commit a201208

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

test/openai/resources/responses/streaming_test.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,31 @@ def test_incomplete_streaming
212212
end
213213
end
214214

215+
def test_streaming_error_handling
216+
stub_request(:post, "http://localhost/responses")
217+
.with(
218+
body: hash_including(
219+
instructions: "You are a helpful assistant",
220+
messages: [{content: "Hello", role: "user"}],
221+
model: "gpt-4",
222+
stream: true
223+
)
224+
)
225+
.to_return(
226+
status: 400,
227+
headers: {"Content-Type" => "text/event-stream"},
228+
body: error_sse_response
229+
)
230+
231+
stream = @client.responses.stream(**basic_params)
232+
error = assert_raises(OpenAI::Errors::APIStatusError) do
233+
stream.each { |_event| } # Consume the stream to trigger the error.
234+
end
235+
236+
assert_equal(400, error.status)
237+
assert_equal("Invalid request: missing required field", error.message)
238+
end
239+
215240
def test_text_method
216241
stub_streaming_response(basic_text_sse_response)
217242

@@ -686,4 +711,14 @@ def resume_stream_structured_output_sse_response
686711
687712
SSE
688713
end
714+
715+
def error_sse_response
716+
<<~SSE
717+
event: response.created
718+
data: {"type":"response.created","sequence_number":1,"response":{"id":"msg_error","object":"realtime.response","status":"in_progress","status_details":null,"output":[],"usage":null,"metadata":null}}
719+
720+
data: {"error":{"message":"Invalid request: missing required field"}}
721+
722+
SSE
723+
end
689724
end

0 commit comments

Comments
 (0)