Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/openai/internal/stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Stream
else
"An error occurred during streaming"
end
err = OpenAI::Errors::APIError.for(
err = OpenAI::Errors::APIStatusError.for(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh my god I am so sorry, this is such an embarrassing bug 🐛.

🤦‍♀️

url: @url,
status: @status,
body: data,
Expand Down
35 changes: 35 additions & 0 deletions test/openai/resources/responses/streaming_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,31 @@ def test_incomplete_streaming
end
end

def test_streaming_error_handling
stub_request(:post, "http://localhost/responses")
.with(
body: hash_including(
instructions: "You are a helpful assistant",
messages: [{content: "Hello", role: "user"}],
model: "gpt-4",
stream: true
)
)
.to_return(
status: 400,
headers: {"Content-Type" => "text/event-stream"},
body: error_sse_response
)

stream = @client.responses.stream(**basic_params)
error = assert_raises(OpenAI::Errors::APIStatusError) do
stream.each { |_event| } # Consume the stream to trigger the error.
end

assert_equal(400, error.status)
assert_equal("Invalid request: missing required field", error.message)
end

def test_text_method
stub_streaming_response(basic_text_sse_response)

Expand Down Expand Up @@ -686,4 +711,14 @@ def resume_stream_structured_output_sse_response

SSE
end

def error_sse_response
<<~SSE
event: response.created
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}}

data: {"error":{"message":"Invalid request: missing required field"}}

SSE
end
end