Prewarm tiktoken cache to avoid Foundry egress#18
Conversation
Avoid runtime DNS/egress failures by caching cl100k_base during image build and setting TIKTOKEN_CACHE_DIR in the container.
|
🚅 Deployed to the dspy-reference-examples-pr-18 environment in dspy-reference-example
|
WalkthroughThe Dockerfile now pre-warms the tiktoken encoding cache (initializes cl100k_base) during the builder stage and creates a dspy cache; those cache directories are copied into the runtime image with TIKTOKEN_CACHE_DIR/PYTHONPATH/PATH adjustments and ownership set for the application user. Changes
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 Comment |
Use /app/.venv/bin/python so the build step can import tiktoken after uv sync.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@Dockerfile`:
- Around line 19-22: The Dockerfile pre-warm step uses the system python which
lacks tiktoken; change the command to run the virtualenv interpreter under
/app/.venv so TIKTOKEN_CACHE_DIR=/app/.tiktoken_cache python -c 'import
tiktoken; tiktoken.get_encoding("cl100k_base")' uses /app/.venv/bin/python
instead, ensuring the tiktoken import and tiktoken.get_encoding call succeed
during build.
🧹 Nitpick comments (1)
Dockerfile (1)
42-45: Minor: Redundant directory creation for.tiktoken_cache.The
/app/.tiktoken_cachedirectory is already copied from the builder on line 42 with correct ownership (--chown=5000:5000), so creating it again on line 45 is unnecessary.♻️ Proposed cleanup
COPY --from=builder --chown=5000:5000 /app/.tiktoken_cache /app/.tiktoken_cache COPY --chown=5000:5000 pyproject.toml uv.lock README.md ./ -RUN mkdir -p /app/data/.dspy_cache /app/.tiktoken_cache && chown -R 5000:5000 /app +RUN mkdir -p /app/data/.dspy_cache && chown -R 5000:5000 /app/data
Why\nFoundry compute modules run with restricted outbound network. At runtime, tiktoken (via litellm) attempts to download the cl100k_base encoding from openaipublic.blob.core.windows.net, causing DNS/egress failures and 500s on classification routes.\n\n## What changed\n- Pre-warm the cl100k_base encoding during image build and copy it into the runtime image.\n- Set
TIKTOKEN_CACHE_DIRin the runtime container to ensure the cached encoding is used.\n\n## Notes\nThis removes the need to allow egress to openaipublic.blob.core.windows.net just for tokenizer initialization.Summary by CodeRabbit