Skip to content

Responses (streaming)

This example shows how to consume server-sent events (SSE) from POST /v1/responses with stream: true.

bash
curl https://api.fastapi.ai/v1/responses \
  -H "Authorization: Bearer $FAST_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4.1",
    "input": "Stream a short greeting.",
    "stream": true
  }'
javascript
const res = await fetch('https://api.fastapi.ai/v1/responses', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.FAST_API_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ model: 'gpt-4.1', input: 'Stream a short greeting.', stream: true }),
});

if (!res.ok) {
  console.error('HTTP', res.status, await res.text());
  process.exit(1);
}

const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = '';

// Print only text deltas when available.
const write = (s) => process.stdout.write(s);

while (true) {
  const { value, done } = await reader.read();
  if (done) break;
  buffer += decoder.decode(value, { stream: true });

  // SSE events are separated by a blank line.
  const parts = buffer.split('\n\n');
  buffer = parts.pop() ?? '';

  for (const part of parts) {
    const dataLines = part
      .split('\n')
      .filter((l) => l.startsWith('data:'))
      .map((l) => l.slice(5).trim());

    for (const data of dataLines) {
      if (!data || data === '[DONE]') continue;
      try {
        const ev = JSON.parse(data);
        if (ev?.type === 'response.output_text.delta' && typeof ev?.delta === 'string') {
          write(ev.delta);
          continue;
        }
        console.log(ev);
      } catch {
        console.log(data);
      }
    }
  }
}
python
import os
import json
import requests

resp = requests.post(
  "https://api.fastapi.ai/v1/responses",
  headers={
    "Authorization": f"Bearer {os.environ['FAST_API_KEY']}",
    "Content-Type": "application/json",
  },
  json={"model": "gpt-4.1", "input": "Stream a short greeting.", "stream": True},
  stream=True,
)

for line in resp.iter_lines():
  if not line:
    continue
  text = line.decode("utf-8")
  if not text.startswith("data:"):
    continue
  data = text[5:].strip()
  if not data or data == "[DONE]":
    continue
  try:
    ev = json.loads(data)
    if ev.get("type") == "response.output_text.delta" and isinstance(ev.get("delta"), str):
      print(ev["delta"], end="")
    else:
      print(ev)
  except json.JSONDecodeError:
    print(data)
go
package main

import (
  "bufio"
  "bytes"
  "encoding/json"
  "fmt"
  "net/http"
  "os"
  "strings"
)

type StreamEvent struct {
  Type  string `json:"type"`
  Delta string `json:"delta"`
}

func main() {
  payload := map[string]any{
    "model":  "gpt-4.1",
    "input":  "Stream a short greeting.",
    "stream": true,
  }
  b, _ := json.Marshal(payload)

  req, _ := http.NewRequest("POST", "https://api.fastapi.ai/v1/responses", bytes.NewReader(b))
  req.Header.Set("Authorization", "Bearer "+os.Getenv("FAST_API_KEY"))
  req.Header.Set("Content-Type", "application/json")

  resp, err := http.DefaultClient.Do(req)
  if err != nil {
    panic(err)
  }
  defer resp.Body.Close()

  scanner := bufio.NewScanner(resp.Body)
  for scanner.Scan() {
    line := scanner.Text()
    if strings.HasPrefix(line, "data:") {
      data := strings.TrimSpace(strings.TrimPrefix(line, "data:"))
      if data == "" || data == "[DONE]" {
        continue
      }
      var ev StreamEvent
      if err := json.Unmarshal([]byte(data), &ev); err == nil {
        if ev.Type == "response.output_text.delta" && ev.Delta != "" {
          fmt.Print(ev.Delta)
          continue
        }
      }
      fmt.Println(data)
    }
  }
}
java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Main {
  public static void main(String[] args) throws Exception {
    String body = "{\"model\":\"gpt-4.1\",\"input\":\"Stream a short greeting.\",\"stream\":true}";

    HttpRequest req = HttpRequest.newBuilder()
      .uri(URI.create("https://api.fastapi.ai/v1/responses"))
      .header("Authorization", "Bearer " + System.getenv("FAST_API_KEY"))
      .header("Content-Type", "application/json")
      .POST(HttpRequest.BodyPublishers.ofString(body))
      .build();

    HttpResponse<java.io.InputStream> resp =
      HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofInputStream());

    try (BufferedReader reader = new BufferedReader(new InputStreamReader(resp.body()))) {
      String line;
      while ((line = reader.readLine()) != null) {
        if (!line.startsWith("data:")) continue;
        String data = line.substring(5).trim();
        if (data.isEmpty() || "[DONE]".equals(data)) continue;
        if (data.contains("\"type\":\"response.output_text.delta\"") && data.contains("\"delta\":\"")) {
          int start = data.indexOf("\"delta\":\"") + 9;
          int end = data.indexOf("\"", start);
          if (start >= 9 && end > start) {
            System.out.print(data.substring(start, end));
            continue;
          }
        }
        System.out.println(data);
      }
    }
  }
}

那年我双手插兜, 让bug稳如老狗