Prompt

How do I set up a chat completion API for content drafting with streaming responses?

Latest observation

Jul 19, 2026 · OpenAI APIWeb search: off

To set up a chat completion API for content drafting with streaming responses, you generally need to:

  1. Choose your endpoint/model
  2. Send a chat-completion request with stream: true
  3. Read incremental tokens/chunks as they arrive
  4. Render them progressively in your UI

Example request

cURL

curl https://api.example.com/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4.1-mini",
    "messages": [
      { "role": "system", "content": "You are a helpful content drafting assistant." },
      { "role": "user", "content": "Draft a short blog intro about sustainable packaging." }
    ],
    "stream": true
  }'

JavaScript example

const response = await fetch("https://api.example.com/v1/chat/completions", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${process.env.API_KEY}`,
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    model: "gpt-4.1-mini",
    messages: [
      { role: "system", content: "You are a helpful content drafting assistant." },
      { role: "user", content: "Draft a short blog intro about sustainable packaging." }
    ],
    stream: true
  })
});

const reader = response.body.getReader();
const decoder = new TextDecoder();

let fullText = "";

while (true) {
  const { done, value } = await reader.read();
  if (done) break;

  const chunk = decoder.decode(value, { stream: true });
  // Depending on the API, this may be SSE/data: lines or plain chunked text
  console.log(chunk);

  fullText += chunk;
}

console.log("Final content:", fullText);

Python example

import requests

with requests.post(
    "https://api.example.com/v1/chat/completions",
    headers={
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json",
    },
    json={
        "model": "gpt-4.1-mini",
        "messages": [
            {"role": "system", "content": "You are a helpful content drafting assistant."},
            {"role": "user", "content": "Draft a short blog intro about sustainable packaging."}
        ],
        "stream": True
    },
    stream=True
) as r:
    for line in r.iter_lines(decode_unicode=True):
        if line:
            print(line)

Typical streaming format

Many chat APIs stream responses as Server-Sent Events (SSE) or chunked JSON. You’ll usually receive:

  • partial text deltas
  • a final “done” event
  • optional metadata like token usage or finish reason

Example SSE-like chunk:

data: {"choices":[{"delta":{"content":"Hello"},"index":0}]}

data: {"choices":[{"delta":{"content":" world"},"index":0}]}

data: [DONE]

Best practices for content drafting

  • Use a system prompt to define tone, style, and structure.
  • Ask for outline first if you want more controlled drafting.
  • Keep a conversation state if the user iterates on drafts.
  • Show a typing indicator in the UI.
  • Handle partial content and reconnect logic if streaming drops.

Recommended prompt pattern

{
  "messages": [
    {
      "role": "system",
      "content": "You are a content drafting assistant. Write clearly, concisely, and in an engaging tone."
    },
    {
      "role": "user",
      "content": "Draft a 150-word product description for a reusable water bottle."
    }
  ]
}

If you want, I can also provide:

  • a complete Node.js/Express streaming backend
  • a browser frontend that renders tokens live
  • or an example specifically for the OpenAI API format

Brands

Observation history

Unlock observation history

Create a free Obsurfable account to unlock historical responses, model comparisons, and deeper brand intelligence.

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, and Meta AI.