Skip to content

Commit 7ad7e60

Browse files
committed
fix: raise Import Errors explicitly
correct regex pattern in test for model initialization
1 parent c925b04 commit 7ad7e60

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

nemoguardrails/llm/models/langchain_initializer.py

+28-7
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ def init_langchain_model(
150150

151151
# Track the last exception for better error reporting
152152
last_exception = None
153-
153+
# but also remember the first import‐error we see
154+
first_import_error: Optional[ImportError] = None
154155
# Try each initializer in sequence
155156
for initializer in initializers:
156157
try:
@@ -163,14 +164,34 @@ def init_langchain_model(
163164
)
164165
if result is not None:
165166
return result
167+
except ImportError as e:
168+
# remember only the first import‐error we encounter
169+
if first_import_error is None:
170+
first_import_error = e
171+
last_exception = e
172+
log.debug(f"Initialization import‐failure in {initializer}: {e}")
166173
except Exception as e:
167-
# Keep track of the last exception
168174
last_exception = e
169-
log.debug(f"Initialization failed with {initializer}: {str(e)}")
170-
171-
raise ModelInitializationError(
172-
f"Failed to initialize model {model_name} with provider {provider_name} in {mode} mode"
173-
) from last_exception
175+
log.debug(f"Initialization failed with {initializer}: {e}")
176+
# build the final message, preferring that first ImportError if we saw one
177+
base = (
178+
f"Failed to initialize model {model_name!r} "
179+
f"with provider {provider_name!r} in {mode!r} mode"
180+
)
181+
182+
# if we ever hit an ImportError, surface its message:
183+
if first_import_error is not None:
184+
base += f": {first_import_error}"
185+
# chain from that importer
186+
raise ModelInitializationError(base) from first_import_error
187+
188+
# otherwise fall back to the last exception we saw
189+
if last_exception is not None:
190+
base += f": {last_exception}"
191+
raise ModelInitializationError(base) from last_exception
192+
193+
# (should never happen—no initializer claimed success, no exception thrown)
194+
raise ModelInitializationError(base)
174195

175196

176197
def _init_chat_completion_model(

tests/llm_providers/test_langchain_initializer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def test_all_initializers_raise_exceptions(mock_initializers):
143143
mock_initializers["community"].side_effect = ImportError("Community model failed")
144144
mock_initializers["text"].side_effect = KeyError("Text model failed")
145145
with pytest.raises(
146-
ModelInitializationError, match="Failed to initialize model unknown-model"
146+
ModelInitializationError, match=r"Failed to initialize model 'unknown-model'"
147147
):
148148
init_langchain_model("unknown-model", "provider", "chat", {})
149149
mock_initializers["special"].assert_called_once()

0 commit comments

Comments
 (0)