griptape icon indicating copy to clipboard operation
griptape copied to clipboard

Remove Use Of `import_optional_dependency`

Open collindutter opened this issue 11 months ago • 3 comments

Is your feature request related to a problem? Please describe. import_optional_dependency was introduced in order to safely import optional dependencies while raising a helpful error message. Unfortunately this function removes the type hints on imports, and requires weird import patterns.

Describe the solution you'd like Refactor the optional import dependency mechanism to use more native python functionality. I.e.

try:
  import anthropic
catch ImportError:
  raise "You need to install the extra for anthropic"

Or

def create_missing_extra_class(message):
    class MissingExtra:
        def __init__(self):
            raise Exception(message)
    return MissingExtra

if is_dependency_installed("anthropic"):
    from .prompt.anthropic_prompt_driver import AnthropicPromptDriver
else:
    AnthropicPromptDriver = create_missing_extra_class("You missed the anthropic-whatever extra!")


__all__ = ['AnthropicPromptDriver']

Describe alternatives you've considered Adding type hint support to import_optional_dependency. Doesn't seem possible, and is still clunky to use.

collindutter avatar Dec 19 '24 20:12 collindutter

it seems like import_optional_dependency is just a convenience around the first option no? if we can get type hints to work as expected, we should keep it around

vachillo avatar Dec 20 '24 03:12 vachillo

Roughly, but the larger issue is how we use import_optional_dependency. We have all imports done lazily rather than eagerly at the top of the file. I think my ideal is something like:

if is_dependency_installed("anthropic"):
    from anthropic import Client
else:
    raise MissingExtraError("drivers-anthropic-prompt")

Feels very un-magical.

The challenge with moving away from lazy imports is we'd have to update our import scheme to something like:

from griptape.drivers.prompt.anthropic import AnthropicPromptDriver

If we wanted to keep the from griptape.drivers import AnthropicPromptDriver syntax we'd need to go down the path of creating the dummy classes that fail at runtime.

collindutter avatar Dec 20 '24 16:12 collindutter

I don't think we can solve this without moving to namespaced imports. I tried the first proposed solution and it took a considerable amount of time to:

  1. Check whether each dependency is installed.
  2. Import that (sometimes heavy) dependency.

collindutter avatar Jan 21 '25 20:01 collindutter