NaN token counts when using streamText with OpenAI models
Issue
I am using streamText with the OpenAI provider for the AI SDK and OpenAI models.
I use createOpenAI to create the provider instance.
When I try to get the token counts, I get NaN values.
Background
OpenAI introduced streamOptions parameters to enable token counts in the stream.
However, this was a breaking change for OpenAI-compatible providers,
and we therefore made it opt-in.
Solution
When you use createOpenAI, you can enable a strict compatibility model:
import { createOpenAI } from '@ai-sdk/openai';
const openai = createOpenAI({ compatibility: 'strict',});This will enable the token counts in the stream.
When you use the default openai provider instance, the setting is enabled by default.