Skip to content

fix(cache): respect TIKTOKEN_OFFLINE env var to prevent network downloads#527

Open
xodn348 wants to merge 1 commit intoopenai:mainfrom
xodn348:fix/issue-513-env-vars
Open

fix(cache): respect TIKTOKEN_OFFLINE env var to prevent network downloads#527
xodn348 wants to merge 1 commit intoopenai:mainfrom
xodn348:fix/issue-513-env-vars

Conversation

@xodn348
Copy link
Copy Markdown

@xodn348 xodn348 commented Apr 18, 2026

Summary

Fixes #513

When TIKTOKEN_OFFLINE is set and a required file is not found in the cache, tiktoken previously ignored the env var and silently attempted a network download. This PR adds a guard that raises a clear ValueError before the read_file() network call, directing users to pre-populate the cache or set TIKTOKEN_CACHE_DIR.

Changes

  • tiktoken/load.py: Added TIKTOKEN_OFFLINE check in read_file_cached() before the network download path. When the env var is set and the file is missing from cache, a ValueError is raised with an actionable error message.

Behavior

Scenario Before After
TIKTOKEN_OFFLINE set, file cached Works Works (no change)
TIKTOKEN_OFFLINE set, file NOT cached Silent network request ValueError with clear message
TIKTOKEN_OFFLINE not set Normal download Normal download (no change)
TIKTOKEN_CACHE_DIR set Already respected Still respected (no change)

Testing

All 33 existing tests pass.

…oads (openai#513)

When TIKTOKEN_OFFLINE is set and a cache miss occurs, tiktoken now raises
a ValueError instead of silently attempting a network download. This allows
users in air-gapped or offline environments to get a clear error message
directing them to pre-populate the cache.

TIKTOKEN_CACHE_DIR was already respected; this commit adds the missing
TIKTOKEN_OFFLINE guard before the read_file() network call.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] tiktoken 0.9.0 ignores TIKTOKEN_OFFLINE and TIKTOKEN_CACHE_DIR, still attempts network download

1 participant