Skip to content

Typescript error while counting tokens #1

Description

@pitininja

Hello,

When using the code shown in the readme :

import Tokenizer, { models } from 'ai-tokenizer';
import { count } from 'ai-tokenizer/sdk';
import * as encoding from 'ai-tokenizer/encoding';

const model = models['openai/gpt-5'];
const tokenizer = new Tokenizer(encoding[model.encoding]);
const result = count({
    tokenizer,
    model,
    messages: []
});

I have a Typescript error :

Type 'import("node_modules/ai-tokenizer/dist/index").Tokenizer' is not assignable to type 'Tokenizer'.
  Types have separate declarations of a private property 'name'.ts(2322)

My assumption would be that the Tokenizer class from ai-tokenizer and the Tokenizer class from ai-tokenizer/sdk have the same code but are separated class declarations, therefore are not type-compatible.

To fix this I guess there should be one and unique Tokenizer class used across all packages (but I can be mistaken).

In the meantime as a workaround I have to use the ugly :

tokenizer as unknown as Tokenizer,

which is obviously dark magic.

Thanks for your time

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions