I’m trying to work with OpenAI’s completions endpoint and want to get streaming data instead of waiting for the complete response. When you set the stream parameter to true, the API sends back data in small pieces like how ChatGPT shows text as it generates it.
I found some examples for other programming languages but nothing specific for Java or Kotlin using OkHttp. Can someone show me how to properly handle these streaming responses in Java? I need to process each chunk as it arrives rather than getting everything at once.
What’s the best way to set up the HTTP client and parse the streaming data from OpenAI’s API?
From my experience implementing this in production, the most reliable approach involves using OkHttp’s enqueue method with a callback that processes the response body incrementally. You’ll need to create a custom ResponseBody wrapper that reads the stream chunk by chunk and parses the Server-Sent Events format. Each chunk typically contains multiple lines, so buffering becomes crucial. I found that maintaining a StringBuilder to accumulate partial JSON objects works well since sometimes the streaming data gets cut mid-object. Don’t forget to handle the content-type header properly and set up proper exception handling for network timeouts. The stream will end with a specific marker, so watch for that to clean up your connections properly.
The key challenge with OpenAI streaming is handling the response body as it arrives. I implemented this using OkHttp’s ResponseBody.source() method with a BufferedSource to read line by line. You need to parse each line that starts with “data:” and extract the JSON content. The tricky part is properly handling the connection lifecycle and ensuring you close resources when the stream ends with “data: [DONE]”. Make sure to set appropriate timeouts since streaming responses can take longer than regular API calls. Also worth noting that you’ll need to handle potential network interruptions gracefully by implementing proper error handling in your streaming loop.
i’ve been working with this lately and okhttp’s sse (server-sent events) support works pretty well for this. you’ll want to use EventSource from the okhttp-sse library. just make sure to handle the data parsing correctly since openai sends json chunks with "data: " prefix. works smoother than trying to parse raw response streams imo