openai / openai/tiktoken

Bug in buffer protocol implementation

Open
#405 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
19.3k
Forks
1.6k
PR merge metrics
No merged PRs in 30d

Description

While inspecting the code, I noticed a small bug in the buffer protocol implementation - the buffer's .shape is equal to num_tokens * buffer.itemsize, but instead it should be num_tokens to ensure math.prod(buffer.shape) * buffer.itemsize == len(buffer) afterward, as per the official spec. This then leads to memoryview(buffer).tolist() returning an incorrect result. Luckily, NumPy ignores the .shape (unlike CPython) and builds the result using len(buffer) // buffer.itemsize.

Even though the encode_to_tiktoken_buffer API is somewhat private, this bug is probably still worth fixing to follow the spec 😃.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the encode_to_tiktoken_buffer implementation and compare its buffer .shape value with the official buffer protocol specification. Verify the corrected shape using memoryview(buffer).tolist() and the invariant math.prod(buffer.shape) * buffer.itemsize == len(buffer); done means CPython reports the expected token sequence.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.