-
-
Notifications
You must be signed in to change notification settings - Fork 1k
perf: lazy load top-level instructor exports #2264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jxnl
wants to merge
2
commits into
main
Choose a base branch
from
codex/import-ram-2205
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,47 @@ | ||
| """Core components of the instructor package.""" | ||
|
|
||
| from .client import Instructor, AsyncInstructor, Response, from_openai, from_litellm | ||
| from .exceptions import ( | ||
| InstructorRetryException, | ||
| InstructorError, | ||
| ConfigurationError, | ||
| IncompleteOutputException, | ||
| ValidationError, | ||
| ProviderError, | ||
| ModeError, | ||
| ClientError, | ||
| AsyncValidationError, | ||
| FailedAttempt, | ||
| ResponseParsingError, | ||
| MultimodalError, | ||
| ) | ||
| from .hooks import Hooks, HookName | ||
| from .patch import patch, apatch | ||
| from .retry import retry_sync, retry_async | ||
|
|
||
| __all__ = [ | ||
| "Instructor", | ||
| "AsyncInstructor", | ||
| "Response", | ||
| "InstructorRetryException", | ||
| "InstructorError", | ||
| "ConfigurationError", | ||
| "IncompleteOutputException", | ||
| "ValidationError", | ||
| "ProviderError", | ||
| "ModeError", | ||
| "ClientError", | ||
| "AsyncValidationError", | ||
| "FailedAttempt", | ||
| "ResponseParsingError", | ||
| "MultimodalError", | ||
| "Hooks", | ||
| "HookName", | ||
| "patch", | ||
| "apatch", | ||
| "from_openai", | ||
| "from_litellm", | ||
| "retry_sync", | ||
| "retry_async", | ||
| ] | ||
| import importlib | ||
| from typing import Any | ||
|
|
||
| _LAZY_ATTRS: dict[str, tuple[str, str]] = { | ||
| "Instructor": (".client", "Instructor"), | ||
| "AsyncInstructor": (".client", "AsyncInstructor"), | ||
| "Response": (".client", "Response"), | ||
| "from_openai": (".client", "from_openai"), | ||
| "from_litellm": (".client", "from_litellm"), | ||
| "InstructorRetryException": (".exceptions", "InstructorRetryException"), | ||
| "InstructorError": (".exceptions", "InstructorError"), | ||
| "ConfigurationError": (".exceptions", "ConfigurationError"), | ||
| "IncompleteOutputException": (".exceptions", "IncompleteOutputException"), | ||
| "ValidationError": (".exceptions", "ValidationError"), | ||
| "ProviderError": (".exceptions", "ProviderError"), | ||
| "ModeError": (".exceptions", "ModeError"), | ||
| "ClientError": (".exceptions", "ClientError"), | ||
| "AsyncValidationError": (".exceptions", "AsyncValidationError"), | ||
| "FailedAttempt": (".exceptions", "FailedAttempt"), | ||
| "ResponseParsingError": (".exceptions", "ResponseParsingError"), | ||
| "MultimodalError": (".exceptions", "MultimodalError"), | ||
| "Hooks": (".hooks", "Hooks"), | ||
| "HookName": (".hooks", "HookName"), | ||
| "patch": (".patch", "patch"), | ||
| "apatch": (".patch", "apatch"), | ||
| "retry_sync": (".retry", "retry_sync"), | ||
| "retry_async": (".retry", "retry_async"), | ||
| } | ||
|
|
||
| __all__ = list(_LAZY_ATTRS) | ||
|
|
||
|
|
||
| def __getattr__(name: str) -> Any: | ||
| if name not in _LAZY_ATTRS: | ||
| raise AttributeError(f"module '{__name__}' has no attribute '{name}'") | ||
|
|
||
| module_name, attr_name = _LAZY_ATTRS[name] | ||
| module = importlib.import_module(module_name, __name__) | ||
| value = getattr(module, attr_name) | ||
| globals()[name] = value | ||
| return value | ||
|
|
||
|
|
||
| def __dir__() -> list[str]: | ||
| return sorted(set(globals()) | set(__all__)) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,35 @@ | ||
| """Processing components for request/response handling.""" | ||
|
|
||
| from .function_calls import OpenAISchema, openai_schema | ||
| from .multimodal import convert_messages | ||
| from .response import ( | ||
| handle_response_model, | ||
| process_response, | ||
| process_response_async, | ||
| handle_reask_kwargs, | ||
| ) | ||
| from .schema import ( | ||
| generate_openai_schema, | ||
| generate_anthropic_schema, | ||
| generate_gemini_schema, | ||
| ) | ||
| from .validators import Validator | ||
|
|
||
| __all__ = [ | ||
| "OpenAISchema", | ||
| "openai_schema", | ||
| "convert_messages", | ||
| "handle_response_model", | ||
| "process_response", | ||
| "process_response_async", | ||
| "handle_reask_kwargs", | ||
| "generate_openai_schema", | ||
| "generate_anthropic_schema", | ||
| "generate_gemini_schema", | ||
| "Validator", | ||
| ] | ||
| import importlib | ||
| from typing import Any | ||
|
|
||
| _LAZY_ATTRS: dict[str, tuple[str, str]] = { | ||
| "OpenAISchema": (".function_calls", "OpenAISchema"), | ||
| "openai_schema": (".function_calls", "openai_schema"), | ||
| "convert_messages": (".multimodal", "convert_messages"), | ||
| "handle_response_model": (".response", "handle_response_model"), | ||
| "process_response": (".response", "process_response"), | ||
| "process_response_async": (".response", "process_response_async"), | ||
| "handle_reask_kwargs": (".response", "handle_reask_kwargs"), | ||
| "generate_openai_schema": (".schema", "generate_openai_schema"), | ||
| "generate_anthropic_schema": (".schema", "generate_anthropic_schema"), | ||
| "generate_gemini_schema": (".schema", "generate_gemini_schema"), | ||
| "Validator": (".validators", "Validator"), | ||
| } | ||
|
|
||
| __all__ = list(_LAZY_ATTRS) | ||
|
|
||
|
|
||
| def __getattr__(name: str) -> Any: | ||
| if name not in _LAZY_ATTRS: | ||
| raise AttributeError(f"module '{__name__}' has no attribute '{name}'") | ||
|
|
||
| module_name, attr_name = _LAZY_ATTRS[name] | ||
| module = importlib.import_module(module_name, __name__) | ||
| value = getattr(module, attr_name) | ||
| globals()[name] = value | ||
| return value | ||
|
|
||
|
|
||
| def __dir__() -> list[str]: | ||
| return sorted(set(globals()) | set(__all__)) |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lazy
hooksresolution fails viagetattron submoduleHigh Severity
The lazy entry
"hooks": (".core", "hooks")tries to resolveinstructor.hooksby importinginstructor.coreand then callinggetattr(module, "hooks"). However,instructor.core.__getattr__only handles names in its_LAZY_ATTRSdict, which contains"Hooks"and"HookName"but not the submodule name"hooks". Unlike thefrom .core import hooksimport statement (which has a submodule fallback),getattron a module with__getattr__does not attempt submodule resolution per PEP 562. This causesAttributeErrorwhen accessinginstructor.hooksunless another lazy attribute has already triggered importinginstructor.core.hooksas a side effect. The mapping needs to be(".core.hooks", None)to directly import the submodule.Additional Locations (1)
instructor/core/__init__.py#L5-L30Reviewed by Cursor Bugbot for commit b743282. Configure here.