OpenAI Chat API TypeScript Types (ChatCompletion)
TypeScript interfaces for the OpenAI Chat Completions API response. Use these types when building AI features without installing the full openai SDK.
OpenAI Chat API — Sample JSON
{
"id": "chatcmpl-abc123xyz",
"object": "chat.completion",
"created": 1711928400,
"model": "gpt-4o",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 12,
"completion_tokens": 9,
"total_tokens": 21
},
"system_fingerprint": "fp_a24b4d720c"
}Generated TypeScript
export interface Message {
role: string;
content: string;
}
export interface ChoicesItem {
index: number;
message: Message;
logprobs: null | null;
finish_reason: string;
}
export interface Usage {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
}
export interface OpenAIChatCompletion {
id: string;
object: string;
created: number;
model: string;
choices: ChoicesItem[];
usage: Usage;
system_fingerprint: string;
}How to use
Copy the TypeScript above into a types.ts file in your project. Import the root interface and use it to type your API responses: const data: OpenAIChatCompletion = await res.json()
Working with a different JSON payload? Paste any JSON and generate TypeScript interfaces instantly.
Convert your own JSON →FAQ
How do I type the OpenAI API response without the openai npm package?
Use these generated interfaces for lightweight environments like Cloudflare Workers, Vercel Edge Functions, or Deno Deploy where you call the OpenAI API directly via fetch. The full openai package adds ~500 KB to your bundle.
Why is choices an array? Does OpenAI ever return multiple choices?
Yes — the n parameter in your request controls how many completions are generated per call. By default n=1, so choices has one element. Setting n=3 returns three independent completions for the same prompt. This is useful for picking the best response.
What does finish_reason 'stop' vs 'length' mean?
'stop' means the model finished naturally (hit the stop sequence or end of response). 'length' means the model was truncated because it hit the max_tokens limit. If you're seeing 'length' frequently, increase max_tokens in your request.