Skip to content

Commit 514f402

Browse files
authored
fix: import errors for models with optional imports (#1384)
* fix: import errors for models with optional imports * Addressed comments: added return type, changed error message * Addressed comments: updated imports
1 parent 3bc34ac commit 514f402

1 file changed

Lines changed: 56 additions & 1 deletion

File tree

src/strands/models/__init__.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,63 @@
33
This package includes an abstract base Model class along with concrete implementations for specific providers.
44
"""
55

6+
from typing import Any
7+
68
from . import bedrock, model
79
from .bedrock import BedrockModel
810
from .model import Model
911

10-
__all__ = ["bedrock", "model", "BedrockModel", "Model"]
12+
__all__ = [
13+
"bedrock",
14+
"model",
15+
"BedrockModel",
16+
"Model",
17+
]
18+
19+
20+
def __getattr__(name: str) -> Any:
21+
"""Lazy load model implementations only when accessed.
22+
23+
This defers the import of optional dependencies until actually needed.
24+
"""
25+
if name == "AnthropicModel":
26+
from .anthropic import AnthropicModel
27+
28+
return AnthropicModel
29+
if name == "GeminiModel":
30+
from .gemini import GeminiModel
31+
32+
return GeminiModel
33+
if name == "LiteLLMModel":
34+
from .litellm import LiteLLMModel
35+
36+
return LiteLLMModel
37+
if name == "LlamaAPIModel":
38+
from .llamaapi import LlamaAPIModel
39+
40+
return LlamaAPIModel
41+
if name == "LlamaCppModel":
42+
from .llamacpp import LlamaCppModel
43+
44+
return LlamaCppModel
45+
if name == "MistralModel":
46+
from .mistral import MistralModel
47+
48+
return MistralModel
49+
if name == "OllamaModel":
50+
from .ollama import OllamaModel
51+
52+
return OllamaModel
53+
if name == "OpenAIModel":
54+
from .openai import OpenAIModel
55+
56+
return OpenAIModel
57+
if name == "SageMakerAIModel":
58+
from .sagemaker import SageMakerAIModel
59+
60+
return SageMakerAIModel
61+
if name == "WriterModel":
62+
from .writer import WriterModel
63+
64+
return WriterModel
65+
raise AttributeError(f"cannot import name '{name}' from '{__name__}' ({__file__})")

0 commit comments

Comments
 (0)