Skip to content

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

  1. Keep the token and model ID in environment variables.
  2. Call /v1/chat/completions with "stream":true in the JSON body.
  3. Use curl -N to disable client buffering and observe events line by line.
  4. In application code, parse only data: lines and append incremental content in order.
  5. 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.

OpenAI-compatible · Multimodal AI gateway