fix(processing): lazy-load provider imports in response.py to reduce import-time RAM usage#2268
Open
chaosreload wants to merge 1 commit into567-labs:mainfrom
Open
Conversation
…RAM on import Move all 11 provider util imports (anthropic, bedrock, cerebras, cohere, fireworks, gemini, mistral, openai, perplexity, writer, xai) behind TYPE_CHECKING and load them on demand via importlib.import_module. This prevents ~1670 modules from being eagerly loaded when importing instructor, significantly reducing initial RAM usage. Closes 567-labs#2205 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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
Continuing the work started in #2206.
PR #2206 moved the top-level provider imports in
__init__.pyto lazy loading — great first step. However,instructor/processing/response.pystill eagerly imports all 11 provider utils at module load time, which in turn pulls in the full provider SDKs (anthropic, bedrock, cohere, gemini, mistral, etc.).As analyzed by @kiyeonjeon21 in #2205:
Root cause
instructor/processing/response.pyhad 11 top-level provider import blocks:These are only needed when a specific provider's mode handler is actually invoked — not at import time.
Changes
instructor/processing/response.py— only file changed:TYPE_CHECKINGblock (type checkers still work correctly)_lazy(module_path, func_name)helper backed by_provider_cache— usesimportlib.import_moduleon first call, cached thereafterPARALLEL_MODES,mode_handlers,REASK_HANDLERS) updated to use_lazy(...)wrappersinstructor/batch/providers/__init__.py— already usesimportlib.util.find_specconditional imports, no changes needed.Verification
Provider SDKs are now only loaded when the corresponding mode handler is first invoked.
Fixes #2205