Chat Completions Interaction Example
Chat Completions Interaction Example
Section titled “Chat Completions Interaction Example”Back to OpenAI Chat Completions.
This compact example mirrors the detailed Chinese walkthrough. It shows the Chat shape: candidate choices[], each containing one assistant delta/message and optional tool_calls[].
- 1
client requestClient sends ordered `messages`, a function tool, optional search-like request settings, and `stream: true`.
- 2
assistant deltaThe stream starts with a choice delta, usually carrying `role: assistant`.
- 3
tool call deltas`choices[].delta.tool_calls[]` carries function argument fragments.
- 4
finish_reason`finish_reason: tool_calls` marks that arguments are complete and local tools should run.
- 5
tool messageThe next request sends a `role: tool` message with the matching `tool_call_id`.
- 6
final answerThe final stream emits `delta.content`, then `finish_reason: stop`, then `[DONE]`.
Join keys
Section titled “Join keys”| Entity | Join key | Notes |
|---|---|---|
choice | choices[].index | Different candidate answers can interleave. |
tool call | choice.index + tool_calls[].index | Later argument chunks may omit the tool id/name. |
tool result | tool_call_id | The next request returns local tool output with this id. |
stream end | finish_reason then [DONE] | Chat streams use a data-only sentinel after terminal chunks. |