Default BPE cache directory to Path.GetTempPath() to avoid infrastructure side effects#22
Default BPE cache directory to Path.GetTempPath() to avoid infrastructure side effects#22sachinsatav wants to merge 1 commit intoaiqinxuancai:masterfrom
Conversation
Summary of ChangesHello @sachinsatav, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request modifies the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly changes the default BPE cache directory to use Path.GetTempPath(), which resolves a critical issue with application restarts in environments like Azure Functions. The change is simple and effective. I've added a couple of comments pointing out opportunities to improve maintainability and robustness in areas related to this change: updating documentation, avoiding duplicated code, and strengthening the error handling for cache directory creation. Overall, this is a valuable improvement.
| { | ||
| var fileName = Path.GetFileName(tikTokenBpeFile); | ||
| var saveDir = PBEFileDirectory; //Path.Combine(AppContext.BaseDirectory, "bpe"); | ||
| var saveDir = PBEFileDirectory; //Path.Combine(Path.GetTempPath(), "bpe"); |
There was a problem hiding this comment.
The change to using a temporary path is a good fix. However, the error handling logic that uses saveDir could be more robust. In the try-catch block on lines 317-328, if creating the bpe subdirectory fails, the code falls back to using the root of the temp path (Path.GetTempPath()). This causes the bpe subdirectory to be lost, and files will be saved directly in the temp root. This could lead to file name collisions or pollute the directory. It would be better to handle this exception more gracefully, for example by re-throwing it, as writing to the temp root might not be desirable.
| /// You can set this item before EncodingForModel to specify the location for storing and downloading the bpe file. If not set, it defaults to the AppContext.BaseDirectory\bpe directory. | ||
| /// </summary> | ||
| public static string PBEFileDirectory { get; set; } = Path.Combine(AppContext.BaseDirectory, "bpe"); | ||
| public static string PBEFileDirectory { get; set; } = Path.Combine(Path.GetTempPath(), "bpe"); |
There was a problem hiding this comment.
While this change is correct, there are a couple of related improvements to consider for better maintainability:
- Outdated Documentation: The XML comment for this property on line 28 is now outdated. It still refers to
AppContext.BaseDirectory. Please update it to reflect the new default (Path.GetTempPath()). - Code Duplication: This default path is also defined in
TiktokenSharp/Services/EncodingManager.cs. Duplicating this string can make future changes more difficult. Consider defining this default path in a single, shared location.
|
To ensure consistency for users of the original library, I will fix the rollback logic instead of simply changing it to use the temp directory, so that it can correctly write to temp when the base directory is unavailable. |
Cool thanks. |
The library defaults to creating the bpe cache folder in AppContext.BaseDirectory. In Azure Functions, writing to the app folder at runtime triggers an automatic host restart. This kills any currently running tasks, which in my case caused 30-minute hangs while the system waited for the "interrupted" work to timeout.
2026-02-20T16:01:01.063007532Z info: Host.Startup[0]
2026-02-20T16:01:01.063046432Z Directory change of type 'Created' detected for '/home/site/wwwroot/bpe'
2026-02-20T16:01:01.063063633Z info: Host.Startup[0]
2026-02-20T16:01:01.063067933Z Host configuration has changed. Signaling restart
Solution
I’ve changed the default cache path to Path.GetTempPath(). This is a standard practice for library caches and ensures that initializing the tokenizer doesn't trigger unexpected infrastructure restarts in cloud environments.