mistralai 1.9.9__py3-none-any.whl → 1.9.11__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. mistralai/_version.py +3 -3
  2. mistralai/accesses.py +43 -108
  3. mistralai/agents.py +29 -68
  4. mistralai/audio.py +8 -3
  5. mistralai/basesdk.py +15 -5
  6. mistralai/batch.py +6 -3
  7. mistralai/beta.py +10 -5
  8. mistralai/chat.py +29 -68
  9. mistralai/classifiers.py +57 -144
  10. mistralai/conversations.py +143 -352
  11. mistralai/documents.py +137 -356
  12. mistralai/embeddings.py +21 -36
  13. mistralai/files.py +47 -176
  14. mistralai/fim.py +29 -68
  15. mistralai/fine_tuning.py +6 -3
  16. mistralai/jobs.py +49 -158
  17. mistralai/libraries.py +71 -178
  18. mistralai/mistral_agents.py +71 -180
  19. mistralai/mistral_jobs.py +41 -128
  20. mistralai/models/__init__.py +36 -3
  21. mistralai/models/apiendpoint.py +5 -0
  22. mistralai/models/embeddingrequest.py +5 -1
  23. mistralai/models/encodingformat.py +7 -0
  24. mistralai/models/httpvalidationerror.py +11 -6
  25. mistralai/models/mistralerror.py +26 -0
  26. mistralai/models/no_response_error.py +13 -0
  27. mistralai/models/responsevalidationerror.py +25 -0
  28. mistralai/models/sdkerror.py +30 -14
  29. mistralai/models/systemmessage.py +7 -3
  30. mistralai/models/systemmessagecontentchunks.py +21 -0
  31. mistralai/models_.py +71 -204
  32. mistralai/ocr.py +15 -36
  33. mistralai/sdk.py +15 -2
  34. mistralai/transcriptions.py +17 -56
  35. mistralai/utils/__init__.py +18 -5
  36. mistralai/utils/eventstreaming.py +10 -0
  37. mistralai/utils/serializers.py +3 -2
  38. mistralai/utils/unmarshal_json_response.py +24 -0
  39. {mistralai-1.9.9.dist-info → mistralai-1.9.11.dist-info}/METADATA +61 -30
  40. {mistralai-1.9.9.dist-info → mistralai-1.9.11.dist-info}/RECORD +42 -36
  41. {mistralai-1.9.9.dist-info → mistralai-1.9.11.dist-info}/WHEEL +1 -1
  42. {mistralai-1.9.9.dist-info → mistralai-1.9.11.dist-info/licenses}/LICENSE +0 -0
mistralai/models_.py CHANGED
@@ -5,6 +5,7 @@ from mistralai import models, utils
5
5
  from mistralai._hooks import HookContext
6
6
  from mistralai.types import OptionalNullable, UNSET
7
7
  from mistralai.utils import get_security_from_env
8
+ from mistralai.utils.unmarshal_json_response import unmarshal_json_response
8
9
  from typing import Any, Mapping, Optional
9
10
 
10
11
 
@@ -78,31 +79,20 @@ class Models(BaseSDK):
78
79
 
79
80
  response_data: Any = None
80
81
  if utils.match_response(http_res, "200", "application/json"):
81
- return utils.unmarshal_json(http_res.text, models.ModelList)
82
+ return unmarshal_json_response(models.ModelList, http_res)
82
83
  if utils.match_response(http_res, "422", "application/json"):
