Explore Library
Code QuizIntermediate

Detecting Truncated Completions

Catch the wrong finish_reason check for truncated responses.

Codejavascript
const res = await client.chat.completions.create({
  model: "gpt-4o",
  messages,
  max_tokens: 100,
});

const choice = res.choices[0];
if (choice.finish_reason === "stop") {
  console.warn("Output was cut off by max_tokens!");
}

What is the bug in this truncation check?