fix: prewarm boundary tiktoken encoding at API startup#285
Open
rendigua2025-gif wants to merge 1 commit into
Open
fix: prewarm boundary tiktoken encoding at API startup#285rendigua2025-gif wants to merge 1 commit into
rendigua2025-gif wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #277.
The first
/api/v1/memory/addrequest could trigger an on-demandtiktokendownload for the boundary tokenizer encoding (o200k_base). If that network/TLS request failed, users saw a generic 500 from the write path even though no memory data had been written yet.This PR adds a small FastAPI lifespan provider that prewarms the boundary tokenizer during API startup.
Scope
tiktoken.get_encoding("o200k_base")during API startup./addfrom being the first place that discovers this cold-start dependency.This is not full offline support. If the encoding is not already cached and the environment cannot reach the tokenizer asset, startup will fail with a clearer tokenizer prewarm error. A separate change would be needed to bundle/cache tokenizer assets for fully offline deployments.
Tests
tests/unit/test_entrypoints/test_api/test_lifespans/test_boundary_tokenizer.pytests/unit/test_entrypoints/test_api/test_lifespans/test_llm.pytests/unit/test_entrypoints/test_api/test_lifespans/test_cascade.pyNote: I verified locally on Windows with a test-process-only
fcntlstub because the current repository still imports POSIXfcntlduring test collection on Windows.