Skip to content

Default BPE cache directory to Path.GetTempPath() to avoid infrastructure side effects#22

Open
sachinsatav wants to merge 1 commit intoaiqinxuancai:masterfrom
sachinsatav:feature/BpeDirectory
Open

Default BPE cache directory to Path.GetTempPath() to avoid infrastructure side effects#22
sachinsatav wants to merge 1 commit intoaiqinxuancai:masterfrom
sachinsatav:feature/BpeDirectory

Conversation

@sachinsatav
Copy link

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.

@gemini-code-assist
Copy link

Summary of Changes

Hello @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 TiktokenSharp library to improve its compatibility with cloud environments, specifically Azure Functions. By redirecting the default BPE cache directory to the system's temporary path, it resolves an issue where writing to the application's base directory would trigger host restarts, ensuring more stable and predictable operation.

Highlights

  • Default BPE Cache Directory: Changed the default location for the BPE cache directory from AppContext.BaseDirectory to Path.GetTempPath() to prevent unintended side effects in cloud environments like Azure Functions.
  • Azure Functions Compatibility: Addressed an issue where writing to AppContext.BaseDirectory in Azure Functions caused host restarts, leading to task interruptions and timeouts.

🧠 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
  • TiktokenSharp/Services/EncodingManager.cs
    • Updated the default PBEFileDirectory to use Path.GetTempPath() instead of AppContext.BaseDirectory.
    • Adjusted a commented-out line in LoadTikTokenBpe to reflect the new temporary path usage.
  • TiktokenSharp/TikToken.cs
    • Modified the default PBEFileDirectory to use Path.GetTempPath().
    • Updated the XML documentation for PBEFileDirectory to describe the new default behavior.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While this change is correct, there are a couple of related improvements to consider for better maintainability:

  1. 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()).
  2. 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.

@aiqinxuancai
Copy link
Owner

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.

@sachinsatav
Copy link
Author

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.

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.

2 participants