83
- response_data = utils.unmarshal_json(
84
- http_res.text, models.HTTPValidationErrorData
84
+ response_data = unmarshal_json_response(
85
+ models.HTTPValidationErrorData, http_res
85
86
  )
86
- raise models.HTTPValidationError(data=response_data)
87
+ raise models.HTTPValidationError(response_data, http_res)
87
88
  if utils.match_response(http_res, "4XX", "*"):
88
89
  http_res_text = utils.stream_to_text(http_res)
89
- raise models.SDKError(
90
- "API error occurred", http_res.status_code, http_res_text, http_res
91
- )
90
+ raise models.SDKError("API error occurred", http_res, http_res_text)
92
91
  if utils.match_response(http_res, "5XX", "*"):
93
92
  http_res_text = utils.stream_to_text(http_res)
94
- raise models.SDKError(
95
- "API error occurred", http_res.status_code, http_res_text, http_res
96
- )
93
+ raise models.SDKError("API error occurred", http_res, http_res_text)
97
94
 
98
- content_type = http_res.headers.get("Content-Type")
99
- http_res_text = utils.stream_to_text(http_res)
100
- raise models.SDKError(
101
- f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
102
- http_res.status_code,
103
- http_res_text,
104
- http_res,
105
- )
95
+ raise models.SDKError("Unexpected response received", http_res)
106
96
 
107
97
  async def list_async(
108
98
  self,
@@ -171,31 +161,20 @@ class Models(BaseSDK):
171
161
 
172
162
  response_data: Any = None
173
163
  if utils.match_response(http_res, "200", "application/json"):
174
- return utils.unmarshal_json(http_res.text, models.ModelList)
164
+ return unmarshal_json_response(models.ModelList, http_res)
175
165
  if utils.match_response(http_res, "422", "application/json"):
176
- response_data = utils.unmarshal_json(
177
- http_res.text, models.HTTPValidationErrorData
166
+ response_data = unmarshal_json_response(
167
+ models.HTTPValidationErrorData, http_res
178
168
  )
179
- raise models.HTTPValidationError(data=response_data)
169
+ raise models.HTTPValidationError(response_data, http_res)
180
170
  if utils.match_response(http_res, "4XX", "*"):
181
171
  http_res_text = await utils.stream_to_text_async(http_res)
182
- raise models.SDKError(
183
- "API error occurred", http_res.status_code, http_res_text, http_res
184
- )
172
+ raise models.SDKError("API error occurred", http_res, http_res_text)
185
173
  if utils.match_response(http_res, "5XX", "*"):
186
174
  http_res_text = await utils.stream_to_text_async(http_res)
187
- raise models.SDKError(
188
- "API error occurred", http_res.status_code, http_res_text, http_res
189
- )
175
+ raise models.SDKError("API error occurred", http_res, http_res_text)
190
176
 
191
- content_type = http_res.headers.get("Content-Type")
192
- http_res_text = await utils.stream_to_text_async(http_res)
193
- raise models.SDKError(
194
- f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
195
- http_res.status_code,
196
- http_res_text,
197
- http_res,
198
- )
177
+ raise models.SDKError("Unexpected response received", http_res)
199
178
 
200
179
  def retrieve(
201
180
  self,
@@ -271,34 +250,23 @@ class Models(BaseSDK):
271
250
 
272
251
  response_data: Any = None
273
252
  if utils.match_response(http_res, "200", "application/json"):
274
- return utils.unmarshal_json(
275
- http_res.text,
253
+ return unmarshal_json_response(
276
254
  models.RetrieveModelV1ModelsModelIDGetResponseRetrieveModelV1ModelsModelIDGet,
255
+ http_res,
277
256
  )
278
257
  if utils.match_response(http_res, "422", "application/json"):
279
- response_data = utils.unmarshal_json(
280
- http_res.text, models.HTTPValidationErrorData
258
+ response_data = unmarshal_json_response(
259
+ models.HTTPValidationErrorData, http_res
281
260
  )
282
- raise models.HTTPValidationError(data=response_data)
261
+ raise models.HTTPValidationError(response_data, http_res)
283
262
  if utils.match_response(http_res, "4XX", "*"):
284
263
  http_res_text = utils.stream_to_text(http_res)
285
- raise models.SDKError(
286
- "API error occurred", http_res.status_code, http_res_text, http_res
287
- )
264
+ raise models.SDKError("API error occurred", http_res, http_res_text)
288
265
  if utils.match_response(http_res, "5XX", "*"):
289
266
  http_res_text = utils.stream_to_text(http_res)
290
- raise models.SDKError(
291
- "API error occurred", http_res.status_code, http_res_text, http_res
292
- )
267
+ raise models.SDKError("API error occurred", http_res, http_res_text)
293
268
 
294
- content_type = http_res.headers.get("Content-Type")
295
- http_res_text = utils.stream_to_text(http_res)
296
- raise models.SDKError(
297
- f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
298
- http_res.status_code,
299
- http_res_text,
300
- http_res,
301
- )
269
+ raise models.SDKError("Unexpected response received", http_res)
302
270
 
303
271
  async def retrieve_async(
304
272
  self,
@@ -374,34 +342,23 @@ class Models(BaseSDK):
374
342
 
375
343
  response_data: Any = None
376
344
  if utils.match_response(http_res, "200", "application/json"):
377
- return utils.unmarshal_json(
378
- http_res.text,
345
+ return unmarshal_json_response(
379
346
  models.RetrieveModelV1ModelsModelIDGetResponseRetrieveModelV1ModelsModelIDGet,
347
+ http_res,
380
348
  )
381
349
  if utils.match_response(http_res, "422", "application/json"):
382
- response_data = utils.unmarshal_json(
383
- http_res.text, models.HTTPValidationErrorData
350
+ response_data = unmarshal_json_response(
351
+ models.HTTPValidationErrorData, http_res
384
352
  )
385
- raise models.HTTPValidationError(data=response_data)
353
+ raise models.HTTPValidationError(response_data, http_res)
386
354
  if utils.match_response(http_res, "4XX", "*"):
387
355
  http_res_text = await utils.stream_to_text_async(http_res)
388
- raise models.SDKError(
389
- "API error occurred", http_res.status_code, http_res_text, http_res
390
- )
356
+ raise models.SDKError("API error occurred", http_res, http_res_text)
391
357
  if utils.match_response(http_res, "5XX", "*"):
392
358
  http_res_text = await utils.stream_to_text_async(http_res)
393
- raise models.SDKError(
394
- "API error occurred", http_res.status_code, http_res_text, http_res
395
- )
359
+ raise models.SDKError("API error occurred", http_res, http_res_text)
396
360
 
397
- content_type = http_res.headers.get("Content-Type")
398
- http_res_text = await utils.stream_to_text_async(http_res)
399
- raise models.SDKError(
400
- f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
401
- http_res.status_code,
402
- http_res_text,
403
- http_res,
404
- )
361
+ raise models.SDKError("Unexpected response received", http_res)
405
362
 
406
363
  def delete(
407
364
  self,
@@ -477,31 +434,20 @@ class Models(BaseSDK):
477
434
 
478
435
  response_data: Any = None
479
436
  if utils.match_response(http_res, "200", "application/json"):
480
- return utils.unmarshal_json(http_res.text, models.DeleteModelOut)
437
+ return unmarshal_json_response(models.DeleteModelOut, http_res)
481
438
  if utils.match_response(http_res, "422", "application/json"):
482
- response_data = utils.unmarshal_json(
483
- http_res.text, models.HTTPValidationErrorData
439
+ response_data = unmarshal_json_response(
440
+ models.HTTPValidationErrorData, http_res
484
441
  )
485
- raise models.HTTPValidationError(data=response_data)
442
+ raise models.HTTPValidationError(response_data, http_res)
486
443
  if utils.match_response(http_res, "4XX", "*"):
487
444
  http_res_text = utils.stream_to_text(http_res)
488
- raise models.SDKError(
489
- "API error occurred", http_res.status_code, http_res_text, http_res
490
- )
445
+ raise models.SDKError("API error occurred", http_res, http_res_text)
491
446
  if utils.match_response(http_res, "5XX", "*"):
492
447
  http_res_text = utils.stream_to_text(http_res)
493
- raise models.SDKError(
494
- "API error occurred", http_res.status_code, http_res_text, http_res
495
- )
448
+ raise models.SDKError("API error occurred", http_res, http_res_text)
496
449
 
497
- content_type = http_res.headers.get("Content-Type")
498
- http_res_text = utils.stream_to_text(http_res)
499
- raise models.SDKError(
500
- f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
501
- http_res.status_code,
502
- http_res_text,
503
- http_res,
504
- )
450
+ raise models.SDKError("Unexpected response received", http_res)
505
451
 
506
452
  async def delete_async(
507
453
  self,
@@ -577,31 +523,20 @@ class Models(BaseSDK):
577
523
 
578
524
  response_data: Any = None
579
525
  if utils.match_response(http_res, "200", "application/json"):
580
- return utils.unmarshal_json(http_res.text, models.DeleteModelOut)
526
+ return unmarshal_json_response(models.DeleteModelOut, http_res)
581
527
  if utils.match_response(http_res, "422", "application/json"):
582
- response_data = utils.unmarshal_json(
583
- http_res.text, models.HTTPValidationErrorData
528
+ response_data = unmarshal_json_response(
529
+ models.HTTPValidationErrorData, http_res
584
530
  )
585
- raise models.HTTPValidationError(data=response_data)
531
+ raise models.HTTPValidationError(response_data, http_res)
586
532
  if utils.match_response(http_res, "4XX", "*"):
587
533
  http_res_text = await utils.stream_to_text_async(http_res)
588
- raise models.SDKError(
589
- "API error occurred", http_res.status_code, http_res_text, http_res
590
- )
534
+ raise models.SDKError("API error occurred", http_res, http_res_text)
591
535
  if utils.match_response(http_res, "5XX", "*"):
592
536
  http_res_text = await utils.stream_to_text_async(http_res)
593
- raise models.SDKError(
594
- "API error occurred", http_res.status_code, http_res_text, http_res
595
- )
537
+ raise models.SDKError("API error occurred", http_res, http_res_text)
596
538
 
597
- content_type = http_res.headers.get("Content-Type")
598
- http_res_text = await utils.stream_to_text_async(http_res)
599
- raise models.SDKError(
600
- f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
601
- http_res.status_code,
602
- http_res_text,
603
- http_res,
604
- )
539
+ raise models.SDKError("Unexpected response received", http_res)
605
540
 
606
541
  def update(
607
542
  self,
@@ -687,29 +622,17 @@ class Models(BaseSDK):
687
622
  )
688
623
 
689
624
  if utils.match_response(http_res, "200", "application/json"):
690
- return utils.unmarshal_json(
691
- http_res.text,
692
- models.JobsAPIRoutesFineTuningUpdateFineTunedModelResponse,
625
+ return unmarshal_json_response(
626
+ models.JobsAPIRoutesFineTuningUpdateFineTunedModelResponse, http_res
693
627
  )
694
628
  if utils.match_response(http_res, "4XX", "*"):
695
629
  http_res_text = utils.stream_to_text(http_res)
696
- raise models.SDKError(
697
- "API error occurred", http_res.status_code, http_res_text, http_res
698
- )
630
+ raise models.SDKError("API error occurred", http_res, http_res_text)
699
631
  if utils.match_response(http_res, "5XX", "*"):
700
632
  http_res_text = utils.stream_to_text(http_res)
701
- raise models.SDKError(
702
- "API error occurred", http_res.status_code, http_res_text, http_res
703
- )
633
+ raise models.SDKError("API error occurred", http_res, http_res_text)
704
634
 
705
- content_type = http_res.headers.get("Content-Type")
706
- http_res_text = utils.stream_to_text(http_res)
707
- raise models.SDKError(
708
- f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
709
- http_res.status_code,
710
- http_res_text,
711
- http_res,
712
- )
635
+ raise models.SDKError("Unexpected response received", http_res)
713
636
 
714
637
  async def update_async(
715
638
  self,
@@ -795,29 +718,17 @@ class Models(BaseSDK):
795
718
  )
796
719
 
797
720
  if utils.match_response(http_res, "200", "application/json"):
798
- return utils.unmarshal_json(
799
- http_res.text,
800
- models.JobsAPIRoutesFineTuningUpdateFineTunedModelResponse,
721
+ return unmarshal_json_response(
722
+ models.JobsAPIRoutesFineTuningUpdateFineTunedModelResponse, http_res
801
723
  )
802
724
  if utils.match_response(http_res, "4XX", "*"):
803
725
  http_res_text = await utils.stream_to_text_async(http_res)
804
- raise models.SDKError(
805
- "API error occurred", http_res.status_code, http_res_text, http_res
806
- )
726
+ raise models.SDKError("API error occurred", http_res, http_res_text)
807
727
  if utils.match_response(http_res, "5XX", "*"):
808
728
  http_res_text = await utils.stream_to_text_async(http_res)
809
- raise models.SDKError(
810
- "API error occurred", http_res.status_code, http_res_text, http_res
811
- )
729
+ raise models.SDKError("API error occurred", http_res, http_res_text)
812
730
 
813
- content_type = http_res.headers.get("Content-Type")
814
- http_res_text = await utils.stream_to_text_async(http_res)
815
- raise models.SDKError(
816
- f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
817
- http_res.status_code,
818
- http_res_text,
819
- http_res,
820
- )
731
+ raise models.SDKError("Unexpected response received", http_res)
821
732
 
822
733
  def archive(
823
734
  self,
@@ -892,26 +803,15 @@ class Models(BaseSDK):
892
803
  )
893
804
 
894
805
  if utils.match_response(http_res, "200", "application/json"):
895
- return utils.unmarshal_json(http_res.text, models.ArchiveFTModelOut)
806
+ return unmarshal_json_response(models.ArchiveFTModelOut, http_res)
896
807
  if utils.match_response(http_res, "4XX", "*"):
897
808
  http_res_text = utils.stream_to_text(http_res)
898
- raise models.SDKError(
899
- "API error occurred", http_res.status_code, http_res_text, http_res
900
- )
809
+ raise models.SDKError("API error occurred", http_res, http_res_text)
901
810
  if utils.match_response(http_res, "5XX", "*"):
902
811
  http_res_text = utils.stream_to_text(http_res)
903
- raise models.SDKError(
904
- "API error occurred", http_res.status_code, http_res_text, http_res
905
- )
812
+ raise models.SDKError("API error occurred", http_res, http_res_text)
906
813
 
907
- content_type = http_res.headers.get("Content-Type")
908
- http_res_text = utils.stream_to_text(http_res)
909
- raise models.SDKError(
910
- f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
911
- http_res.status_code,
912
- http_res_text,
913
- http_res,
914
- )
814
+ raise models.SDKError("Unexpected response received", http_res)
915
815
 
916
816
  async def archive_async(
917
817
  self,
@@ -986,26 +886,15 @@ class Models(BaseSDK):
986
886
  )
987
887
 
988
888
  if utils.match_response(http_res, "200", "application/json"):
989
- return utils.unmarshal_json(http_res.text, models.ArchiveFTModelOut)
889
+ return unmarshal_json_response(models.ArchiveFTModelOut, http_res)
990
890
  if utils.match_response(http_res, "4XX", "*"):
991
891
  http_res_text = await utils.stream_to_text_async(http_res)
992
- raise models.SDKError(
993
- "API error occurred", http_res.status_code, http_res_text, http_res
994
- )
892
+ raise models.SDKError("API error occurred", http_res, http_res_text)
995
893
  if utils.match_response(http_res, "5XX", "*"):
996
894
  http_res_text = await utils.stream_to_text_async(http_res)
997
- raise models.SDKError(
998
- "API error occurred", http_res.status_code, http_res_text, http_res
999
- )
895
+ raise models.SDKError("API error occurred", http_res, http_res_text)
1000
896
 
1001
- content_type = http_res.headers.get("Content-Type")
1002
- http_res_text = await utils.stream_to_text_async(http_res)
1003
- raise models.SDKError(
1004
- f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
1005
- http_res.status_code,
1006
- http_res_text,
1007
- http_res,
1008
- )
897
+ raise models.SDKError("Unexpected response received", http_res)
1009
898
 
1010
899
  def unarchive(
1011
900
  self,
@@ -1080,26 +969,15 @@ class Models(BaseSDK):
1080
969
  )
1081
970
 
1082
971
  if utils.match_response(http_res, "200", "application/json"):
1083
- return utils.unmarshal_json(http_res.text, models.UnarchiveFTModelOut)
972
+ return unmarshal_json_response(models.UnarchiveFTModelOut, http_res)
1084
973
  if utils.match_response(http_res, "4XX", "*"):
1085
974
  http_res_text = utils.stream_to_text(http_res)
1086
- raise models.SDKError(
1087
- "API error occurred", http_res.status_code, http_res_text, http_res
1088
- )
975
+ raise models.SDKError("API error occurred", http_res, http_res_text)
1089
976
  if utils.match_response(http_res, "5XX", "*"):
1090
977
  http_res_text = utils.stream_to_text(http_res)
1091
- raise models.SDKError(
1092
- "API error occurred", http_res.status_code, http_res_text, http_res
1093
- )
978
+ raise models.SDKError("API error occurred", http_res, http_res_text)
1094
979
 
1095
- content_type = http_res.headers.get("Content-Type")
1096
- http_res_text = utils.stream_to_text(http_res)
1097
- raise models.SDKError(
1098
- f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
1099
- http_res.status_code,
1100
- http_res_text,
1101
- http_res,
1102
- )
980
+ raise models.SDKError("Unexpected response received", http_res)
1103
981
 
1104
982
  async def unarchive_async(
1105
983
  self,
@@ -1174,23 +1052,12 @@ class Models(BaseSDK):
1174
1052
  )
1175
1053
 
1176
1054
  if utils.match_response(http_res, "200", "application/json"):
1177
- return utils.unmarshal_json(http_res.text, models.UnarchiveFTModelOut)
1055
+ return unmarshal_json_response(models.UnarchiveFTModelOut, http_res)
1178
1056
  if utils.match_response(http_res, "4XX", "*"):
1179
1057
  http_res_text = await utils.stream_to_text_async(http_res)
1180
- raise models.SDKError(
1181
- "API error occurred", http_res.status_code, http_res_text, http_res
1182
- )
1058
+ raise models.SDKError("API error occurred", http_res, http_res_text)
1183
1059
  if utils.match_response(http_res, "5XX", "*"):
1184
1060
  http_res_text = await utils.stream_to_text_async(http_res)
1185
- raise models.SDKError(
1186
- "API error occurred", http_res.status_code, http_res_text, http_res
1187
- )
1061
+ raise models.SDKError("API error occurred", http_res, http_res_text)
1188
1062
 
1189
- content_type = http_res.headers.get("Content-Type")
1190
- http_res_text = await utils.stream_to_text_async(http_res)
1191
- raise models.SDKError(
1192
- f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
1193
- http_res.status_code,
1194
- http_res_text,
1195
- http_res,
1196
- )
1063
+ raise models.SDKError("Unexpected response received", http_res)
mistralai/ocr.py CHANGED
@@ -5,6 +5,7 @@ from mistralai import models, utils
5
5
  from mistralai._hooks import HookContext
6
6
  from mistralai.types import Nullable, OptionalNullable, UNSET
7
7
  from mistralai.utils import get_security_from_env
8
+ from mistralai.utils.unmarshal_json_response import unmarshal_json_response
8
9
  from typing import Any, List, Mapping, Optional, Union
9
10
 
10
11
 
@@ -118,31 +119,20 @@ class Ocr(BaseSDK):
118
119
 
119
120
  response_data: Any = None
120
121
  if utils.match_response(http_res, "200", "application/json"):
121
- return utils.unmarshal_json(http_res.text, models.OCRResponse)
122
+ return unmarshal_json_response(models.OCRResponse, http_res)
122
123
  if utils.match_response(http_res, "422", "application/json"):
123
- response_data = utils.unmarshal_json(
124
- http_res.text, models.HTTPValidationErrorData
124
+ response_data = unmarshal_json_response(
125
+ models.HTTPValidationErrorData, http_res
125
126
  )
126
- raise models.HTTPValidationError(data=response_data)
127
+ raise models.HTTPValidationError(response_data, http_res)
127
128
  if utils.match_response(http_res, "4XX", "*"):
128
129
  http_res_text = utils.stream_to_text(http_res)
129
- raise models.SDKError(
130
- "API error occurred", http_res.status_code, http_res_text, http_res
131
- )
130
+ raise models.SDKError("API error occurred", http_res, http_res_text)
132
131
  if utils.match_response(http_res, "5XX", "*"):
133
132
  http_res_text = utils.stream_to_text(http_res)
134
- raise models.SDKError(
135
- "API error occurred", http_res.status_code, http_res_text, http_res
136
- )
133
+ raise models.SDKError("API error occurred", http_res, http_res_text)
137
134
 
138
- content_type = http_res.headers.get("Content-Type")
139
- http_res_text = utils.stream_to_text(http_res)
140
- raise models.SDKError(
141
- f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
142
- http_res.status_code,
143
- http_res_text,
144
- http_res,
145
- )
135
+ raise models.SDKError("Unexpected response received", http_res)
146
136
 
147
137
  async def process_async(
148
138
  self,
@@ -251,28 +241,17 @@ class Ocr(BaseSDK):
251
241
 
252
242
  response_data: Any = None
253
243
  if utils.match_response(http_res, "200", "application/json"):
254
- return utils.unmarshal_json(http_res.text, models.OCRResponse)
244
+ return unmarshal_json_response(models.OCRResponse, http_res)
255
245
  if utils.match_response(http_res, "422", "application/json"):
256
- response_data = utils.unmarshal_json(
257
- http_res.text, models.HTTPValidationErrorData
246
+ response_data = unmarshal_json_response(
247
+ models.HTTPValidationErrorData, http_res
258
248
  )
259
- raise models.HTTPValidationError(data=response_data)
249
+ raise models.HTTPValidationError(response_data, http_res)
260
250
  if utils.match_response(http_res, "4XX", "*"):
261
251
  http_res_text = await utils.stream_to_text_async(http_res)
262
- raise models.SDKError(
263
- "API error occurred", http_res.status_code, http_res_text, http_res
264
- )
252
+ raise models.SDKError("API error occurred", http_res, http_res_text)
265
253
  if utils.match_response(http_res, "5XX", "*"):
266
254
  http_res_text = await utils.stream_to_text_async(http_res)
267
- raise models.SDKError(
268
- "API error occurred", http_res.status_code, http_res_text, http_res
269
- )
255
+ raise models.SDKError("API error occurred", http_res, http_res_text)
270
256
 
271
- content_type = http_res.headers.get("Content-Type")
272
- http_res_text = await utils.stream_to_text_async(http_res)
273
- raise models.SDKError(
274
- f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
275
- http_res.status_code,
276
- http_res_text,
277
- http_res,
278
- )
257
+ raise models.SDKError("Unexpected response received", http_res)
mistralai/sdk.py CHANGED
@@ -10,6 +10,7 @@ import importlib
10
10
  from mistralai import models, utils
11
11
  from mistralai._hooks import SDKHooks
12
12
  from mistralai.types import OptionalNullable, UNSET
13
+ import sys
13
14
  from typing import Any, Callable, Dict, Optional, TYPE_CHECKING, Union, cast
14
15
  import weakref
15
16
 
@@ -135,6 +136,7 @@ class Mistral(BaseSDK):
135
136
  timeout_ms=timeout_ms,
136
137
  debug_logger=debug_logger,
137
138
  ),
139
+ parent_ref=self,
138
140
  )
139
141
 
140
142
  hooks = SDKHooks()
@@ -159,13 +161,24 @@ class Mistral(BaseSDK):
159
161
  self.sdk_configuration.async_client_supplied,
160
162
  )
161
163
 
164
+ def dynamic_import(self, modname, retries=3):
165
+ for attempt in range(retries):
166
+ try:
167
+ return importlib.import_module(modname)
168
+ except KeyError:
169
+ # Clear any half-initialized module and retry
170
+ sys.modules.pop(modname, None)
171
+ if attempt == retries - 1:
172
+ break
173
+ raise KeyError(f"Failed to import module '{modname}' after {retries} attempts")
174
+
162
175
  def __getattr__(self, name: str):
163
176
  if name in self._sub_sdk_map:
164
177
  module_path, class_name = self._sub_sdk_map[name]
165
178
  try:
166
- module = importlib.import_module(module_path)
179
+ module = self.dynamic_import(module_path)
167
180
  klass = getattr(module, class_name)
168
- instance = klass(self.sdk_configuration)
181
+ instance = klass(self.sdk_configuration, parent_ref=self)
169
182
  setattr(self, name, instance)
170
183
  return instance
171
184
  except ImportError as e: