@@ -150,7 +150,8 @@ def init_langchain_model(
150
150
151
151
# Track the last exception for better error reporting
152
152
last_exception = None
153
-
153
+ # but also remember the first import‐error we see
154
+ first_import_error : Optional [ImportError ] = None
154
155
# Try each initializer in sequence
155
156
for initializer in initializers :
156
157
try :
@@ -163,14 +164,34 @@ def init_langchain_model(
163
164
)
164
165
if result is not None :
165
166
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 } " )
166
173
except Exception as e :
167
- # Keep track of the last exception
168
174
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 )
174
195
175
196
176
197
def _init_chat_completion_model (
0 commit comments