Handle Streaming Chat Responses
Goal
Observe one complete streaming chat response from the command line.
Prerequisites
Complete the first API call and obtain a valid model ID from the model list.
Steps
- Keep the token and model ID in environment variables.
- Call
/v1/chat/completionswith"stream":truein the JSON body. - Use
curl -Nto disable client buffering and observe events line by line. - In application code, parse only
data:lines and append incremental content in order. - Close the reader after the terminal marker; preserve completed text and record the error on interruption.
Expected result
Content arrives in chunks and assembles into one complete response.
Troubleshooting
- Everything appears at the end: check client and reverse-proxy buffering.
- JSON parsing fails: strip the event prefix and skip the terminal marker.
- Text is duplicated: distinguish incremental fields from complete fields.