本文へスキップ

ストリーミングイベント

stream: true のときの 3 つのプロトコルの SSE イベント形式、HopBase が挿入するキープアライブのコメント、ストリーム中のエラーの通知方法。

リクエストボディで "stream": true を指定すると、チャット補完を作成、レスポンスを作成、メッセージを作成は text/event-stream(SSE)を返すようになります。イベント形式は各プロトコルの公式定義のままなので、公式 SDK のストリーミング API をそのまま使えます。HopBase はキープアライブのコメントを挿入するだけで、イベントの内容は変更しません。

POST/v1/chat/completionsPOST/v1/responsesPOST/v1/messages

キープアライブのコメント

ストリーミング中、HopBase は約 10 秒ごとにイベントとイベントの間に 1 行の SSE コメントを書き込み、長い推論の間に中間プロキシが接続をアイドルと判断して切断するのを防ぎます:

: hopbase-keepalive

コロンで始まる行は SSE 仕様上のコメントで、公式 SDK や標準の SSE パーサーは無視します。自分で行単位に解析する場合は : で始まる行を読み飛ばし、JSON として解析しないでください。モデルが長時間まったく出力しない場合(約 90〜150 秒、モデルにより異なる)、ゲートウェイは停止と判定して再試行またはエラーを返します。詳しくは同時実行数、タイムアウト、課金を参照してください。

Chat Completions

各イベントは 1 行の data: で、内容は chat.completion.chunk オブジェクトです。テキストは choices[0].delta.content にあり、最後は data: [DONE] で終わります。

data: {"id":"chatcmpl-EXAMPLE","object":"chat.completion.chunk","model":"gpt-5.6-sol","choices":[{"index":0,"delta":{"role":"assistant","content":"私は"},"finish_reason":null}]}

data: {"id":"chatcmpl-EXAMPLE","object":"chat.completion.chunk","model":"gpt-5.6-sol","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: {"id":"chatcmpl-EXAMPLE","object":"chat.completion.chunk","model":"gpt-5.6-sol","choices":[],"usage":{"prompt_tokens":14,"completion_tokens":52,"total_tokens":66}}

data: [DONE]
フィールド説明
choices[].delta.contentこのチャンクのテキスト
choices[].delta.tool_callsツール呼び出しの引数は分割して届くため、index ごとに function.arguments を連結します
choices[].finish_reason最後のチャンクでのみ非空:stop / length / tool_calls
usageリクエストで stream_options.include_usage: true を指定した場合のみ、choices が空の最後のイベントで届きます。指定しなくても課金には影響しません

Responses

各イベントは event: 行と data: 行で構成され、data 内の type はイベント名と同じです。

event: response.created
data: {"type":"response.created","response":{"id":"resp_EXAMPLE","status":"in_progress"}}

event: response.output_text.delta
data: {"type":"response.output_text.delta","item_id":"msg_EXAMPLE","output_index":0,"content_index":0,"delta":"私は"}

event: response.completed
data: {"type":"response.completed","response":{"id":"resp_EXAMPLE","status":"completed","usage":{"input_tokens":14,"output_tokens":58,"total_tokens":72}}}
イベント説明
response.createdレスポンスの開始
response.output_text.delta出力テキストの断片。delta に入ります
response.output_text.done1 つのコンテンツの終了。text が完全なテキストです
response.completed正常終了。response.usage が使用量です
response.failed途中で失敗。理由は response.error.code:rate_limit_exceeded / server_error / invalid_prompt

Anthropic Messages

イベントの順序:message_start → コンテンツブロックごとに content_block_start / content_block_delta / content_block_stop → message_delta(stop_reason と出力の使用量を含む)→ message_stop。

event: message_start
data: {"type":"message_start","message":{"id":"msg_EXAMPLE","role":"assistant","model":"claude-sonnet-5","content":[],"usage":{"input_tokens":9,"output_tokens":1}}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"こんにちは!"}}

event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":14}}

event: message_stop
data: {"type":"message_stop"}

ストリーム中のエラー

ストリームが出力を始めた時点で HTTP ステータスコードはすでに 200 になっており、以降のエラーはイベントとして通知され、ステータスコードは変わりません:

Chat Completions   data: {"error": {…}}      (event: 行なし)
Responses          event: response.failed
Anthropic          event: error

Claude のストリームが途中で切断された場合、HopBase は Anthropic のエラーイベントを 1 つ追加で送ってから終了します:

event: error
data: {"type":"error","error":{"type":"api_error","message":"Response stream interrupted, please retry"}}

クライアントはストリームの読み取りループ内でエラーイベントを処理してください。受信済みの断片から再開することはできず、リクエスト全体を送り直す必要があります。中断前に生成された分は使用量に応じて課金されます。再試行の方針はエラーコードと再試行を参照してください。