From a37bac03b00ec62ecddcbf6d803c63c568399421 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 25 Sep 2025 12:39:53 +0000 Subject: [PATCH] Update Speakeasy SDKs to version 1.628.4 --- .gitignore | 1 + .speakeasy/gen.lock | 21 +- .speakeasy/gen.yaml | 8 + .speakeasy/workflow.lock | 14 +- .speakeasy/workflow.yaml | 2 +- README.md | 89 +++- USAGE.md | 12 +- docs/sdks/accesses/README.md | 3 + docs/sdks/agents/README.md | 2 + docs/sdks/chat/README.md | 2 + docs/sdks/classifiers/README.md | 4 + docs/sdks/conversations/README.md | 10 + docs/sdks/documents/README.md | 10 + docs/sdks/embeddings/README.md | 1 + docs/sdks/files/README.md | 6 + docs/sdks/fim/README.md | 2 + docs/sdks/jobs/README.md | 5 + docs/sdks/libraries/README.md | 5 + docs/sdks/mistralagents/README.md | 5 + docs/sdks/mistraljobs/README.md | 4 + docs/sdks/models/README.md | 6 + docs/sdks/ocr/README.md | 1 + docs/sdks/transcriptions/README.md | 2 + poetry.toml | 1 + scripts/prepare_readme.py | 2 + scripts/publish.sh | 1 - src/mistralai/_version.py | 4 +- src/mistralai/accesses.py | 151 ++---- src/mistralai/agents.py | 97 +--- src/mistralai/audio.py | 11 +- src/mistralai/basesdk.py | 20 +- src/mistralai/batch.py | 9 +- src/mistralai/beta.py | 15 +- src/mistralai/chat.py | 97 +--- src/mistralai/classifiers.py | 201 ++----- src/mistralai/conversations.py | 495 +++++------------- src/mistralai/documents.py | 493 +++++------------ src/mistralai/embeddings.py | 51 +- src/mistralai/files.py | 223 ++------ src/mistralai/fim.py | 97 +--- src/mistralai/fine_tuning.py | 9 +- src/mistralai/jobs.py | 207 ++------ src/mistralai/libraries.py | 249 +++------ src/mistralai/mistral_agents.py | 251 +++------ src/mistralai/mistral_jobs.py | 169 ++---- src/mistralai/models/__init__.py | 28 +- src/mistralai/models/agent.py | 2 +- src/mistralai/models/agentconversation.py | 2 +- src/mistralai/models/agenthandoffdoneevent.py | 2 +- src/mistralai/models/agenthandoffentry.py | 5 +- .../models/agenthandoffstartedevent.py | 2 +- src/mistralai/models/archiveftmodelout.py | 2 +- src/mistralai/models/assistantmessage.py | 2 +- src/mistralai/models/audiochunk.py | 2 +- src/mistralai/models/basemodelcard.py | 2 +- src/mistralai/models/batchjobout.py | 2 +- src/mistralai/models/batchjobsout.py | 2 +- src/mistralai/models/chatcompletionchoice.py | 9 +- .../models/classifierdetailedjobout.py | 6 +- src/mistralai/models/classifierftmodelout.py | 5 +- src/mistralai/models/classifierjobout.py | 6 +- src/mistralai/models/codeinterpretertool.py | 2 +- .../models/completiondetailedjobout.py | 7 +- src/mistralai/models/completionftmodelout.py | 5 +- src/mistralai/models/completionjobout.py | 7 +- .../models/completionresponsestreamchoice.py | 8 +- .../models/conversationappendrequest.py | 5 +- .../models/conversationappendstreamrequest.py | 5 +- src/mistralai/models/conversationhistory.py | 3 +- src/mistralai/models/conversationmessages.py | 2 +- src/mistralai/models/conversationrequest.py | 6 +- src/mistralai/models/conversationresponse.py | 3 +- .../models/conversationrestartrequest.py | 5 +- .../conversationrestartstreamrequest.py | 5 +- .../models/conversationstreamrequest.py | 6 +- src/mistralai/models/documentlibrarytool.py | 2 +- src/mistralai/models/documenturlchunk.py | 2 +- src/mistralai/models/embeddingdtype.py | 8 +- src/mistralai/models/encodingformat.py | 5 +- src/mistralai/models/entitytype.py | 9 +- src/mistralai/models/filepurpose.py | 9 +- src/mistralai/models/finetuneablemodeltype.py | 5 +- .../models/ftclassifierlossfunction.py | 5 +- src/mistralai/models/ftmodelcard.py | 2 +- src/mistralai/models/functioncallentry.py | 5 +- src/mistralai/models/functioncallevent.py | 2 +- src/mistralai/models/functionresultentry.py | 5 +- src/mistralai/models/functiontool.py | 2 +- src/mistralai/models/githubrepositoryin.py | 2 +- src/mistralai/models/githubrepositoryout.py | 2 +- src/mistralai/models/httpvalidationerror.py | 23 +- src/mistralai/models/imagegenerationtool.py | 2 +- src/mistralai/models/imageurlchunk.py | 2 +- src/mistralai/models/jobsout.py | 2 +- src/mistralai/models/legacyjobmetadataout.py | 2 +- src/mistralai/models/messageinputentry.py | 12 +- src/mistralai/models/messageoutputentry.py | 9 +- src/mistralai/models/messageoutputevent.py | 6 +- src/mistralai/models/mistralerror.py | 30 ++ src/mistralai/models/mistralpromptmode.py | 2 +- src/mistralai/models/modelconversation.py | 2 +- src/mistralai/models/no_response_error.py | 17 + src/mistralai/models/referencechunk.py | 2 +- src/mistralai/models/responsedoneevent.py | 2 +- src/mistralai/models/responseerrorevent.py | 2 +- src/mistralai/models/responseformats.py | 6 +- src/mistralai/models/responsestartedevent.py | 2 +- .../models/responsevalidationerror.py | 27 + src/mistralai/models/sampletype.py | 8 +- src/mistralai/models/sdkerror.py | 46 +- src/mistralai/models/shareenum.py | 8 +- src/mistralai/models/source.py | 9 +- src/mistralai/models/systemmessage.py | 2 +- src/mistralai/models/textchunk.py | 2 +- src/mistralai/models/thinkchunk.py | 2 +- src/mistralai/models/timestampgranularity.py | 2 +- src/mistralai/models/toolchoiceenum.py | 7 +- .../models/toolexecutiondeltaevent.py | 2 +- .../models/toolexecutiondoneevent.py | 2 +- src/mistralai/models/toolexecutionentry.py | 5 +- .../models/toolexecutionstartedevent.py | 2 +- src/mistralai/models/toolfilechunk.py | 2 +- src/mistralai/models/toolmessage.py | 2 +- src/mistralai/models/toolreferencechunk.py | 2 +- src/mistralai/models/tooltypes.py | 2 +- .../models/transcriptionsegmentchunk.py | 2 +- .../models/transcriptionstreamdone.py | 2 +- .../models/transcriptionstreamlanguage.py | 2 +- .../models/transcriptionstreamsegmentdelta.py | 2 +- .../models/transcriptionstreamtextdelta.py | 2 +- src/mistralai/models/unarchiveftmodelout.py | 2 +- src/mistralai/models/usermessage.py | 2 +- src/mistralai/models/wandbintegration.py | 2 +- src/mistralai/models/wandbintegrationout.py | 2 +- src/mistralai/models/websearchpremiumtool.py | 2 +- src/mistralai/models/websearchtool.py | 2 +- src/mistralai/models_.py | 275 +++------- src/mistralai/ocr.py | 51 +- src/mistralai/sdk.py | 21 +- src/mistralai/transcriptions.py | 73 +-- src/mistralai/utils/__init__.py | 23 +- src/mistralai/utils/annotations.py | 40 +- src/mistralai/utils/eventstreaming.py | 10 + src/mistralai/utils/serializers.py | 5 +- .../utils/unmarshal_json_response.py | 24 + 145 files changed, 1549 insertions(+), 2531 deletions(-) create mode 100644 src/mistralai/models/mistralerror.py create mode 100644 src/mistralai/models/no_response_error.py create mode 100644 src/mistralai/models/responsevalidationerror.py create mode 100644 src/mistralai/utils/unmarshal_json_response.py diff --git a/.gitignore b/.gitignore index 954adb7c..4adc68c6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.env.local **/__pycache__/ **/.speakeasy/temp/ **/.speakeasy/logs/ diff --git a/.speakeasy/gen.lock b/.speakeasy/gen.lock index d980c62f..79686864 100644 --- a/.speakeasy/gen.lock +++ b/.speakeasy/gen.lock @@ -3,10 +3,10 @@ id: 2d045ec7-2ebb-4f4d-ad25-40953b132161 management: docChecksum: c3693e4872a0785b2ed46c59a8464804 docVersion: 1.0.0 - speakeasyVersion: 1.568.2 - generationVersion: 2.634.2 + speakeasyVersion: 1.628.4 + generationVersion: 2.716.5 releaseVersion: 1.9.10 - configChecksum: 438b1373b4090838b050a2779ab5383f + configChecksum: 7f23b5385b52f9096be1cb6451f4188e repoURL: https://github.com/mistralai/client-python.git installationURL: https://github.com/mistralai/client-python.git published: true @@ -15,16 +15,16 @@ features: additionalDependencies: 1.0.0 additionalProperties: 1.0.1 constsAndDefaults: 1.0.5 - core: 5.19.3 + core: 5.22.0 customCodeRegions: 0.1.1 defaultEnabledRetries: 0.2.0 downloadStreams: 1.0.1 enumUnions: 0.1.0 envVarSecurityUsage: 0.3.2 - examples: 3.0.1 + examples: 3.0.2 flatRequests: 1.0.1 flattening: 3.1.1 - globalSecurity: 3.0.3 + globalSecurity: 3.0.4 globalSecurityCallbacks: 1.0.0 globalSecurityFlattening: 1.0.0 globalServerURLs: 3.1.1 @@ -36,14 +36,13 @@ features: responseFormat: 1.0.1 retries: 3.0.2 sdkHooks: 1.1.0 - serverEvents: 1.0.7 + serverEvents: 1.0.10 serverEventsSentinels: 0.1.0 serverIDs: 3.0.0 - unions: 3.0.4 + unions: 3.0.5 uploadStreams: 1.0.0 generatedFiles: - .gitattributes - - .python-version - .vscode/settings.json - USAGE.md - docs/models/agent.md @@ -665,12 +664,14 @@ generatedFiles: - src/mistralai/models/messageoutputentry.py - src/mistralai/models/messageoutputevent.py - src/mistralai/models/metricout.py + - src/mistralai/models/mistralerror.py - src/mistralai/models/mistralpromptmode.py - src/mistralai/models/modelcapabilities.py - src/mistralai/models/modelconversation.py - src/mistralai/models/modellist.py - src/mistralai/models/moderationobject.py - src/mistralai/models/moderationresponse.py + - src/mistralai/models/no_response_error.py - src/mistralai/models/ocrimageobject.py - src/mistralai/models/ocrpagedimensions.py - src/mistralai/models/ocrpageobject.py @@ -687,6 +688,7 @@ generatedFiles: - src/mistralai/models/responseformat.py - src/mistralai/models/responseformats.py - src/mistralai/models/responsestartedevent.py + - src/mistralai/models/responsevalidationerror.py - src/mistralai/models/retrieve_model_v1_models_model_id_getop.py - src/mistralai/models/retrievefileout.py - src/mistralai/models/sampletype.py @@ -756,6 +758,7 @@ generatedFiles: - src/mistralai/utils/retries.py - src/mistralai/utils/security.py - src/mistralai/utils/serializers.py + - src/mistralai/utils/unmarshal_json_response.py - src/mistralai/utils/url.py - src/mistralai/utils/values.py examples: diff --git a/.speakeasy/gen.yaml b/.speakeasy/gen.yaml index 1fb8877e..208f1e6b 100644 --- a/.speakeasy/gen.yaml +++ b/.speakeasy/gen.yaml @@ -16,6 +16,7 @@ generation: auth: oAuth2ClientCredentialsEnabled: true oAuth2PasswordEnabled: false + hoistGlobalSecurity: true tests: generateTests: true generateNewTests: false @@ -26,6 +27,10 @@ python: dev: pytest: ^8.2.2 pytest-asyncio: ^0.23.7 + allowedRedefinedBuiltins: + - id + - object + asyncMode: both authors: - Mistral baseErrorName: MistralError @@ -49,12 +54,15 @@ python: shared: "" webhooks: "" inputModelSuffix: input + legacyPyright: true maxMethodParams: 15 methodArguments: infer-optional-args moduleName: "" outputModelSuffix: output + packageManager: poetry packageName: mistralai pytestFilterWarnings: [] pytestTimeout: 0 responseFormat: flat + sseFlatResponse: false templateVersion: v2 diff --git a/.speakeasy/workflow.lock b/.speakeasy/workflow.lock index 03418bf8..f4abb990 100644 --- a/.speakeasy/workflow.lock +++ b/.speakeasy/workflow.lock @@ -1,4 +1,4 @@ -speakeasyVersion: 1.568.2 +speakeasyVersion: 1.628.4 sources: mistral-azure-source: sourceNamespace: mistral-openapi-azure @@ -18,7 +18,7 @@ sources: sourceBlobDigest: sha256:97767522559603de92a9738938e522cea4d558b2a854500acf6fe8d81f8ccfb8 tags: - latest - - speakeasy-sdk-regen-1756796562 + - main targets: mistralai-azure-sdk: source: mistral-azure-source @@ -40,20 +40,20 @@ targets: sourceRevisionDigest: sha256:eefc1f0b6a5e9ec673d317d61cad766290710b5fc369412491b75f732cccfedd sourceBlobDigest: sha256:97767522559603de92a9738938e522cea4d558b2a854500acf6fe8d81f8ccfb8 codeSamplesNamespace: mistral-openapi-code-samples - codeSamplesRevisionDigest: sha256:372d48b60fafc0c389b12af1dad61faa479598e8194dc3a2a1ed27c207ab9b18 + codeSamplesRevisionDigest: sha256:31dee4783f73e1efa932b16da7238ddfe29b94f4b1c66223d6e0f2393cdc6f72 workflow: workflowVersion: 1.0.0 - speakeasyVersion: 1.568.2 + speakeasyVersion: 1.628.4 sources: mistral-azure-source: inputs: - - location: registry.speakeasyapi.dev/mistral-dev/mistral-dev/mistral-openapi-azure:main + - location: registry.speakeasyapi.dev/mistral-dev/mistral-dev/mistral-openapi-azure:sha256:670c460702ec74f7077491464a6dc5ee9d873969c80e812c48dbf4deb160e470 mistral-google-cloud-source: inputs: - - location: registry.speakeasyapi.dev/mistral-dev/mistral-dev/mistral-openapi-google-cloud:main + - location: registry.speakeasyapi.dev/mistral-dev/mistral-dev/mistral-openapi-google-cloud:sha256:4a5343e63c6a78152e472b00ccc46d7bcb15594496bc94c8040039d3a9d4c5f8 mistral-openapi: inputs: - - location: registry.speakeasyapi.dev/mistral-dev/mistral-dev/mistral-openapi:main + - location: registry.speakeasyapi.dev/mistral-dev/mistral-dev/mistral-openapi:sha256:eefc1f0b6a5e9ec673d317d61cad766290710b5fc369412491b75f732cccfedd targets: mistralai-azure-sdk: target: python diff --git a/.speakeasy/workflow.yaml b/.speakeasy/workflow.yaml index fe32bb3f..856fe8b2 100644 --- a/.speakeasy/workflow.yaml +++ b/.speakeasy/workflow.yaml @@ -1,5 +1,5 @@ workflowVersion: 1.0.0 -speakeasyVersion: 1.568.2 +speakeasyVersion: 1.628.4 sources: mistral-azure-source: inputs: diff --git a/README.md b/README.md index f71ccfcb..b0edb204 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,15 @@ Mistral AI API: Our Chat Completion and Embeddings APIs specification. Create yo > > Once a Python version reaches its [official end of life date](https://devguide.python.org/versions/), a 3-month grace period is provided for users to upgrade. Following this grace period, the minimum python version supported in the SDK will be updated. -The SDK can be installed with either *pip* or *poetry* package managers. +The SDK can be installed with *uv*, *pip*, or *poetry* package managers. + +### uv + +*uv* is a fast Python package installer and resolver, designed as a drop-in replacement for pip and pip-tools. It's recommended for its speed and modern Python tooling capabilities. + +```bash +uv add mistralai +``` ### PIP @@ -150,7 +158,8 @@ with Mistral(
-The same SDK client can also be used to make asychronous requests by importing asyncio. +The same SDK client can also be used to make asynchronous requests by importing asyncio. + ```python # Asynchronous Example import asyncio @@ -201,7 +210,8 @@ with Mistral(
-The same SDK client can also be used to make asychronous requests by importing asyncio. +The same SDK client can also be used to make asynchronous requests by importing asyncio. + ```python # Asynchronous Example import asyncio @@ -252,7 +262,8 @@ with Mistral(
-The same SDK client can also be used to make asychronous requests by importing asyncio. +The same SDK client can also be used to make asynchronous requests by importing asyncio. + ```python # Asynchronous Example import asyncio @@ -303,7 +314,8 @@ with Mistral(
-The same SDK client can also be used to make asychronous requests by importing asyncio. +The same SDK client can also be used to make asynchronous requests by importing asyncio. + ```python # Asynchronous Example import asyncio @@ -680,27 +692,20 @@ with Mistral( ## Error Handling -Handling errors in this SDK should largely match your expectations. All operations return a response object or raise an exception. - -By default, an API error will raise a models.SDKError exception, which has the following properties: - -| Property | Type | Description | -|-----------------|------------------|-----------------------| -| `.status_code` | *int* | The HTTP status code | -| `.message` | *str* | The error message | -| `.raw_response` | *httpx.Response* | The raw HTTP response | -| `.body` | *str* | The response content | - -When custom error responses are specified for an operation, the SDK may also raise their associated exceptions. You can refer to respective *Errors* tables in SDK docs for more details on possible exception types for each operation. For example, the `list_async` method may raise the following exceptions: +[`MistralError`](./src/mistralai/models/mistralerror.py) is the base class for all HTTP error responses. It has the following properties: -| Error Type | Status Code | Content Type | -| -------------------------- | ----------- | ---------------- | -| models.HTTPValidationError | 422 | application/json | -| models.SDKError | 4XX, 5XX | \*/\* | +| Property | Type | Description | +| ------------------ | ---------------- | --------------------------------------------------------------------------------------- | +| `err.message` | `str` | Error message | +| `err.status_code` | `int` | HTTP response status code eg `404` | +| `err.headers` | `httpx.Headers` | HTTP response headers | +| `err.body` | `str` | HTTP body. Can be empty string if no body is returned. | +| `err.raw_response` | `httpx.Response` | Raw HTTP response | +| `err.data` | | Optional. Some errors may contain structured data. [See Error Classes](#error-classes). | ### Example - ```python +import mistralai from mistralai import Mistral, models import os @@ -716,13 +721,41 @@ with Mistral( # Handle response print(res) - except models.HTTPValidationError as e: - # handle e.data: models.HTTPValidationErrorData - raise(e) - except models.SDKError as e: - # handle exception - raise(e) + + except models.MistralError as e: + # The base class for HTTP error responses + print(e.message) + print(e.status_code) + print(e.body) + print(e.headers) + print(e.raw_response) + + # Depending on the method different errors may be thrown + if isinstance(e, models.HTTPValidationError): + print(e.data.detail) # Optional[List[mistralai.ValidationError]] ``` + +### Error Classes +**Primary error:** +* [`MistralError`](./src/mistralai/models/mistralerror.py): The base class for HTTP error responses. + +
Less common errors (6) + +
+ +**Network errors:** +* [`httpx.RequestError`](https://www.python-httpx.org/exceptions/#httpx.RequestError): Base class for request errors. + * [`httpx.ConnectError`](https://www.python-httpx.org/exceptions/#httpx.ConnectError): HTTP client was unable to make a request to a server. + * [`httpx.TimeoutException`](https://www.python-httpx.org/exceptions/#httpx.TimeoutException): HTTP request timed out. + + +**Inherit from [`MistralError`](./src/mistralai/models/mistralerror.py)**: +* [`HTTPValidationError`](./src/mistralai/models/httpvalidationerror.py): Validation Error. Status code `422`. Applicable to 47 of 68 methods.* +* [`ResponseValidationError`](./src/mistralai/models/responsevalidationerror.py): Type mismatch between the response data and the expected Pydantic model. Provides access to the Pydantic validation error via the `cause` attribute. + +
+ +\* Check [the method documentation](#available-resources-and-operations) to see if the error is applicable. diff --git a/USAGE.md b/USAGE.md index d2bba404..3bad7f8c 100644 --- a/USAGE.md +++ b/USAGE.md @@ -26,7 +26,8 @@ with Mistral(
-The same SDK client can also be used to make asychronous requests by importing asyncio. +The same SDK client can also be used to make asynchronous requests by importing asyncio. + ```python # Asynchronous Example import asyncio @@ -77,7 +78,8 @@ with Mistral(
-The same SDK client can also be used to make asychronous requests by importing asyncio. +The same SDK client can also be used to make asynchronous requests by importing asyncio. + ```python # Asynchronous Example import asyncio @@ -128,7 +130,8 @@ with Mistral(
-The same SDK client can also be used to make asychronous requests by importing asyncio. +The same SDK client can also be used to make asynchronous requests by importing asyncio. + ```python # Asynchronous Example import asyncio @@ -179,7 +182,8 @@ with Mistral(
-The same SDK client can also be used to make asychronous requests by importing asyncio. +The same SDK client can also be used to make asynchronous requests by importing asyncio. + ```python # Asynchronous Example import asyncio diff --git a/docs/sdks/accesses/README.md b/docs/sdks/accesses/README.md index c41b8454..f06cce76 100644 --- a/docs/sdks/accesses/README.md +++ b/docs/sdks/accesses/README.md @@ -17,6 +17,7 @@ Given a library, list all of the Entity that have access and to what level. ### Example Usage + ```python from mistralai import Mistral import os @@ -57,6 +58,7 @@ Given a library id, you can create or update the access level of an entity. You ### Example Usage + ```python from mistralai import Mistral import os @@ -101,6 +103,7 @@ Given a library id, you can delete the access level of an entity. An owner canno ### Example Usage + ```python from mistralai import Mistral import os diff --git a/docs/sdks/agents/README.md b/docs/sdks/agents/README.md index 6bab08dd..a4e8b22e 100644 --- a/docs/sdks/agents/README.md +++ b/docs/sdks/agents/README.md @@ -16,6 +16,7 @@ Agents Completion ### Example Usage + ```python from mistralai import Mistral import os @@ -75,6 +76,7 @@ Mistral AI provides the ability to stream responses back to a client in order to ### Example Usage + ```python from mistralai import Mistral import os diff --git a/docs/sdks/chat/README.md b/docs/sdks/chat/README.md index 7b467b58..c5c45e0f 100644 --- a/docs/sdks/chat/README.md +++ b/docs/sdks/chat/README.md @@ -16,6 +16,7 @@ Chat Completion ### Example Usage + ```python from mistralai import Mistral import os @@ -78,6 +79,7 @@ Mistral AI provides the ability to stream responses back to a client in order to ### Example Usage + ```python from mistralai import Mistral import os diff --git a/docs/sdks/classifiers/README.md b/docs/sdks/classifiers/README.md index 3f9d3a3c..87eb8d69 100644 --- a/docs/sdks/classifiers/README.md +++ b/docs/sdks/classifiers/README.md @@ -18,6 +18,7 @@ Moderations ### Example Usage + ```python from mistralai import Mistral import os @@ -62,6 +63,7 @@ Chat Moderations ### Example Usage + ```python from mistralai import Mistral import os @@ -108,6 +110,7 @@ Classifications ### Example Usage + ```python from mistralai import Mistral import os @@ -151,6 +154,7 @@ Chat Classifications ### Example Usage + ```python from mistralai import Mistral import os diff --git a/docs/sdks/conversations/README.md b/docs/sdks/conversations/README.md index 2eceb451..d3ce96c2 100644 --- a/docs/sdks/conversations/README.md +++ b/docs/sdks/conversations/README.md @@ -24,6 +24,7 @@ Create a new conversation, using a base model or an agent and append entries. Co ### Example Usage + ```python from mistralai import Mistral import os @@ -74,6 +75,7 @@ Retrieve a list of conversation entities sorted by creation time. ### Example Usage + ```python from mistralai import Mistral import os @@ -115,6 +117,7 @@ Given a conversation_id retrieve a conversation entity with its attributes. ### Example Usage + ```python from mistralai import Mistral import os @@ -155,6 +158,7 @@ Run completion on the history of the conversation and the user entries. Return t ### Example Usage + ```python from mistralai import Mistral import os @@ -200,6 +204,7 @@ Given a conversation_id retrieve all the entries belonging to that conversation. ### Example Usage + ```python from mistralai import Mistral import os @@ -240,6 +245,7 @@ Given a conversation_id retrieve all the messages belonging to that conversation ### Example Usage + ```python from mistralai import Mistral import os @@ -280,6 +286,7 @@ Given a conversation_id and an id, recreate a conversation from this point and r ### Example Usage + ```python from mistralai import Mistral import os @@ -326,6 +333,7 @@ Create a new conversation, using a base model or an agent and append entries. Co ### Example Usage + ```python from mistralai import Mistral import os @@ -385,6 +393,7 @@ Run completion on the history of the conversation and the user entries. Return t ### Example Usage + ```python from mistralai import Mistral import os @@ -432,6 +441,7 @@ Given a conversation_id and an id, recreate a conversation from this point and r ### Example Usage + ```python from mistralai import Mistral import os diff --git a/docs/sdks/documents/README.md b/docs/sdks/documents/README.md index 0b49c05c..71848b07 100644 --- a/docs/sdks/documents/README.md +++ b/docs/sdks/documents/README.md @@ -24,6 +24,7 @@ Given a library, lists the document that have been uploaded to that library. ### Example Usage + ```python from mistralai import Mistral import os @@ -69,6 +70,7 @@ Given a library, upload a new document to that library. It is queued for process ### Example Usage + ```python from mistralai import Mistral import os @@ -113,6 +115,7 @@ Given a library and a document in this library, you can retrieve the metadata of ### Example Usage + ```python from mistralai import Mistral import os @@ -154,6 +157,7 @@ Given a library and a document in that library, update the name of that document ### Example Usage + ```python from mistralai import Mistral import os @@ -196,6 +200,7 @@ Given a library and a document in that library, delete that document. The docume ### Example Usage + ```python from mistralai import Mistral import os @@ -232,6 +237,7 @@ Given a library and a document in that library, you can retrieve the text conten ### Example Usage + ```python from mistralai import Mistral import os @@ -273,6 +279,7 @@ Given a library and a document in that library, retrieve the processing status o ### Example Usage + ```python from mistralai import Mistral import os @@ -314,6 +321,7 @@ Given a library and a document in that library, retrieve the signed URL of a spe ### Example Usage + ```python from mistralai import Mistral import os @@ -355,6 +363,7 @@ Given a library and a document in that library, retrieve the signed URL of text ### Example Usage + ```python from mistralai import Mistral import os @@ -396,6 +405,7 @@ Given a library and a document in that library, reprocess that document, it will ### Example Usage + ```python from mistralai import Mistral import os diff --git a/docs/sdks/embeddings/README.md b/docs/sdks/embeddings/README.md index a145855f..9554e7b7 100644 --- a/docs/sdks/embeddings/README.md +++ b/docs/sdks/embeddings/README.md @@ -15,6 +15,7 @@ Embeddings ### Example Usage + ```python from mistralai import Mistral import os diff --git a/docs/sdks/files/README.md b/docs/sdks/files/README.md index bc39f2e4..e8d28c86 100644 --- a/docs/sdks/files/README.md +++ b/docs/sdks/files/README.md @@ -24,6 +24,7 @@ Please contact us if you need to increase these storage limits. ### Example Usage + ```python from mistralai import Mistral import os @@ -67,6 +68,7 @@ Returns a list of files that belong to the user's organization. ### Example Usage + ```python from mistralai import Mistral import os @@ -111,6 +113,7 @@ Returns information about a specific file. ### Example Usage + ```python from mistralai import Mistral import os @@ -150,6 +153,7 @@ Delete a file. ### Example Usage + ```python from mistralai import Mistral import os @@ -189,6 +193,7 @@ Download a file ### Example Usage + ```python from mistralai import Mistral import os @@ -228,6 +233,7 @@ Get Signed Url ### Example Usage + ```python from mistralai import Mistral import os diff --git a/docs/sdks/fim/README.md b/docs/sdks/fim/README.md index 10e6255d..cce1c070 100644 --- a/docs/sdks/fim/README.md +++ b/docs/sdks/fim/README.md @@ -16,6 +16,7 @@ FIM completion. ### Example Usage + ```python from mistralai import Mistral import os @@ -65,6 +66,7 @@ Mistral AI provides the ability to stream responses back to a client in order to ### Example Usage + ```python from mistralai import Mistral import os diff --git a/docs/sdks/jobs/README.md b/docs/sdks/jobs/README.md index 1e240c33..b06170f8 100644 --- a/docs/sdks/jobs/README.md +++ b/docs/sdks/jobs/README.md @@ -17,6 +17,7 @@ Get a list of fine-tuning jobs for your organization and user. ### Example Usage + ```python from mistralai import Mistral import os @@ -65,6 +66,7 @@ Create a new fine-tuning job, it will be queued for processing. ### Example Usage + ```python from mistralai import Mistral import os @@ -116,6 +118,7 @@ Get a fine-tuned job details by its UUID. ### Example Usage + ```python from mistralai import Mistral import os @@ -155,6 +158,7 @@ Request the cancellation of a fine tuning job. ### Example Usage + ```python from mistralai import Mistral import os @@ -194,6 +198,7 @@ Request the start of a validated fine tuning job. ### Example Usage + ```python from mistralai import Mistral import os diff --git a/docs/sdks/libraries/README.md b/docs/sdks/libraries/README.md index 4b441c85..14d39f97 100644 --- a/docs/sdks/libraries/README.md +++ b/docs/sdks/libraries/README.md @@ -19,6 +19,7 @@ List all libraries that you have created or have been shared with you. ### Example Usage + ```python from mistralai import Mistral import os @@ -57,6 +58,7 @@ Create a new Library, you will be marked as the owner and only you will have the ### Example Usage + ```python from mistralai import Mistral import os @@ -99,6 +101,7 @@ Given a library id, details information about that Library. ### Example Usage + ```python from mistralai import Mistral import os @@ -139,6 +142,7 @@ Given a library id, deletes it together with all documents that have been upload ### Example Usage + ```python from mistralai import Mistral import os @@ -179,6 +183,7 @@ Given a library id, you can update the name and description. ### Example Usage + ```python from mistralai import Mistral import os diff --git a/docs/sdks/mistralagents/README.md b/docs/sdks/mistralagents/README.md index 58082d21..44b7fcf2 100644 --- a/docs/sdks/mistralagents/README.md +++ b/docs/sdks/mistralagents/README.md @@ -19,6 +19,7 @@ Create a new agent giving it instructions, tools, description. The agent is then ### Example Usage + ```python from mistralai import Mistral import os @@ -65,6 +66,7 @@ Retrieve a list of agent entities sorted by creation time. ### Example Usage + ```python from mistralai import Mistral import os @@ -106,6 +108,7 @@ Given an agent retrieve an agent entity with its attributes. ### Example Usage + ```python from mistralai import Mistral import os @@ -146,6 +149,7 @@ Update an agent attributes and create a new version. ### Example Usage + ```python from mistralai import Mistral import os @@ -193,6 +197,7 @@ Switch the version of an agent. ### Example Usage + ```python from mistralai import Mistral import os diff --git a/docs/sdks/mistraljobs/README.md b/docs/sdks/mistraljobs/README.md index ef1e1549..0ef3f138 100644 --- a/docs/sdks/mistraljobs/README.md +++ b/docs/sdks/mistraljobs/README.md @@ -16,6 +16,7 @@ Get a list of batch jobs for your organization and user. ### Example Usage + ```python from mistralai import Mistral import os @@ -62,6 +63,7 @@ Create a new batch job, it will be queued for processing. ### Example Usage + ```python from mistralai import Mistral import os @@ -108,6 +110,7 @@ Get a batch job details by its UUID. ### Example Usage + ```python from mistralai import Mistral import os @@ -147,6 +150,7 @@ Request the cancellation of a batch job. ### Example Usage + ```python from mistralai import Mistral import os diff --git a/docs/sdks/models/README.md b/docs/sdks/models/README.md index 7dd5d1de..3877c545 100644 --- a/docs/sdks/models/README.md +++ b/docs/sdks/models/README.md @@ -20,6 +20,7 @@ List all models available to the user. ### Example Usage + ```python from mistralai import Mistral import os @@ -59,6 +60,7 @@ Retrieve information about a model. ### Example Usage + ```python from mistralai import Mistral import os @@ -99,6 +101,7 @@ Delete a fine-tuned model. ### Example Usage + ```python from mistralai import Mistral import os @@ -139,6 +142,7 @@ Update a model name or description. ### Example Usage + ```python from mistralai import Mistral import os @@ -180,6 +184,7 @@ Archive a fine-tuned model. ### Example Usage + ```python from mistralai import Mistral import os @@ -219,6 +224,7 @@ Un-archive a fine-tuned model. ### Example Usage + ```python from mistralai import Mistral import os diff --git a/docs/sdks/ocr/README.md b/docs/sdks/ocr/README.md index 2188f378..c0c1293e 100644 --- a/docs/sdks/ocr/README.md +++ b/docs/sdks/ocr/README.md @@ -15,6 +15,7 @@ OCR ### Example Usage + ```python from mistralai import Mistral import os diff --git a/docs/sdks/transcriptions/README.md b/docs/sdks/transcriptions/README.md index fcac2467..022066ac 100644 --- a/docs/sdks/transcriptions/README.md +++ b/docs/sdks/transcriptions/README.md @@ -16,6 +16,7 @@ Create Transcription ### Example Usage + ```python from mistralai import Mistral import os @@ -61,6 +62,7 @@ Create streaming transcription (SSE) ### Example Usage + ```python from mistralai import Mistral import os diff --git a/poetry.toml b/poetry.toml index ab1033bd..cd3492ac 100644 --- a/poetry.toml +++ b/poetry.toml @@ -1,2 +1,3 @@ + [virtualenvs] in-project = true diff --git a/scripts/prepare_readme.py b/scripts/prepare_readme.py index 16f6fc7e..1b0a56ec 100644 --- a/scripts/prepare_readme.py +++ b/scripts/prepare_readme.py @@ -10,12 +10,14 @@ GITHUB_URL = ( GITHUB_URL[: -len(".git")] if GITHUB_URL.endswith(".git") else GITHUB_URL ) + REPO_SUBDIR = "" # links on PyPI should have absolute URLs readme_contents = re.sub( r"(\[[^\]]+\]\()((?!https?:)[^\)]+)(\))", lambda m: m.group(1) + GITHUB_URL + "/blob/master/" + + REPO_SUBDIR + m.group(2) + m.group(3), readme_contents, diff --git a/scripts/publish.sh b/scripts/publish.sh index f2f2cf2c..2a3ead70 100755 --- a/scripts/publish.sh +++ b/scripts/publish.sh @@ -1,5 +1,4 @@ #!/usr/bin/env bash - export POETRY_PYPI_TOKEN_PYPI=${PYPI_TOKEN} poetry run python scripts/prepare_readme.py diff --git a/src/mistralai/_version.py b/src/mistralai/_version.py index 3fa6e7eb..6d3828b9 100644 --- a/src/mistralai/_version.py +++ b/src/mistralai/_version.py @@ -5,8 +5,8 @@ __title__: str = "mistralai" __version__: str = "1.9.10" __openapi_doc_version__: str = "1.0.0" -__gen_version__: str = "2.634.2" -__user_agent__: str = "speakeasy-sdk/python 1.9.10 2.634.2 1.0.0 mistralai" +__gen_version__: str = "2.716.5" +__user_agent__: str = "speakeasy-sdk/python 1.9.10 2.716.5 1.0.0 mistralai" try: if __package__ is not None: diff --git a/src/mistralai/accesses.py b/src/mistralai/accesses.py index f5f5b446..ea33517b 100644 --- a/src/mistralai/accesses.py +++ b/src/mistralai/accesses.py @@ -5,6 +5,7 @@ from mistralai._hooks import HookContext from mistralai.types import OptionalNullable, UNSET from mistralai.utils import get_security_from_env +from mistralai.utils.unmarshal_json_response import unmarshal_json_response from typing import Any, Mapping, Optional @@ -85,31 +86,20 @@ def list( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ListSharingOut) + return unmarshal_json_response(models.ListSharingOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def list_async( self, @@ -185,31 +175,20 @@ async def list_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ListSharingOut) + return unmarshal_json_response(models.ListSharingOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def update_or_create( self, @@ -302,31 +281,20 @@ def update_or_create( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.SharingOut) + return unmarshal_json_response(models.SharingOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def update_or_create_async( self, @@ -419,31 +387,20 @@ async def update_or_create_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.SharingOut) + return unmarshal_json_response(models.SharingOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def delete( self, @@ -533,31 +490,20 @@ def delete( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.SharingOut) + return unmarshal_json_response(models.SharingOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def delete_async( self, @@ -647,28 +593,17 @@ async def delete_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.SharingOut) + return unmarshal_json_response(models.SharingOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) diff --git a/src/mistralai/agents.py b/src/mistralai/agents.py index 48c06372..b220ca5b 100644 --- a/src/mistralai/agents.py +++ b/src/mistralai/agents.py @@ -5,6 +5,7 @@ from mistralai._hooks import HookContext from mistralai.types import OptionalNullable, UNSET from mistralai.utils import eventstreaming, get_security_from_env +from mistralai.utils.unmarshal_json_response import unmarshal_json_response from typing import Any, List, Mapping, Optional, Union @@ -155,31 +156,20 @@ def complete( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ChatCompletionResponse) + return unmarshal_json_response(models.ChatCompletionResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def complete_async( self, @@ -325,31 +315,20 @@ async def complete_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ChatCompletionResponse) + return unmarshal_json_response(models.ChatCompletionResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def stream( self, @@ -502,32 +481,23 @@ def stream( http_res, lambda raw: utils.unmarshal_json(raw, models.CompletionEvent), sentinel="[DONE]", + client_ref=self, ) if utils.match_response(http_res, "422", "application/json"): http_res_text = utils.stream_to_text(http_res) - response_data = utils.unmarshal_json( - http_res_text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res, http_res_text ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res, http_res_text) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res, http_res_text) async def stream_async( self, @@ -680,29 +650,20 @@ async def stream_async( http_res, lambda raw: utils.unmarshal_json(raw, models.CompletionEvent), sentinel="[DONE]", + client_ref=self, ) if utils.match_response(http_res, "422", "application/json"): http_res_text = await utils.stream_to_text_async(http_res) - response_data = utils.unmarshal_json( - http_res_text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res, http_res_text ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res, http_res_text) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res, http_res_text) diff --git a/src/mistralai/audio.py b/src/mistralai/audio.py index 66934a86..5687abdb 100644 --- a/src/mistralai/audio.py +++ b/src/mistralai/audio.py @@ -3,16 +3,21 @@ from .basesdk import BaseSDK from .sdkconfiguration import SDKConfiguration from mistralai.transcriptions import Transcriptions +from typing import Optional class Audio(BaseSDK): transcriptions: Transcriptions r"""API for audio transcription.""" - def __init__(self, sdk_config: SDKConfiguration) -> None: - BaseSDK.__init__(self, sdk_config) + def __init__( + self, sdk_config: SDKConfiguration, parent_ref: Optional[object] = None + ) -> None: + BaseSDK.__init__(self, sdk_config, parent_ref=parent_ref) self.sdk_configuration = sdk_config self._init_sdks() def _init_sdks(self): - self.transcriptions = Transcriptions(self.sdk_configuration) + self.transcriptions = Transcriptions( + self.sdk_configuration, parent_ref=self.parent_ref + ) diff --git a/src/mistralai/basesdk.py b/src/mistralai/basesdk.py index bb37a1ee..6b62ddae 100644 --- a/src/mistralai/basesdk.py +++ b/src/mistralai/basesdk.py @@ -15,9 +15,19 @@ class BaseSDK: sdk_configuration: SDKConfiguration + parent_ref: Optional[object] = None + """ + Reference to the root SDK instance, if any. This will prevent it from + being garbage collected while there are active streams. + """ - def __init__(self, sdk_config: SDKConfiguration) -> None: + def __init__( + self, + sdk_config: SDKConfiguration, + parent_ref: Optional[object] = None, + ) -> None: self.sdk_configuration = sdk_config + self.parent_ref = parent_ref def _get_url(self, base_url, url_variables): sdk_url, sdk_variables = self.sdk_configuration.get_server_details() @@ -244,7 +254,7 @@ def do(): if http_res is None: logger.debug("Raising no response SDK error") - raise models.SDKError("No response received") + raise models.NoResponseError("No response received") logger.debug( "Response:\nStatus Code: %s\nURL: %s\nHeaders: %s\nBody: %s", @@ -265,7 +275,7 @@ def do(): http_res = result else: logger.debug("Raising unexpected SDK error") - raise models.SDKError("Unexpected error occurred") + raise models.SDKError("Unexpected error occurred", http_res) return http_res @@ -316,7 +326,7 @@ async def do(): if http_res is None: logger.debug("Raising no response SDK error") - raise models.SDKError("No response received") + raise models.NoResponseError("No response received") logger.debug( "Response:\nStatus Code: %s\nURL: %s\nHeaders: %s\nBody: %s", @@ -337,7 +347,7 @@ async def do(): http_res = result else: logger.debug("Raising unexpected SDK error") - raise models.SDKError("Unexpected error occurred") + raise models.SDKError("Unexpected error occurred", http_res) return http_res diff --git a/src/mistralai/batch.py b/src/mistralai/batch.py index bb59abda..7ed7ccef 100644 --- a/src/mistralai/batch.py +++ b/src/mistralai/batch.py @@ -3,15 +3,18 @@ from .basesdk import BaseSDK from .sdkconfiguration import SDKConfiguration from mistralai.mistral_jobs import MistralJobs +from typing import Optional class Batch(BaseSDK): jobs: MistralJobs - def __init__(self, sdk_config: SDKConfiguration) -> None: - BaseSDK.__init__(self, sdk_config) + def __init__( + self, sdk_config: SDKConfiguration, parent_ref: Optional[object] = None + ) -> None: + BaseSDK.__init__(self, sdk_config, parent_ref=parent_ref) self.sdk_configuration = sdk_config self._init_sdks() def _init_sdks(self): - self.jobs = MistralJobs(self.sdk_configuration) + self.jobs = MistralJobs(self.sdk_configuration, parent_ref=self.parent_ref) diff --git a/src/mistralai/beta.py b/src/mistralai/beta.py index 5201022e..4bbf1fa3 100644 --- a/src/mistralai/beta.py +++ b/src/mistralai/beta.py @@ -5,6 +5,7 @@ from mistralai.conversations import Conversations from mistralai.libraries import Libraries from mistralai.mistral_agents import MistralAgents +from typing import Optional class Beta(BaseSDK): @@ -15,12 +16,16 @@ class Beta(BaseSDK): libraries: Libraries r"""(beta) Libraries API to create and manage libraries - index your documents to enhance agent capabilities.""" - def __init__(self, sdk_config: SDKConfiguration) -> None: - BaseSDK.__init__(self, sdk_config) + def __init__( + self, sdk_config: SDKConfiguration, parent_ref: Optional[object] = None + ) -> None: + BaseSDK.__init__(self, sdk_config, parent_ref=parent_ref) self.sdk_configuration = sdk_config self._init_sdks() def _init_sdks(self): - self.conversations = Conversations(self.sdk_configuration) - self.agents = MistralAgents(self.sdk_configuration) - self.libraries = Libraries(self.sdk_configuration) + self.conversations = Conversations( + self.sdk_configuration, parent_ref=self.parent_ref + ) + self.agents = MistralAgents(self.sdk_configuration, parent_ref=self.parent_ref) + self.libraries = Libraries(self.sdk_configuration, parent_ref=self.parent_ref) diff --git a/src/mistralai/chat.py b/src/mistralai/chat.py index 1ed067e8..67777a1a 100644 --- a/src/mistralai/chat.py +++ b/src/mistralai/chat.py @@ -5,6 +5,7 @@ from mistralai._hooks import HookContext from mistralai.types import OptionalNullable, UNSET from mistralai.utils import eventstreaming, get_security_from_env +from mistralai.utils.unmarshal_json_response import unmarshal_json_response from typing import Any, List, Mapping, Optional, Union # region imports @@ -236,31 +237,20 @@ def complete( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ChatCompletionResponse) + return unmarshal_json_response(models.ChatCompletionResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def complete_async( self, @@ -405,31 +395,20 @@ async def complete_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ChatCompletionResponse) + return unmarshal_json_response(models.ChatCompletionResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def stream( self, @@ -591,32 +570,23 @@ def stream( http_res, lambda raw: utils.unmarshal_json(raw, models.CompletionEvent), sentinel="[DONE]", + client_ref=self, ) if utils.match_response(http_res, "422", "application/json"): http_res_text = utils.stream_to_text(http_res) - response_data = utils.unmarshal_json( - http_res_text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res, http_res_text ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res, http_res_text) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res, http_res_text) async def stream_async( self, @@ -778,29 +748,20 @@ async def stream_async( http_res, lambda raw: utils.unmarshal_json(raw, models.CompletionEvent), sentinel="[DONE]", + client_ref=self, ) if utils.match_response(http_res, "422", "application/json"): http_res_text = await utils.stream_to_text_async(http_res) - response_data = utils.unmarshal_json( - http_res_text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res, http_res_text ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res, http_res_text) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res, http_res_text) diff --git a/src/mistralai/classifiers.py b/src/mistralai/classifiers.py index 6ebf7834..cd6a9415 100644 --- a/src/mistralai/classifiers.py +++ b/src/mistralai/classifiers.py @@ -5,6 +5,7 @@ from mistralai._hooks import HookContext from mistralai.types import OptionalNullable, UNSET from mistralai.utils import get_security_from_env +from mistralai.utils.unmarshal_json_response import unmarshal_json_response from typing import Any, Mapping, Optional, Union @@ -92,31 +93,20 @@ def moderate( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ModerationResponse) + return unmarshal_json_response(models.ModerationResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def moderate_async( self, @@ -199,31 +189,20 @@ async def moderate_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ModerationResponse) + return unmarshal_json_response(models.ModerationResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def moderate_chat( self, @@ -306,31 +285,20 @@ def moderate_chat( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ModerationResponse) + return unmarshal_json_response(models.ModerationResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def moderate_chat_async( self, @@ -413,31 +381,20 @@ async def moderate_chat_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ModerationResponse) + return unmarshal_json_response(models.ModerationResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def classify( self, @@ -520,31 +477,20 @@ def classify( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ClassificationResponse) + return unmarshal_json_response(models.ClassificationResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def classify_async( self, @@ -627,31 +573,20 @@ async def classify_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ClassificationResponse) + return unmarshal_json_response(models.ClassificationResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def classify_chat( self, @@ -731,31 +666,20 @@ def classify_chat( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ClassificationResponse) + return unmarshal_json_response(models.ClassificationResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def classify_chat_async( self, @@ -835,28 +759,17 @@ async def classify_chat_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ClassificationResponse) + return unmarshal_json_response(models.ClassificationResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) diff --git a/src/mistralai/conversations.py b/src/mistralai/conversations.py index 12d690f8..27eddedf 100644 --- a/src/mistralai/conversations.py +++ b/src/mistralai/conversations.py @@ -5,6 +5,7 @@ from mistralai._hooks import HookContext from mistralai.types import OptionalNullable, UNSET from mistralai.utils import eventstreaming, get_security_from_env +from mistralai.utils.unmarshal_json_response import unmarshal_json_response from typing import Any, List, Mapping, Optional, Union # region imports @@ -328,31 +329,20 @@ def start( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ConversationResponse) + return unmarshal_json_response(models.ConversationResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def start_async( self, @@ -467,31 +457,20 @@ async def start_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ConversationResponse) + return unmarshal_json_response(models.ConversationResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def list( self, @@ -570,31 +549,20 @@ def list( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, List[models.ResponseBody]) + return unmarshal_json_response(List[models.ResponseBody], http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def list_async( self, @@ -673,31 +641,20 @@ async def list_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, List[models.ResponseBody]) + return unmarshal_json_response(List[models.ResponseBody], http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def get( self, @@ -773,34 +730,22 @@ def get( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json( - http_res.text, - models.AgentsAPIV1ConversationsGetResponseV1ConversationsGet, + return unmarshal_json_response( + models.AgentsAPIV1ConversationsGetResponseV1ConversationsGet, http_res ) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def get_async( self, @@ -876,34 +821,22 @@ async def get_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json( - http_res.text, - models.AgentsAPIV1ConversationsGetResponseV1ConversationsGet, + return unmarshal_json_response( + models.AgentsAPIV1ConversationsGetResponseV1ConversationsGet, http_res ) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def append( self, @@ -1009,31 +942,20 @@ def append( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ConversationResponse) + return unmarshal_json_response(models.ConversationResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def append_async( self, @@ -1139,31 +1061,20 @@ async def append_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ConversationResponse) + return unmarshal_json_response(models.ConversationResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def get_history( self, @@ -1239,31 +1150,20 @@ def get_history( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ConversationHistory) + return unmarshal_json_response(models.ConversationHistory, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def get_history_async( self, @@ -1339,31 +1239,20 @@ async def get_history_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ConversationHistory) + return unmarshal_json_response(models.ConversationHistory, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def get_messages( self, @@ -1439,31 +1328,20 @@ def get_messages( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ConversationMessages) + return unmarshal_json_response(models.ConversationMessages, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def get_messages_async( self, @@ -1539,31 +1417,20 @@ async def get_messages_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ConversationMessages) + return unmarshal_json_response(models.ConversationMessages, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def restart( self, @@ -1672,31 +1539,20 @@ def restart( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ConversationResponse) + return unmarshal_json_response(models.ConversationResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def restart_async( self, @@ -1805,31 +1661,20 @@ async def restart_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ConversationResponse) + return unmarshal_json_response(models.ConversationResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def start_stream( self, @@ -1955,32 +1800,23 @@ def start_stream( return eventstreaming.EventStream( http_res, lambda raw: utils.unmarshal_json(raw, models.ConversationEvents), + client_ref=self, ) if utils.match_response(http_res, "422", "application/json"): http_res_text = utils.stream_to_text(http_res) - response_data = utils.unmarshal_json( - http_res_text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res, http_res_text ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res, http_res_text) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res, http_res_text) async def start_stream_async( self, @@ -2106,32 +1942,23 @@ async def start_stream_async( return eventstreaming.EventStreamAsync( http_res, lambda raw: utils.unmarshal_json(raw, models.ConversationEvents), + client_ref=self, ) if utils.match_response(http_res, "422", "application/json"): http_res_text = await utils.stream_to_text_async(http_res) - response_data = utils.unmarshal_json( - http_res_text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res, http_res_text ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res, http_res_text) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res, http_res_text) def append_stream( self, @@ -2241,32 +2068,23 @@ def append_stream( return eventstreaming.EventStream( http_res, lambda raw: utils.unmarshal_json(raw, models.ConversationEvents), + client_ref=self, ) if utils.match_response(http_res, "422", "application/json"): http_res_text = utils.stream_to_text(http_res) - response_data = utils.unmarshal_json( - http_res_text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res, http_res_text ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res, http_res_text) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res, http_res_text) async def append_stream_async( self, @@ -2376,32 +2194,23 @@ async def append_stream_async( return eventstreaming.EventStreamAsync( http_res, lambda raw: utils.unmarshal_json(raw, models.ConversationEvents), + client_ref=self, ) if utils.match_response(http_res, "422", "application/json"): http_res_text = await utils.stream_to_text_async(http_res) - response_data = utils.unmarshal_json( - http_res_text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res, http_res_text ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res, http_res_text) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res, http_res_text) def restart_stream( self, @@ -2514,32 +2323,23 @@ def restart_stream( return eventstreaming.EventStream( http_res, lambda raw: utils.unmarshal_json(raw, models.ConversationEvents), + client_ref=self, ) if utils.match_response(http_res, "422", "application/json"): http_res_text = utils.stream_to_text(http_res) - response_data = utils.unmarshal_json( - http_res_text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res, http_res_text ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res, http_res_text) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res, http_res_text) async def restart_stream_async( self, @@ -2652,29 +2452,20 @@ async def restart_stream_async( return eventstreaming.EventStreamAsync( http_res, lambda raw: utils.unmarshal_json(raw, models.ConversationEvents), + client_ref=self, ) if utils.match_response(http_res, "422", "application/json"): http_res_text = await utils.stream_to_text_async(http_res) - response_data = utils.unmarshal_json( - http_res_text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res, http_res_text ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res, http_res_text) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res, http_res_text) diff --git a/src/mistralai/documents.py b/src/mistralai/documents.py index c28758d2..5f8c6b9f 100644 --- a/src/mistralai/documents.py +++ b/src/mistralai/documents.py @@ -5,6 +5,7 @@ from mistralai._hooks import HookContext from mistralai.types import OptionalNullable, UNSET from mistralai.utils import get_security_from_env +from mistralai.utils.unmarshal_json_response import unmarshal_json_response from typing import Any, Mapping, Optional, Union @@ -100,31 +101,20 @@ def list( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ListDocumentOut) + return unmarshal_json_response(models.ListDocumentOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def list_async( self, @@ -215,31 +205,20 @@ async def list_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ListDocumentOut) + return unmarshal_json_response(models.ListDocumentOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def upload( self, @@ -327,31 +306,20 @@ def upload( response_data: Any = None if utils.match_response(http_res, ["200", "201"], "application/json"): - return utils.unmarshal_json(http_res.text, models.DocumentOut) + return unmarshal_json_response(models.DocumentOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def upload_async( self, @@ -439,31 +407,20 @@ async def upload_async( response_data: Any = None if utils.match_response(http_res, ["200", "201"], "application/json"): - return utils.unmarshal_json(http_res.text, models.DocumentOut) + return unmarshal_json_response(models.DocumentOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def get( self, @@ -542,31 +499,20 @@ def get( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.DocumentOut) + return unmarshal_json_response(models.DocumentOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def get_async( self, @@ -645,31 +591,20 @@ async def get_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.DocumentOut) + return unmarshal_json_response(models.DocumentOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def update( self, @@ -760,31 +695,20 @@ def update( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.DocumentOut) + return unmarshal_json_response(models.DocumentOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def update_async( self, @@ -875,31 +799,20 @@ async def update_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.DocumentOut) + return unmarshal_json_response(models.DocumentOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def delete( self, @@ -980,29 +893,18 @@ def delete( if utils.match_response(http_res, "204", "*"): return if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def delete_async( self, @@ -1083,29 +985,18 @@ async def delete_async( if utils.match_response(http_res, "204", "*"): return if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def text_content( self, @@ -1184,31 +1075,20 @@ def text_content( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.DocumentTextContent) + return unmarshal_json_response(models.DocumentTextContent, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def text_content_async( self, @@ -1287,31 +1167,20 @@ async def text_content_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.DocumentTextContent) + return unmarshal_json_response(models.DocumentTextContent, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def status( self, @@ -1390,31 +1259,20 @@ def status( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ProcessingStatusOut) + return unmarshal_json_response(models.ProcessingStatusOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def status_async( self, @@ -1493,31 +1351,20 @@ async def status_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ProcessingStatusOut) + return unmarshal_json_response(models.ProcessingStatusOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def get_signed_url( self, @@ -1596,31 +1443,20 @@ def get_signed_url( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, str) + return unmarshal_json_response(str, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def get_signed_url_async( self, @@ -1699,31 +1535,20 @@ async def get_signed_url_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, str) + return unmarshal_json_response(str, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def extracted_text_signed_url( self, @@ -1802,31 +1627,20 @@ def extracted_text_signed_url( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, str) + return unmarshal_json_response(str, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def extracted_text_signed_url_async( self, @@ -1905,31 +1719,20 @@ async def extracted_text_signed_url_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, str) + return unmarshal_json_response(str, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def reprocess( self, @@ -2010,29 +1813,18 @@ def reprocess( if utils.match_response(http_res, "204", "*"): return if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def reprocess_async( self, @@ -2113,26 +1905,15 @@ async def reprocess_async( if utils.match_response(http_res, "204", "*"): return if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) diff --git a/src/mistralai/embeddings.py b/src/mistralai/embeddings.py index 79309eea..1822a1ec 100644 --- a/src/mistralai/embeddings.py +++ b/src/mistralai/embeddings.py @@ -5,6 +5,7 @@ from mistralai._hooks import HookContext from mistralai.types import OptionalNullable, UNSET from mistralai.utils import get_security_from_env +from mistralai.utils.unmarshal_json_response import unmarshal_json_response from typing import Any, Mapping, Optional, Union @@ -102,31 +103,20 @@ def create( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.EmbeddingResponse) + return unmarshal_json_response(models.EmbeddingResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def create_async( self, @@ -219,28 +209,17 @@ async def create_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.EmbeddingResponse) + return unmarshal_json_response(models.EmbeddingResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) diff --git a/src/mistralai/files.py b/src/mistralai/files.py index 05739eeb..c6e438af 100644 --- a/src/mistralai/files.py +++ b/src/mistralai/files.py @@ -6,6 +6,7 @@ from mistralai._hooks import HookContext from mistralai.types import OptionalNullable, UNSET from mistralai.utils import get_security_from_env +from mistralai.utils.unmarshal_json_response import unmarshal_json_response from typing import List, Mapping, Optional, Union @@ -99,26 +100,15 @@ def upload( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.UploadFileOut) + return unmarshal_json_response(models.UploadFileOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def upload_async( self, @@ -207,26 +197,15 @@ async def upload_async( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.UploadFileOut) + return unmarshal_json_response(models.UploadFileOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def list( self, @@ -316,26 +295,15 @@ def list( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ListFilesOut) + return unmarshal_json_response(models.ListFilesOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def list_async( self, @@ -425,26 +393,15 @@ async def list_async( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ListFilesOut) + return unmarshal_json_response(models.ListFilesOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def retrieve( self, @@ -519,26 +476,15 @@ def retrieve( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.RetrieveFileOut) + return unmarshal_json_response(models.RetrieveFileOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def retrieve_async( self, @@ -613,26 +559,15 @@ async def retrieve_async( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.RetrieveFileOut) + return unmarshal_json_response(models.RetrieveFileOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def delete( self, @@ -707,26 +642,15 @@ def delete( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.DeleteFileOut) + return unmarshal_json_response(models.DeleteFileOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def delete_async( self, @@ -801,26 +725,15 @@ async def delete_async( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.DeleteFileOut) + return unmarshal_json_response(models.DeleteFileOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def download( self, @@ -899,23 +812,13 @@ def download( return http_res if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res, http_res_text) async def download_async( self, @@ -994,23 +897,13 @@ async def download_async( return http_res if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res, http_res_text) def get_signed_url( self, @@ -1086,26 +979,15 @@ def get_signed_url( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.FileSignedURL) + return unmarshal_json_response(models.FileSignedURL, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def get_signed_url_async( self, @@ -1181,23 +1063,12 @@ async def get_signed_url_async( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.FileSignedURL) + return unmarshal_json_response(models.FileSignedURL, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) diff --git a/src/mistralai/fim.py b/src/mistralai/fim.py index c57bc68e..fa7b15c2 100644 --- a/src/mistralai/fim.py +++ b/src/mistralai/fim.py @@ -5,6 +5,7 @@ from mistralai._hooks import HookContext from mistralai.types import OptionalNullable, UNSET from mistralai.utils import eventstreaming, get_security_from_env +from mistralai.utils.unmarshal_json_response import unmarshal_json_response from typing import Any, Mapping, Optional, Union @@ -120,31 +121,20 @@ def complete( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.FIMCompletionResponse) + return unmarshal_json_response(models.FIMCompletionResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def complete_async( self, @@ -255,31 +245,20 @@ async def complete_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.FIMCompletionResponse) + return unmarshal_json_response(models.FIMCompletionResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def stream( self, @@ -395,32 +374,23 @@ def stream( http_res, lambda raw: utils.unmarshal_json(raw, models.CompletionEvent), sentinel="[DONE]", + client_ref=self, ) if utils.match_response(http_res, "422", "application/json"): http_res_text = utils.stream_to_text(http_res) - response_data = utils.unmarshal_json( - http_res_text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res, http_res_text ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res, http_res_text) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res, http_res_text) async def stream_async( self, @@ -536,29 +506,20 @@ async def stream_async( http_res, lambda raw: utils.unmarshal_json(raw, models.CompletionEvent), sentinel="[DONE]", + client_ref=self, ) if utils.match_response(http_res, "422", "application/json"): http_res_text = await utils.stream_to_text_async(http_res) - response_data = utils.unmarshal_json( - http_res_text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res, http_res_text ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res, http_res_text) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res, http_res_text) diff --git a/src/mistralai/fine_tuning.py b/src/mistralai/fine_tuning.py index ce3d1389..8ed5788a 100644 --- a/src/mistralai/fine_tuning.py +++ b/src/mistralai/fine_tuning.py @@ -3,15 +3,18 @@ from .basesdk import BaseSDK from .sdkconfiguration import SDKConfiguration from mistralai.jobs import Jobs +from typing import Optional class FineTuning(BaseSDK): jobs: Jobs - def __init__(self, sdk_config: SDKConfiguration) -> None: - BaseSDK.__init__(self, sdk_config) + def __init__( + self, sdk_config: SDKConfiguration, parent_ref: Optional[object] = None + ) -> None: + BaseSDK.__init__(self, sdk_config, parent_ref=parent_ref) self.sdk_configuration = sdk_config self._init_sdks() def _init_sdks(self): - self.jobs = Jobs(self.sdk_configuration) + self.jobs = Jobs(self.sdk_configuration, parent_ref=self.parent_ref) diff --git a/src/mistralai/jobs.py b/src/mistralai/jobs.py index 020c40f0..af6364cb 100644 --- a/src/mistralai/jobs.py +++ b/src/mistralai/jobs.py @@ -6,6 +6,7 @@ from mistralai._hooks import HookContext from mistralai.types import OptionalNullable, UNSET from mistralai.utils import get_security_from_env +from mistralai.utils.unmarshal_json_response import unmarshal_json_response from typing import List, Mapping, Optional, Union @@ -110,26 +111,15 @@ def list( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.JobsOut) + return unmarshal_json_response(models.JobsOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def list_async( self, @@ -231,26 +221,15 @@ async def list_async( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.JobsOut) + return unmarshal_json_response(models.JobsOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def create( self, @@ -383,28 +362,17 @@ def create( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json( - http_res.text, models.JobsAPIRoutesFineTuningCreateFineTuningJobResponse + return unmarshal_json_response( + models.JobsAPIRoutesFineTuningCreateFineTuningJobResponse, http_res ) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def create_async( self, @@ -537,28 +505,17 @@ async def create_async( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json( - http_res.text, models.JobsAPIRoutesFineTuningCreateFineTuningJobResponse + return unmarshal_json_response( + models.JobsAPIRoutesFineTuningCreateFineTuningJobResponse, http_res ) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def get( self, @@ -633,28 +590,17 @@ def get( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json( - http_res.text, models.JobsAPIRoutesFineTuningGetFineTuningJobResponse + return unmarshal_json_response( + models.JobsAPIRoutesFineTuningGetFineTuningJobResponse, http_res ) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def get_async( self, @@ -729,28 +675,17 @@ async def get_async( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json( - http_res.text, models.JobsAPIRoutesFineTuningGetFineTuningJobResponse + return unmarshal_json_response( + models.JobsAPIRoutesFineTuningGetFineTuningJobResponse, http_res ) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def cancel( self, @@ -825,28 +760,17 @@ def cancel( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json( - http_res.text, models.JobsAPIRoutesFineTuningCancelFineTuningJobResponse + return unmarshal_json_response( + models.JobsAPIRoutesFineTuningCancelFineTuningJobResponse, http_res ) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def cancel_async( self, @@ -921,28 +845,17 @@ async def cancel_async( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json( - http_res.text, models.JobsAPIRoutesFineTuningCancelFineTuningJobResponse + return unmarshal_json_response( + models.JobsAPIRoutesFineTuningCancelFineTuningJobResponse, http_res ) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def start( self, @@ -1017,28 +930,17 @@ def start( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json( - http_res.text, models.JobsAPIRoutesFineTuningStartFineTuningJobResponse + return unmarshal_json_response( + models.JobsAPIRoutesFineTuningStartFineTuningJobResponse, http_res ) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def start_async( self, @@ -1113,25 +1015,14 @@ async def start_async( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json( - http_res.text, models.JobsAPIRoutesFineTuningStartFineTuningJobResponse + return unmarshal_json_response( + models.JobsAPIRoutesFineTuningStartFineTuningJobResponse, http_res ) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) diff --git a/src/mistralai/libraries.py b/src/mistralai/libraries.py index 852f6997..e9f19047 100644 --- a/src/mistralai/libraries.py +++ b/src/mistralai/libraries.py @@ -8,6 +8,7 @@ from mistralai.documents import Documents from mistralai.types import OptionalNullable, UNSET from mistralai.utils import get_security_from_env +from mistralai.utils.unmarshal_json_response import unmarshal_json_response from typing import Any, Mapping, Optional @@ -19,14 +20,16 @@ class Libraries(BaseSDK): accesses: Accesses r"""(beta) Libraries API - manage access to a library.""" - def __init__(self, sdk_config: SDKConfiguration) -> None: - BaseSDK.__init__(self, sdk_config) + def __init__( + self, sdk_config: SDKConfiguration, parent_ref: Optional[object] = None + ) -> None: + BaseSDK.__init__(self, sdk_config, parent_ref=parent_ref) self.sdk_configuration = sdk_config self._init_sdks() def _init_sdks(self): - self.documents = Documents(self.sdk_configuration) - self.accesses = Accesses(self.sdk_configuration) + self.documents = Documents(self.sdk_configuration, parent_ref=self.parent_ref) + self.accesses = Accesses(self.sdk_configuration, parent_ref=self.parent_ref) def list( self, @@ -94,26 +97,15 @@ def list( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ListLibraryOut) + return unmarshal_json_response(models.ListLibraryOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def list_async( self, @@ -181,26 +173,15 @@ async def list_async( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ListLibraryOut) + return unmarshal_json_response(models.ListLibraryOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def create( self, @@ -285,31 +266,20 @@ def create( response_data: Any = None if utils.match_response(http_res, "201", "application/json"): - return utils.unmarshal_json(http_res.text, models.LibraryOut) + return unmarshal_json_response(models.LibraryOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def create_async( self, @@ -394,31 +364,20 @@ async def create_async( response_data: Any = None if utils.match_response(http_res, "201", "application/json"): - return utils.unmarshal_json(http_res.text, models.LibraryOut) + return unmarshal_json_response(models.LibraryOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def get( self, @@ -494,31 +453,20 @@ def get( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.LibraryOut) + return unmarshal_json_response(models.LibraryOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def get_async( self, @@ -594,31 +542,20 @@ async def get_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.LibraryOut) + return unmarshal_json_response(models.LibraryOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def delete( self, @@ -694,31 +631,20 @@ def delete( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.LibraryOut) + return unmarshal_json_response(models.LibraryOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def delete_async( self, @@ -794,31 +720,20 @@ async def delete_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.LibraryOut) + return unmarshal_json_response(models.LibraryOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def update( self, @@ -905,31 +820,20 @@ def update( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.LibraryOut) + return unmarshal_json_response(models.LibraryOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def update_async( self, @@ -1016,28 +920,17 @@ async def update_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.LibraryOut) + return unmarshal_json_response(models.LibraryOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) diff --git a/src/mistralai/mistral_agents.py b/src/mistralai/mistral_agents.py index f0d4be01..65f256d6 100644 --- a/src/mistralai/mistral_agents.py +++ b/src/mistralai/mistral_agents.py @@ -5,6 +5,7 @@ from mistralai._hooks import HookContext from mistralai.types import OptionalNullable, UNSET from mistralai.utils import get_security_from_env +from mistralai.utils.unmarshal_json_response import unmarshal_json_response from typing import Any, List, Mapping, Optional, Union @@ -117,31 +118,20 @@ def create( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.Agent) + return unmarshal_json_response(models.Agent, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def create_async( self, @@ -249,31 +239,20 @@ async def create_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.Agent) + return unmarshal_json_response(models.Agent, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def list( self, @@ -352,31 +331,20 @@ def list( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, List[models.Agent]) + return unmarshal_json_response(List[models.Agent], http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def list_async( self, @@ -455,31 +423,20 @@ async def list_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, List[models.Agent]) + return unmarshal_json_response(List[models.Agent], http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def get( self, @@ -555,31 +512,20 @@ def get( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.Agent) + return unmarshal_json_response(models.Agent, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def get_async( self, @@ -655,31 +601,20 @@ async def get_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.Agent) + return unmarshal_json_response(models.Agent, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def update( self, @@ -796,31 +731,20 @@ def update( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.Agent) + return unmarshal_json_response(models.Agent, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def update_async( self, @@ -937,31 +861,20 @@ async def update_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.Agent) + return unmarshal_json_response(models.Agent, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def update_version( self, @@ -1040,31 +953,20 @@ def update_version( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.Agent) + return unmarshal_json_response(models.Agent, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def update_version_async( self, @@ -1143,28 +1045,17 @@ async def update_version_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.Agent) + return unmarshal_json_response(models.Agent, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) diff --git a/src/mistralai/mistral_jobs.py b/src/mistralai/mistral_jobs.py index c51d64a7..fb0a0de7 100644 --- a/src/mistralai/mistral_jobs.py +++ b/src/mistralai/mistral_jobs.py @@ -6,6 +6,7 @@ from mistralai._hooks import HookContext from mistralai.types import OptionalNullable, UNSET from mistralai.utils import get_security_from_env +from mistralai.utils.unmarshal_json_response import unmarshal_json_response from typing import Any, Dict, List, Mapping, Optional @@ -104,26 +105,15 @@ def list( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.BatchJobsOut) + return unmarshal_json_response(models.BatchJobsOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) - - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("API error occurred", http_res, http_res_text) + + raise models.SDKError("Unexpected response received", http_res) async def list_async( self, @@ -219,26 +209,15 @@ async def list_async( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.BatchJobsOut) + return unmarshal_json_response(models.BatchJobsOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) - - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("API error occurred", http_res, http_res_text) + + raise models.SDKError("Unexpected response received", http_res) def create( self, @@ -331,26 +310,15 @@ def create( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.BatchJobOut) + return unmarshal_json_response(models.BatchJobOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) - - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("API error occurred", http_res, http_res_text) + + raise models.SDKError("Unexpected response received", http_res) async def create_async( self, @@ -443,26 +411,15 @@ async def create_async( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.BatchJobOut) + return unmarshal_json_response(models.BatchJobOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) - - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("API error occurred", http_res, http_res_text) + + raise models.SDKError("Unexpected response received", http_res) def get( self, @@ -537,26 +494,15 @@ def get( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.BatchJobOut) + return unmarshal_json_response(models.BatchJobOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) - - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("API error occurred", http_res, http_res_text) + + raise models.SDKError("Unexpected response received", http_res) async def get_async( self, @@ -631,26 +577,15 @@ async def get_async( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.BatchJobOut) + return unmarshal_json_response(models.BatchJobOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) - - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("API error occurred", http_res, http_res_text) + + raise models.SDKError("Unexpected response received", http_res) def cancel( self, @@ -725,26 +660,15 @@ def cancel( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.BatchJobOut) + return unmarshal_json_response(models.BatchJobOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) - - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("API error occurred", http_res, http_res_text) + + raise models.SDKError("Unexpected response received", http_res) async def cancel_async( self, @@ -819,23 +743,12 @@ async def cancel_async( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.BatchJobOut) + return unmarshal_json_response(models.BatchJobOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) - - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("API error occurred", http_res, http_res_text) + + raise models.SDKError("Unexpected response received", http_res) diff --git a/src/mistralai/models/__init__.py b/src/mistralai/models/__init__.py index 9ca279f8..0298e73b 100644 --- a/src/mistralai/models/__init__.py +++ b/src/mistralai/models/__init__.py @@ -1,7 +1,10 @@ """Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.""" +from .mistralerror import MistralError from typing import TYPE_CHECKING from importlib import import_module +import builtins +import sys if TYPE_CHECKING: from .agent import ( @@ -713,6 +716,7 @@ from .modellist import Data, DataTypedDict, ModelList, ModelListTypedDict from .moderationobject import ModerationObject, ModerationObjectTypedDict from .moderationresponse import ModerationResponse, ModerationResponseTypedDict + from .no_response_error import NoResponseError from .ocrimageobject import OCRImageObject, OCRImageObjectTypedDict from .ocrpagedimensions import OCRPageDimensions, OCRPageDimensionsTypedDict from .ocrpageobject import OCRPageObject, OCRPageObjectTypedDict @@ -745,6 +749,7 @@ ResponseStartedEventType, ResponseStartedEventTypedDict, ) + from .responsevalidationerror import ResponseValidationError from .retrieve_model_v1_models_model_id_getop import ( RetrieveModelV1ModelsModelIDGetRequest, RetrieveModelV1ModelsModelIDGetRequestTypedDict, @@ -1391,6 +1396,7 @@ "MessagesTypedDict", "MetricOut", "MetricOutTypedDict", + "MistralError", "MistralPromptMode", "ModelCapabilities", "ModelCapabilitiesTypedDict", @@ -1406,6 +1412,7 @@ "ModerationObjectTypedDict", "ModerationResponse", "ModerationResponseTypedDict", + "NoResponseError", "OCRImageObject", "OCRImageObjectTypedDict", "OCRPageDimensions", @@ -1453,6 +1460,7 @@ "ResponseStartedEvent", "ResponseStartedEventType", "ResponseStartedEventTypedDict", + "ResponseValidationError", "RetrieveFileOut", "RetrieveFileOutTypedDict", "RetrieveModelV1ModelsModelIDGetRequest", @@ -2107,6 +2115,7 @@ "ModerationObjectTypedDict": ".moderationobject", "ModerationResponse": ".moderationresponse", "ModerationResponseTypedDict": ".moderationresponse", + "NoResponseError": ".no_response_error", "OCRImageObject": ".ocrimageobject", "OCRImageObjectTypedDict": ".ocrimageobject", "OCRPageDimensions": ".ocrpagedimensions", @@ -2144,6 +2153,7 @@ "ResponseStartedEvent": ".responsestartedevent", "ResponseStartedEventType": ".responsestartedevent", "ResponseStartedEventTypedDict": ".responsestartedevent", + "ResponseValidationError": ".responsevalidationerror", "RetrieveModelV1ModelsModelIDGetRequest": ".retrieve_model_v1_models_model_id_getop", "RetrieveModelV1ModelsModelIDGetRequestTypedDict": ".retrieve_model_v1_models_model_id_getop", "RetrieveModelV1ModelsModelIDGetResponseRetrieveModelV1ModelsModelIDGet": ".retrieve_model_v1_models_model_id_getop", @@ -2268,6 +2278,18 @@ } +def dynamic_import(modname, retries=3): + for attempt in range(retries): + try: + return import_module(modname, __package__) + except KeyError: + # Clear any half-initialized module and retry + sys.modules.pop(modname, None) + if attempt == retries - 1: + break + raise KeyError(f"Failed to import module '{modname}' after {retries} attempts") + + def __getattr__(attr_name: str) -> object: module_name = _dynamic_imports.get(attr_name) if module_name is None: @@ -2276,7 +2298,7 @@ def __getattr__(attr_name: str) -> object: ) try: - module = import_module(module_name, __package__) + module = dynamic_import(module_name) result = getattr(module, attr_name) return result except ImportError as e: @@ -2290,5 +2312,5 @@ def __getattr__(attr_name: str) -> object: def __dir__(): - lazy_attrs = list(_dynamic_imports.keys()) - return sorted(lazy_attrs) + lazy_attrs = builtins.list(_dynamic_imports.keys()) + return builtins.sorted(lazy_attrs) diff --git a/src/mistralai/models/agent.py b/src/mistralai/models/agent.py index b6bf17ab..ade2c123 100644 --- a/src/mistralai/models/agent.py +++ b/src/mistralai/models/agent.py @@ -42,7 +42,7 @@ ] -AgentObject = Literal["agent"] +AgentObject = Literal["agent",] class AgentTypedDict(TypedDict): diff --git a/src/mistralai/models/agentconversation.py b/src/mistralai/models/agentconversation.py index 42ab84f5..94c9bd76 100644 --- a/src/mistralai/models/agentconversation.py +++ b/src/mistralai/models/agentconversation.py @@ -8,7 +8,7 @@ from typing_extensions import NotRequired, TypedDict -AgentConversationObject = Literal["conversation"] +AgentConversationObject = Literal["conversation",] class AgentConversationTypedDict(TypedDict): diff --git a/src/mistralai/models/agenthandoffdoneevent.py b/src/mistralai/models/agenthandoffdoneevent.py index fa545a02..1cdbf456 100644 --- a/src/mistralai/models/agenthandoffdoneevent.py +++ b/src/mistralai/models/agenthandoffdoneevent.py @@ -7,7 +7,7 @@ from typing_extensions import NotRequired, TypedDict -AgentHandoffDoneEventType = Literal["agent.handoff.done"] +AgentHandoffDoneEventType = Literal["agent.handoff.done",] class AgentHandoffDoneEventTypedDict(TypedDict): diff --git a/src/mistralai/models/agenthandoffentry.py b/src/mistralai/models/agenthandoffentry.py index 44bfe0f2..66136256 100644 --- a/src/mistralai/models/agenthandoffentry.py +++ b/src/mistralai/models/agenthandoffentry.py @@ -8,9 +8,10 @@ from typing_extensions import NotRequired, TypedDict -AgentHandoffEntryObject = Literal["entry"] +AgentHandoffEntryObject = Literal["entry",] -AgentHandoffEntryType = Literal["agent.handoff"] + +AgentHandoffEntryType = Literal["agent.handoff",] class AgentHandoffEntryTypedDict(TypedDict): diff --git a/src/mistralai/models/agenthandoffstartedevent.py b/src/mistralai/models/agenthandoffstartedevent.py index 9033a0a9..11bfa918 100644 --- a/src/mistralai/models/agenthandoffstartedevent.py +++ b/src/mistralai/models/agenthandoffstartedevent.py @@ -7,7 +7,7 @@ from typing_extensions import NotRequired, TypedDict -AgentHandoffStartedEventType = Literal["agent.handoff.started"] +AgentHandoffStartedEventType = Literal["agent.handoff.started",] class AgentHandoffStartedEventTypedDict(TypedDict): diff --git a/src/mistralai/models/archiveftmodelout.py b/src/mistralai/models/archiveftmodelout.py index cff27c4e..0f753cfc 100644 --- a/src/mistralai/models/archiveftmodelout.py +++ b/src/mistralai/models/archiveftmodelout.py @@ -6,7 +6,7 @@ from typing_extensions import NotRequired, TypedDict -ArchiveFTModelOutObject = Literal["model"] +ArchiveFTModelOutObject = Literal["model",] class ArchiveFTModelOutTypedDict(TypedDict): diff --git a/src/mistralai/models/assistantmessage.py b/src/mistralai/models/assistantmessage.py index 18841a72..a38a10c4 100644 --- a/src/mistralai/models/assistantmessage.py +++ b/src/mistralai/models/assistantmessage.py @@ -19,7 +19,7 @@ ) -AssistantMessageRole = Literal["assistant"] +AssistantMessageRole = Literal["assistant",] class AssistantMessageTypedDict(TypedDict): diff --git a/src/mistralai/models/audiochunk.py b/src/mistralai/models/audiochunk.py index 2780570a..64fc43ff 100644 --- a/src/mistralai/models/audiochunk.py +++ b/src/mistralai/models/audiochunk.py @@ -6,7 +6,7 @@ from typing_extensions import NotRequired, TypedDict -AudioChunkType = Literal["input_audio"] +AudioChunkType = Literal["input_audio",] class AudioChunkTypedDict(TypedDict): diff --git a/src/mistralai/models/basemodelcard.py b/src/mistralai/models/basemodelcard.py index a4a061ff..706841b7 100644 --- a/src/mistralai/models/basemodelcard.py +++ b/src/mistralai/models/basemodelcard.py @@ -12,7 +12,7 @@ from typing_extensions import Annotated, NotRequired, TypedDict -BaseModelCardType = Literal["base"] +BaseModelCardType = Literal["base",] class BaseModelCardTypedDict(TypedDict): diff --git a/src/mistralai/models/batchjobout.py b/src/mistralai/models/batchjobout.py index 88304313..3d9f0dba 100644 --- a/src/mistralai/models/batchjobout.py +++ b/src/mistralai/models/batchjobout.py @@ -9,7 +9,7 @@ from typing_extensions import NotRequired, TypedDict -BatchJobOutObject = Literal["batch"] +BatchJobOutObject = Literal["batch",] class BatchJobOutTypedDict(TypedDict): diff --git a/src/mistralai/models/batchjobsout.py b/src/mistralai/models/batchjobsout.py index 8ce26f31..a1eba5db 100644 --- a/src/mistralai/models/batchjobsout.py +++ b/src/mistralai/models/batchjobsout.py @@ -7,7 +7,7 @@ from typing_extensions import NotRequired, TypedDict -BatchJobsOutObject = Literal["list"] +BatchJobsOutObject = Literal["list",] class BatchJobsOutTypedDict(TypedDict): diff --git a/src/mistralai/models/chatcompletionchoice.py b/src/mistralai/models/chatcompletionchoice.py index f4f37fb4..b26ce1ec 100644 --- a/src/mistralai/models/chatcompletionchoice.py +++ b/src/mistralai/models/chatcompletionchoice.py @@ -10,7 +10,14 @@ FinishReason = Union[ - Literal["stop", "length", "model_length", "error", "tool_calls"], UnrecognizedStr + Literal[ + "stop", + "length", + "model_length", + "error", + "tool_calls", + ], + UnrecognizedStr, ] diff --git a/src/mistralai/models/classifierdetailedjobout.py b/src/mistralai/models/classifierdetailedjobout.py index da5bd281..701aee6e 100644 --- a/src/mistralai/models/classifierdetailedjobout.py +++ b/src/mistralai/models/classifierdetailedjobout.py @@ -29,7 +29,9 @@ "CANCELLATION_REQUESTED", ] -ClassifierDetailedJobOutObject = Literal["job"] + +ClassifierDetailedJobOutObject = Literal["job",] + ClassifierDetailedJobOutIntegrationsTypedDict = WandbIntegrationOutTypedDict @@ -37,7 +39,7 @@ ClassifierDetailedJobOutIntegrations = WandbIntegrationOut -ClassifierDetailedJobOutJobType = Literal["classifier"] +ClassifierDetailedJobOutJobType = Literal["classifier",] class ClassifierDetailedJobOutTypedDict(TypedDict): diff --git a/src/mistralai/models/classifierftmodelout.py b/src/mistralai/models/classifierftmodelout.py index 56ffe96d..d2a31fae 100644 --- a/src/mistralai/models/classifierftmodelout.py +++ b/src/mistralai/models/classifierftmodelout.py @@ -12,9 +12,10 @@ from typing_extensions import NotRequired, TypedDict -ClassifierFTModelOutObject = Literal["model"] +ClassifierFTModelOutObject = Literal["model",] -ClassifierFTModelOutModelType = Literal["classifier"] + +ClassifierFTModelOutModelType = Literal["classifier",] class ClassifierFTModelOutTypedDict(TypedDict): diff --git a/src/mistralai/models/classifierjobout.py b/src/mistralai/models/classifierjobout.py index c8df6da3..a2f7cc08 100644 --- a/src/mistralai/models/classifierjobout.py +++ b/src/mistralai/models/classifierjobout.py @@ -27,16 +27,18 @@ ] r"""The current status of the fine-tuning job.""" -ClassifierJobOutObject = Literal["job"] + +ClassifierJobOutObject = Literal["job",] r"""The object type of the fine-tuning job.""" + ClassifierJobOutIntegrationsTypedDict = WandbIntegrationOutTypedDict ClassifierJobOutIntegrations = WandbIntegrationOut -ClassifierJobOutJobType = Literal["classifier"] +ClassifierJobOutJobType = Literal["classifier",] r"""The type of job (`FT` for fine-tuning).""" diff --git a/src/mistralai/models/codeinterpretertool.py b/src/mistralai/models/codeinterpretertool.py index b0fc4d20..48b74ee8 100644 --- a/src/mistralai/models/codeinterpretertool.py +++ b/src/mistralai/models/codeinterpretertool.py @@ -6,7 +6,7 @@ from typing_extensions import NotRequired, TypedDict -CodeInterpreterToolType = Literal["code_interpreter"] +CodeInterpreterToolType = Literal["code_interpreter",] class CodeInterpreterToolTypedDict(TypedDict): diff --git a/src/mistralai/models/completiondetailedjobout.py b/src/mistralai/models/completiondetailedjobout.py index 8fb1b62a..df41bc2a 100644 --- a/src/mistralai/models/completiondetailedjobout.py +++ b/src/mistralai/models/completiondetailedjobout.py @@ -29,7 +29,9 @@ "CANCELLATION_REQUESTED", ] -CompletionDetailedJobOutObject = Literal["job"] + +CompletionDetailedJobOutObject = Literal["job",] + CompletionDetailedJobOutIntegrationsTypedDict = WandbIntegrationOutTypedDict @@ -37,7 +39,8 @@ CompletionDetailedJobOutIntegrations = WandbIntegrationOut -CompletionDetailedJobOutJobType = Literal["completion"] +CompletionDetailedJobOutJobType = Literal["completion",] + CompletionDetailedJobOutRepositoriesTypedDict = GithubRepositoryOutTypedDict diff --git a/src/mistralai/models/completionftmodelout.py b/src/mistralai/models/completionftmodelout.py index ab71168b..7b6520de 100644 --- a/src/mistralai/models/completionftmodelout.py +++ b/src/mistralai/models/completionftmodelout.py @@ -11,9 +11,10 @@ from typing_extensions import NotRequired, TypedDict -CompletionFTModelOutObject = Literal["model"] +CompletionFTModelOutObject = Literal["model",] -ModelType = Literal["completion"] + +ModelType = Literal["completion",] class CompletionFTModelOutTypedDict(TypedDict): diff --git a/src/mistralai/models/completionjobout.py b/src/mistralai/models/completionjobout.py index bed67b50..70995d2a 100644 --- a/src/mistralai/models/completionjobout.py +++ b/src/mistralai/models/completionjobout.py @@ -28,18 +28,21 @@ ] r"""The current status of the fine-tuning job.""" -CompletionJobOutObject = Literal["job"] + +CompletionJobOutObject = Literal["job",] r"""The object type of the fine-tuning job.""" + IntegrationsTypedDict = WandbIntegrationOutTypedDict Integrations = WandbIntegrationOut -JobType = Literal["completion"] +JobType = Literal["completion",] r"""The type of job (`FT` for fine-tuning).""" + RepositoriesTypedDict = GithubRepositoryOutTypedDict diff --git a/src/mistralai/models/completionresponsestreamchoice.py b/src/mistralai/models/completionresponsestreamchoice.py index 2426148c..c9657515 100644 --- a/src/mistralai/models/completionresponsestreamchoice.py +++ b/src/mistralai/models/completionresponsestreamchoice.py @@ -11,7 +11,13 @@ CompletionResponseStreamChoiceFinishReason = Union[ - Literal["stop", "length", "error", "tool_calls"], UnrecognizedStr + Literal[ + "stop", + "length", + "error", + "tool_calls", + ], + UnrecognizedStr, ] diff --git a/src/mistralai/models/conversationappendrequest.py b/src/mistralai/models/conversationappendrequest.py index ecc47e45..15cbc687 100644 --- a/src/mistralai/models/conversationappendrequest.py +++ b/src/mistralai/models/conversationappendrequest.py @@ -8,7 +8,10 @@ from typing_extensions import NotRequired, TypedDict -ConversationAppendRequestHandoffExecution = Literal["client", "server"] +ConversationAppendRequestHandoffExecution = Literal[ + "client", + "server", +] class ConversationAppendRequestTypedDict(TypedDict): diff --git a/src/mistralai/models/conversationappendstreamrequest.py b/src/mistralai/models/conversationappendstreamrequest.py index 25ffe5fb..8cecf89d 100644 --- a/src/mistralai/models/conversationappendstreamrequest.py +++ b/src/mistralai/models/conversationappendstreamrequest.py @@ -8,7 +8,10 @@ from typing_extensions import NotRequired, TypedDict -ConversationAppendStreamRequestHandoffExecution = Literal["client", "server"] +ConversationAppendStreamRequestHandoffExecution = Literal[ + "client", + "server", +] class ConversationAppendStreamRequestTypedDict(TypedDict): diff --git a/src/mistralai/models/conversationhistory.py b/src/mistralai/models/conversationhistory.py index 472915fe..d5206a57 100644 --- a/src/mistralai/models/conversationhistory.py +++ b/src/mistralai/models/conversationhistory.py @@ -12,7 +12,8 @@ from typing_extensions import NotRequired, TypeAliasType, TypedDict -ConversationHistoryObject = Literal["conversation.history"] +ConversationHistoryObject = Literal["conversation.history",] + EntriesTypedDict = TypeAliasType( "EntriesTypedDict", diff --git a/src/mistralai/models/conversationmessages.py b/src/mistralai/models/conversationmessages.py index 9027045b..32ca9c20 100644 --- a/src/mistralai/models/conversationmessages.py +++ b/src/mistralai/models/conversationmessages.py @@ -7,7 +7,7 @@ from typing_extensions import NotRequired, TypedDict -ConversationMessagesObject = Literal["conversation.messages"] +ConversationMessagesObject = Literal["conversation.messages",] class ConversationMessagesTypedDict(TypedDict): diff --git a/src/mistralai/models/conversationrequest.py b/src/mistralai/models/conversationrequest.py index 0fcca512..798019dc 100644 --- a/src/mistralai/models/conversationrequest.py +++ b/src/mistralai/models/conversationrequest.py @@ -16,7 +16,11 @@ from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict -HandoffExecution = Literal["client", "server"] +HandoffExecution = Literal[ + "client", + "server", +] + ToolsTypedDict = TypeAliasType( "ToolsTypedDict", diff --git a/src/mistralai/models/conversationresponse.py b/src/mistralai/models/conversationresponse.py index 61de8565..ff318e35 100644 --- a/src/mistralai/models/conversationresponse.py +++ b/src/mistralai/models/conversationresponse.py @@ -11,7 +11,8 @@ from typing_extensions import NotRequired, TypeAliasType, TypedDict -ConversationResponseObject = Literal["conversation.response"] +ConversationResponseObject = Literal["conversation.response",] + OutputsTypedDict = TypeAliasType( "OutputsTypedDict", diff --git a/src/mistralai/models/conversationrestartrequest.py b/src/mistralai/models/conversationrestartrequest.py index 58376140..47d368b9 100644 --- a/src/mistralai/models/conversationrestartrequest.py +++ b/src/mistralai/models/conversationrestartrequest.py @@ -8,7 +8,10 @@ from typing_extensions import NotRequired, TypedDict -ConversationRestartRequestHandoffExecution = Literal["client", "server"] +ConversationRestartRequestHandoffExecution = Literal[ + "client", + "server", +] class ConversationRestartRequestTypedDict(TypedDict): diff --git a/src/mistralai/models/conversationrestartstreamrequest.py b/src/mistralai/models/conversationrestartstreamrequest.py index f213aea3..92542b9b 100644 --- a/src/mistralai/models/conversationrestartstreamrequest.py +++ b/src/mistralai/models/conversationrestartstreamrequest.py @@ -8,7 +8,10 @@ from typing_extensions import NotRequired, TypedDict -ConversationRestartStreamRequestHandoffExecution = Literal["client", "server"] +ConversationRestartStreamRequestHandoffExecution = Literal[ + "client", + "server", +] class ConversationRestartStreamRequestTypedDict(TypedDict): diff --git a/src/mistralai/models/conversationstreamrequest.py b/src/mistralai/models/conversationstreamrequest.py index 0880727e..e02cdbc5 100644 --- a/src/mistralai/models/conversationstreamrequest.py +++ b/src/mistralai/models/conversationstreamrequest.py @@ -16,7 +16,11 @@ from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict -ConversationStreamRequestHandoffExecution = Literal["client", "server"] +ConversationStreamRequestHandoffExecution = Literal[ + "client", + "server", +] + ConversationStreamRequestToolsTypedDict = TypeAliasType( "ConversationStreamRequestToolsTypedDict", diff --git a/src/mistralai/models/documentlibrarytool.py b/src/mistralai/models/documentlibrarytool.py index f36de710..8d4c122b 100644 --- a/src/mistralai/models/documentlibrarytool.py +++ b/src/mistralai/models/documentlibrarytool.py @@ -6,7 +6,7 @@ from typing_extensions import NotRequired, TypedDict -DocumentLibraryToolType = Literal["document_library"] +DocumentLibraryToolType = Literal["document_library",] class DocumentLibraryToolTypedDict(TypedDict): diff --git a/src/mistralai/models/documenturlchunk.py b/src/mistralai/models/documenturlchunk.py index 33f29ba8..6d0b1dc6 100644 --- a/src/mistralai/models/documenturlchunk.py +++ b/src/mistralai/models/documenturlchunk.py @@ -7,7 +7,7 @@ from typing_extensions import NotRequired, TypedDict -DocumentURLChunkType = Literal["document_url"] +DocumentURLChunkType = Literal["document_url",] class DocumentURLChunkTypedDict(TypedDict): diff --git a/src/mistralai/models/embeddingdtype.py b/src/mistralai/models/embeddingdtype.py index 4f3c41bd..26eee779 100644 --- a/src/mistralai/models/embeddingdtype.py +++ b/src/mistralai/models/embeddingdtype.py @@ -4,4 +4,10 @@ from typing import Literal -EmbeddingDtype = Literal["float", "int8", "uint8", "binary", "ubinary"] +EmbeddingDtype = Literal[ + "float", + "int8", + "uint8", + "binary", + "ubinary", +] diff --git a/src/mistralai/models/encodingformat.py b/src/mistralai/models/encodingformat.py index 6c28a15a..be6c1a14 100644 --- a/src/mistralai/models/encodingformat.py +++ b/src/mistralai/models/encodingformat.py @@ -4,4 +4,7 @@ from typing import Literal -EncodingFormat = Literal["float", "base64"] +EncodingFormat = Literal[ + "float", + "base64", +] diff --git a/src/mistralai/models/entitytype.py b/src/mistralai/models/entitytype.py index b5149c5f..8d2d4bbe 100644 --- a/src/mistralai/models/entitytype.py +++ b/src/mistralai/models/entitytype.py @@ -5,5 +5,12 @@ from typing import Literal, Union -EntityType = Union[Literal["User", "Workspace", "Org"], UnrecognizedStr] +EntityType = Union[ + Literal[ + "User", + "Workspace", + "Org", + ], + UnrecognizedStr, +] r"""The type of entity, used to share a library.""" diff --git a/src/mistralai/models/filepurpose.py b/src/mistralai/models/filepurpose.py index 8599192b..b109b350 100644 --- a/src/mistralai/models/filepurpose.py +++ b/src/mistralai/models/filepurpose.py @@ -5,4 +5,11 @@ from typing import Literal, Union -FilePurpose = Union[Literal["fine-tune", "batch", "ocr"], UnrecognizedStr] +FilePurpose = Union[ + Literal[ + "fine-tune", + "batch", + "ocr", + ], + UnrecognizedStr, +] diff --git a/src/mistralai/models/finetuneablemodeltype.py b/src/mistralai/models/finetuneablemodeltype.py index 3507dc91..f5b8b2ed 100644 --- a/src/mistralai/models/finetuneablemodeltype.py +++ b/src/mistralai/models/finetuneablemodeltype.py @@ -4,4 +4,7 @@ from typing import Literal -FineTuneableModelType = Literal["completion", "classifier"] +FineTuneableModelType = Literal[ + "completion", + "classifier", +] diff --git a/src/mistralai/models/ftclassifierlossfunction.py b/src/mistralai/models/ftclassifierlossfunction.py index df2d19ff..c4ef66e0 100644 --- a/src/mistralai/models/ftclassifierlossfunction.py +++ b/src/mistralai/models/ftclassifierlossfunction.py @@ -4,4 +4,7 @@ from typing import Literal -FTClassifierLossFunction = Literal["single_class", "multi_class"] +FTClassifierLossFunction = Literal[ + "single_class", + "multi_class", +] diff --git a/src/mistralai/models/ftmodelcard.py b/src/mistralai/models/ftmodelcard.py index 7159ce00..1c3bd04d 100644 --- a/src/mistralai/models/ftmodelcard.py +++ b/src/mistralai/models/ftmodelcard.py @@ -12,7 +12,7 @@ from typing_extensions import Annotated, NotRequired, TypedDict -FTModelCardType = Literal["fine-tuned"] +FTModelCardType = Literal["fine-tuned",] class FTModelCardTypedDict(TypedDict): diff --git a/src/mistralai/models/functioncallentry.py b/src/mistralai/models/functioncallentry.py index 1e47fda9..4ea62c4f 100644 --- a/src/mistralai/models/functioncallentry.py +++ b/src/mistralai/models/functioncallentry.py @@ -12,9 +12,10 @@ from typing_extensions import NotRequired, TypedDict -FunctionCallEntryObject = Literal["entry"] +FunctionCallEntryObject = Literal["entry",] -FunctionCallEntryType = Literal["function.call"] + +FunctionCallEntryType = Literal["function.call",] class FunctionCallEntryTypedDict(TypedDict): diff --git a/src/mistralai/models/functioncallevent.py b/src/mistralai/models/functioncallevent.py index 90b4b226..e3992cf1 100644 --- a/src/mistralai/models/functioncallevent.py +++ b/src/mistralai/models/functioncallevent.py @@ -7,7 +7,7 @@ from typing_extensions import NotRequired, TypedDict -FunctionCallEventType = Literal["function.call.delta"] +FunctionCallEventType = Literal["function.call.delta",] class FunctionCallEventTypedDict(TypedDict): diff --git a/src/mistralai/models/functionresultentry.py b/src/mistralai/models/functionresultentry.py index f09e11ae..1c61395a 100644 --- a/src/mistralai/models/functionresultentry.py +++ b/src/mistralai/models/functionresultentry.py @@ -8,9 +8,10 @@ from typing_extensions import NotRequired, TypedDict -FunctionResultEntryObject = Literal["entry"] +FunctionResultEntryObject = Literal["entry",] -FunctionResultEntryType = Literal["function.result"] + +FunctionResultEntryType = Literal["function.result",] class FunctionResultEntryTypedDict(TypedDict): diff --git a/src/mistralai/models/functiontool.py b/src/mistralai/models/functiontool.py index 7ce5c464..009fe280 100644 --- a/src/mistralai/models/functiontool.py +++ b/src/mistralai/models/functiontool.py @@ -7,7 +7,7 @@ from typing_extensions import NotRequired, TypedDict -FunctionToolType = Literal["function"] +FunctionToolType = Literal["function",] class FunctionToolTypedDict(TypedDict): diff --git a/src/mistralai/models/githubrepositoryin.py b/src/mistralai/models/githubrepositoryin.py index 801c0540..b16ce0d2 100644 --- a/src/mistralai/models/githubrepositoryin.py +++ b/src/mistralai/models/githubrepositoryin.py @@ -7,7 +7,7 @@ from typing_extensions import NotRequired, TypedDict -GithubRepositoryInType = Literal["github"] +GithubRepositoryInType = Literal["github",] class GithubRepositoryInTypedDict(TypedDict): diff --git a/src/mistralai/models/githubrepositoryout.py b/src/mistralai/models/githubrepositoryout.py index 0d74c17a..372477c1 100644 --- a/src/mistralai/models/githubrepositoryout.py +++ b/src/mistralai/models/githubrepositoryout.py @@ -7,7 +7,7 @@ from typing_extensions import NotRequired, TypedDict -GithubRepositoryOutType = Literal["github"] +GithubRepositoryOutType = Literal["github",] class GithubRepositoryOutTypedDict(TypedDict): diff --git a/src/mistralai/models/httpvalidationerror.py b/src/mistralai/models/httpvalidationerror.py index 37f2dd76..18618066 100644 --- a/src/mistralai/models/httpvalidationerror.py +++ b/src/mistralai/models/httpvalidationerror.py @@ -2,7 +2,9 @@ from __future__ import annotations from .validationerror import ValidationError -from mistralai import utils +from dataclasses import dataclass, field +import httpx +from mistralai.models import MistralError from mistralai.types import BaseModel from typing import List, Optional @@ -11,11 +13,16 @@ class HTTPValidationErrorData(BaseModel): detail: Optional[List[ValidationError]] = None -class HTTPValidationError(Exception): - data: HTTPValidationErrorData +@dataclass(frozen=True) +class HTTPValidationError(MistralError): + data: HTTPValidationErrorData = field(hash=False) - def __init__(self, data: HTTPValidationErrorData): - self.data = data - - def __str__(self) -> str: - return utils.marshal_json(self.data, HTTPValidationErrorData) + def __init__( + self, + data: HTTPValidationErrorData, + raw_response: httpx.Response, + body: Optional[str] = None, + ): + message = body or raw_response.text + super().__init__(message, raw_response, body) + object.__setattr__(self, "data", data) diff --git a/src/mistralai/models/imagegenerationtool.py b/src/mistralai/models/imagegenerationtool.py index 27bb2d12..a92335db 100644 --- a/src/mistralai/models/imagegenerationtool.py +++ b/src/mistralai/models/imagegenerationtool.py @@ -6,7 +6,7 @@ from typing_extensions import NotRequired, TypedDict -ImageGenerationToolType = Literal["image_generation"] +ImageGenerationToolType = Literal["image_generation",] class ImageGenerationToolTypedDict(TypedDict): diff --git a/src/mistralai/models/imageurlchunk.py b/src/mistralai/models/imageurlchunk.py index 498690f5..8e8aac42 100644 --- a/src/mistralai/models/imageurlchunk.py +++ b/src/mistralai/models/imageurlchunk.py @@ -15,7 +15,7 @@ ImageURLChunkImageURL = TypeAliasType("ImageURLChunkImageURL", Union[ImageURL, str]) -ImageURLChunkType = Literal["image_url"] +ImageURLChunkType = Literal["image_url",] class ImageURLChunkTypedDict(TypedDict): diff --git a/src/mistralai/models/jobsout.py b/src/mistralai/models/jobsout.py index abdf18fd..680b1d58 100644 --- a/src/mistralai/models/jobsout.py +++ b/src/mistralai/models/jobsout.py @@ -24,7 +24,7 @@ ] -JobsOutObject = Literal["list"] +JobsOutObject = Literal["list",] class JobsOutTypedDict(TypedDict): diff --git a/src/mistralai/models/legacyjobmetadataout.py b/src/mistralai/models/legacyjobmetadataout.py index d878173b..49951219 100644 --- a/src/mistralai/models/legacyjobmetadataout.py +++ b/src/mistralai/models/legacyjobmetadataout.py @@ -7,7 +7,7 @@ from typing_extensions import NotRequired, TypedDict -LegacyJobMetadataOutObject = Literal["job.metadata"] +LegacyJobMetadataOutObject = Literal["job.metadata",] class LegacyJobMetadataOutTypedDict(TypedDict): diff --git a/src/mistralai/models/messageinputentry.py b/src/mistralai/models/messageinputentry.py index c14ad5ae..edf05631 100644 --- a/src/mistralai/models/messageinputentry.py +++ b/src/mistralai/models/messageinputentry.py @@ -12,11 +12,17 @@ from typing_extensions import NotRequired, TypeAliasType, TypedDict -Object = Literal["entry"] +Object = Literal["entry",] -MessageInputEntryType = Literal["message.input"] -MessageInputEntryRole = Literal["assistant", "user"] +MessageInputEntryType = Literal["message.input",] + + +MessageInputEntryRole = Literal[ + "assistant", + "user", +] + MessageInputEntryContentTypedDict = TypeAliasType( "MessageInputEntryContentTypedDict", diff --git a/src/mistralai/models/messageoutputentry.py b/src/mistralai/models/messageoutputentry.py index 1c2e4107..0e2df81e 100644 --- a/src/mistralai/models/messageoutputentry.py +++ b/src/mistralai/models/messageoutputentry.py @@ -12,11 +12,14 @@ from typing_extensions import NotRequired, TypeAliasType, TypedDict -MessageOutputEntryObject = Literal["entry"] +MessageOutputEntryObject = Literal["entry",] -MessageOutputEntryType = Literal["message.output"] -MessageOutputEntryRole = Literal["assistant"] +MessageOutputEntryType = Literal["message.output",] + + +MessageOutputEntryRole = Literal["assistant",] + MessageOutputEntryContentTypedDict = TypeAliasType( "MessageOutputEntryContentTypedDict", diff --git a/src/mistralai/models/messageoutputevent.py b/src/mistralai/models/messageoutputevent.py index 474cb081..751767a3 100644 --- a/src/mistralai/models/messageoutputevent.py +++ b/src/mistralai/models/messageoutputevent.py @@ -9,9 +9,11 @@ from typing_extensions import NotRequired, TypeAliasType, TypedDict -MessageOutputEventType = Literal["message.output.delta"] +MessageOutputEventType = Literal["message.output.delta",] + + +MessageOutputEventRole = Literal["assistant",] -MessageOutputEventRole = Literal["assistant"] MessageOutputEventContentTypedDict = TypeAliasType( "MessageOutputEventContentTypedDict", Union[str, OutputContentChunksTypedDict] diff --git a/src/mistralai/models/mistralerror.py b/src/mistralai/models/mistralerror.py new file mode 100644 index 00000000..9cc07279 --- /dev/null +++ b/src/mistralai/models/mistralerror.py @@ -0,0 +1,30 @@ +"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.""" + +import httpx +from typing import Optional +from dataclasses import dataclass, field + + +@dataclass(frozen=True) +class MistralError(Exception): + """The base class for all HTTP error responses.""" + + message: str + status_code: int + body: str + headers: httpx.Headers = field(hash=False) + raw_response: httpx.Response = field(hash=False) + + def __init__( + self, message: str, raw_response: httpx.Response, body: Optional[str] = None + ): + object.__setattr__(self, "message", message) + object.__setattr__(self, "status_code", raw_response.status_code) + object.__setattr__( + self, "body", body if body is not None else raw_response.text + ) + object.__setattr__(self, "headers", raw_response.headers) + object.__setattr__(self, "raw_response", raw_response) + + def __str__(self): + return self.message diff --git a/src/mistralai/models/mistralpromptmode.py b/src/mistralai/models/mistralpromptmode.py index 0ffd6787..ee82fb6d 100644 --- a/src/mistralai/models/mistralpromptmode.py +++ b/src/mistralai/models/mistralpromptmode.py @@ -5,4 +5,4 @@ from typing import Literal, Union -MistralPromptMode = Union[Literal["reasoning"], UnrecognizedStr] +MistralPromptMode = Union[Literal["reasoning",], UnrecognizedStr] diff --git a/src/mistralai/models/modelconversation.py b/src/mistralai/models/modelconversation.py index 4ced79ea..af591401 100644 --- a/src/mistralai/models/modelconversation.py +++ b/src/mistralai/models/modelconversation.py @@ -42,7 +42,7 @@ ] -ModelConversationObject = Literal["conversation"] +ModelConversationObject = Literal["conversation",] class ModelConversationTypedDict(TypedDict): diff --git a/src/mistralai/models/no_response_error.py b/src/mistralai/models/no_response_error.py new file mode 100644 index 00000000..b710ea2b --- /dev/null +++ b/src/mistralai/models/no_response_error.py @@ -0,0 +1,17 @@ +"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.""" + +from dataclasses import dataclass + + +@dataclass(frozen=True) +class NoResponseError(Exception): + """Error raised when no HTTP response is received from the server.""" + + message: str + + def __init__(self, message: str = "No response received"): + object.__setattr__(self, "message", message) + super().__init__(message) + + def __str__(self): + return self.message diff --git a/src/mistralai/models/referencechunk.py b/src/mistralai/models/referencechunk.py index 4a5503f2..1864ac79 100644 --- a/src/mistralai/models/referencechunk.py +++ b/src/mistralai/models/referencechunk.py @@ -6,7 +6,7 @@ from typing_extensions import NotRequired, TypedDict -ReferenceChunkType = Literal["reference"] +ReferenceChunkType = Literal["reference",] class ReferenceChunkTypedDict(TypedDict): diff --git a/src/mistralai/models/responsedoneevent.py b/src/mistralai/models/responsedoneevent.py index 296cb430..5a3a3dfb 100644 --- a/src/mistralai/models/responsedoneevent.py +++ b/src/mistralai/models/responsedoneevent.py @@ -8,7 +8,7 @@ from typing_extensions import NotRequired, TypedDict -ResponseDoneEventType = Literal["conversation.response.done"] +ResponseDoneEventType = Literal["conversation.response.done",] class ResponseDoneEventTypedDict(TypedDict): diff --git a/src/mistralai/models/responseerrorevent.py b/src/mistralai/models/responseerrorevent.py index e4190d17..6cb1b268 100644 --- a/src/mistralai/models/responseerrorevent.py +++ b/src/mistralai/models/responseerrorevent.py @@ -7,7 +7,7 @@ from typing_extensions import NotRequired, TypedDict -ResponseErrorEventType = Literal["conversation.response.error"] +ResponseErrorEventType = Literal["conversation.response.error",] class ResponseErrorEventTypedDict(TypedDict): diff --git a/src/mistralai/models/responseformats.py b/src/mistralai/models/responseformats.py index 08c39951..5948d9f4 100644 --- a/src/mistralai/models/responseformats.py +++ b/src/mistralai/models/responseformats.py @@ -4,5 +4,9 @@ from typing import Literal -ResponseFormats = Literal["text", "json_object", "json_schema"] +ResponseFormats = Literal[ + "text", + "json_object", + "json_schema", +] r"""An object specifying the format that the model must output. Setting to `{ \"type\": \"json_object\" }` enables JSON mode, which guarantees the message the model generates is in JSON. When using JSON mode you MUST also instruct the model to produce JSON yourself with a system or a user message.""" diff --git a/src/mistralai/models/responsestartedevent.py b/src/mistralai/models/responsestartedevent.py index 6acb483e..d14d45ef 100644 --- a/src/mistralai/models/responsestartedevent.py +++ b/src/mistralai/models/responsestartedevent.py @@ -7,7 +7,7 @@ from typing_extensions import NotRequired, TypedDict -ResponseStartedEventType = Literal["conversation.response.started"] +ResponseStartedEventType = Literal["conversation.response.started",] class ResponseStartedEventTypedDict(TypedDict): diff --git a/src/mistralai/models/responsevalidationerror.py b/src/mistralai/models/responsevalidationerror.py new file mode 100644 index 00000000..4cc0f5dd --- /dev/null +++ b/src/mistralai/models/responsevalidationerror.py @@ -0,0 +1,27 @@ +"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.""" + +import httpx +from typing import Optional +from dataclasses import dataclass + +from mistralai.models import MistralError + + +@dataclass(frozen=True) +class ResponseValidationError(MistralError): + """Error raised when there is a type mismatch between the response data and the expected Pydantic model.""" + + def __init__( + self, + message: str, + raw_response: httpx.Response, + cause: Exception, + body: Optional[str] = None, + ): + message = f"{message}: {cause}" + super().__init__(message, raw_response, body) + + @property + def cause(self): + """Normally the Pydantic ValidationError""" + return self.__cause__ diff --git a/src/mistralai/models/sampletype.py b/src/mistralai/models/sampletype.py index adc90ec7..efb43e9b 100644 --- a/src/mistralai/models/sampletype.py +++ b/src/mistralai/models/sampletype.py @@ -6,6 +6,12 @@ SampleType = Union[ - Literal["pretrain", "instruct", "batch_request", "batch_result", "batch_error"], + Literal[ + "pretrain", + "instruct", + "batch_request", + "batch_result", + "batch_error", + ], UnrecognizedStr, ] diff --git a/src/mistralai/models/sdkerror.py b/src/mistralai/models/sdkerror.py index 03216cbf..2fdbc395 100644 --- a/src/mistralai/models/sdkerror.py +++ b/src/mistralai/models/sdkerror.py @@ -1,22 +1,40 @@ """Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.""" -from dataclasses import dataclass -from typing import Optional import httpx +from typing import Optional +from dataclasses import dataclass + +from mistralai.models import MistralError + +MAX_MESSAGE_LEN = 10_000 + + +@dataclass(frozen=True) +class SDKError(MistralError): + """The fallback error class if no more specific error class is matched.""" + + def __init__( + self, message: str, raw_response: httpx.Response, body: Optional[str] = None + ): + body_display = body or raw_response.text or '""' + if message: + message += ": " + message += f"Status {raw_response.status_code}" -@dataclass -class SDKError(Exception): - """Represents an error returned by the API.""" + headers = raw_response.headers + content_type = headers.get("content-type", '""') + if content_type != "application/json": + if " " in content_type: + content_type = f'"{content_type}"' + message += f" Content-Type {content_type}" - message: str - status_code: int = -1 - body: str = "" - raw_response: Optional[httpx.Response] = None + if len(body_display) > MAX_MESSAGE_LEN: + truncated = body_display[:MAX_MESSAGE_LEN] + remaining = len(body_display) - MAX_MESSAGE_LEN + body_display = f"{truncated}...and {remaining} more chars" - def __str__(self): - body = "" - if len(self.body) > 0: - body = f"\n{self.body}" + message += f". Body: {body_display}" + message = message.strip() - return f"{self.message}: Status {self.status_code}{body}" + super().__init__(message, raw_response, body) diff --git a/src/mistralai/models/shareenum.py b/src/mistralai/models/shareenum.py index c2945514..634ba4b7 100644 --- a/src/mistralai/models/shareenum.py +++ b/src/mistralai/models/shareenum.py @@ -5,4 +5,10 @@ from typing import Literal, Union -ShareEnum = Union[Literal["Viewer", "Editor"], UnrecognizedStr] +ShareEnum = Union[ + Literal[ + "Viewer", + "Editor", + ], + UnrecognizedStr, +] diff --git a/src/mistralai/models/source.py b/src/mistralai/models/source.py index c21550f2..cc3abce2 100644 --- a/src/mistralai/models/source.py +++ b/src/mistralai/models/source.py @@ -5,4 +5,11 @@ from typing import Literal, Union -Source = Union[Literal["upload", "repository", "mistral"], UnrecognizedStr] +Source = Union[ + Literal[ + "upload", + "repository", + "mistral", + ], + UnrecognizedStr, +] diff --git a/src/mistralai/models/systemmessage.py b/src/mistralai/models/systemmessage.py index 25b51f95..2b34607b 100644 --- a/src/mistralai/models/systemmessage.py +++ b/src/mistralai/models/systemmessage.py @@ -21,7 +21,7 @@ ) -Role = Literal["system"] +Role = Literal["system",] class SystemMessageTypedDict(TypedDict): diff --git a/src/mistralai/models/textchunk.py b/src/mistralai/models/textchunk.py index 02b115f6..6052686e 100644 --- a/src/mistralai/models/textchunk.py +++ b/src/mistralai/models/textchunk.py @@ -6,7 +6,7 @@ from typing_extensions import NotRequired, TypedDict -TextChunkType = Literal["text"] +TextChunkType = Literal["text",] class TextChunkTypedDict(TypedDict): diff --git a/src/mistralai/models/thinkchunk.py b/src/mistralai/models/thinkchunk.py index 24b466f9..627ae488 100644 --- a/src/mistralai/models/thinkchunk.py +++ b/src/mistralai/models/thinkchunk.py @@ -16,7 +16,7 @@ Thinking = TypeAliasType("Thinking", Union[ReferenceChunk, TextChunk]) -ThinkChunkType = Literal["thinking"] +ThinkChunkType = Literal["thinking",] class ThinkChunkTypedDict(TypedDict): diff --git a/src/mistralai/models/timestampgranularity.py b/src/mistralai/models/timestampgranularity.py index dd1b6446..02816df6 100644 --- a/src/mistralai/models/timestampgranularity.py +++ b/src/mistralai/models/timestampgranularity.py @@ -4,4 +4,4 @@ from typing import Literal -TimestampGranularity = Literal["segment"] +TimestampGranularity = Literal["segment",] diff --git a/src/mistralai/models/toolchoiceenum.py b/src/mistralai/models/toolchoiceenum.py index 8e6a6ad8..01f6f677 100644 --- a/src/mistralai/models/toolchoiceenum.py +++ b/src/mistralai/models/toolchoiceenum.py @@ -4,4 +4,9 @@ from typing import Literal -ToolChoiceEnum = Literal["auto", "none", "any", "required"] +ToolChoiceEnum = Literal[ + "auto", + "none", + "any", + "required", +] diff --git a/src/mistralai/models/toolexecutiondeltaevent.py b/src/mistralai/models/toolexecutiondeltaevent.py index 99b97e68..b2fa97ef 100644 --- a/src/mistralai/models/toolexecutiondeltaevent.py +++ b/src/mistralai/models/toolexecutiondeltaevent.py @@ -8,7 +8,7 @@ from typing_extensions import NotRequired, TypedDict -ToolExecutionDeltaEventType = Literal["tool.execution.delta"] +ToolExecutionDeltaEventType = Literal["tool.execution.delta",] class ToolExecutionDeltaEventTypedDict(TypedDict): diff --git a/src/mistralai/models/toolexecutiondoneevent.py b/src/mistralai/models/toolexecutiondoneevent.py index c73d943a..3e7aa55e 100644 --- a/src/mistralai/models/toolexecutiondoneevent.py +++ b/src/mistralai/models/toolexecutiondoneevent.py @@ -8,7 +8,7 @@ from typing_extensions import NotRequired, TypedDict -ToolExecutionDoneEventType = Literal["tool.execution.done"] +ToolExecutionDoneEventType = Literal["tool.execution.done",] class ToolExecutionDoneEventTypedDict(TypedDict): diff --git a/src/mistralai/models/toolexecutionentry.py b/src/mistralai/models/toolexecutionentry.py index db503ea8..c2f1f727 100644 --- a/src/mistralai/models/toolexecutionentry.py +++ b/src/mistralai/models/toolexecutionentry.py @@ -9,9 +9,10 @@ from typing_extensions import NotRequired, TypedDict -ToolExecutionEntryObject = Literal["entry"] +ToolExecutionEntryObject = Literal["entry",] -ToolExecutionEntryType = Literal["tool.execution"] + +ToolExecutionEntryType = Literal["tool.execution",] class ToolExecutionEntryTypedDict(TypedDict): diff --git a/src/mistralai/models/toolexecutionstartedevent.py b/src/mistralai/models/toolexecutionstartedevent.py index 7a54058f..f843810f 100644 --- a/src/mistralai/models/toolexecutionstartedevent.py +++ b/src/mistralai/models/toolexecutionstartedevent.py @@ -8,7 +8,7 @@ from typing_extensions import NotRequired, TypedDict -ToolExecutionStartedEventType = Literal["tool.execution.started"] +ToolExecutionStartedEventType = Literal["tool.execution.started",] class ToolExecutionStartedEventTypedDict(TypedDict): diff --git a/src/mistralai/models/toolfilechunk.py b/src/mistralai/models/toolfilechunk.py index 77c07d6d..d3e9dcd4 100644 --- a/src/mistralai/models/toolfilechunk.py +++ b/src/mistralai/models/toolfilechunk.py @@ -8,7 +8,7 @@ from typing_extensions import NotRequired, TypedDict -ToolFileChunkType = Literal["tool_file"] +ToolFileChunkType = Literal["tool_file",] class ToolFileChunkTypedDict(TypedDict): diff --git a/src/mistralai/models/toolmessage.py b/src/mistralai/models/toolmessage.py index 82f62e0f..ef917c43 100644 --- a/src/mistralai/models/toolmessage.py +++ b/src/mistralai/models/toolmessage.py @@ -16,7 +16,7 @@ ToolMessageContent = TypeAliasType("ToolMessageContent", Union[str, List[ContentChunk]]) -ToolMessageRole = Literal["tool"] +ToolMessageRole = Literal["tool",] class ToolMessageTypedDict(TypedDict): diff --git a/src/mistralai/models/toolreferencechunk.py b/src/mistralai/models/toolreferencechunk.py index e50b8451..48035484 100644 --- a/src/mistralai/models/toolreferencechunk.py +++ b/src/mistralai/models/toolreferencechunk.py @@ -8,7 +8,7 @@ from typing_extensions import NotRequired, TypedDict -ToolReferenceChunkType = Literal["tool_reference"] +ToolReferenceChunkType = Literal["tool_reference",] class ToolReferenceChunkTypedDict(TypedDict): diff --git a/src/mistralai/models/tooltypes.py b/src/mistralai/models/tooltypes.py index fb581820..f54893c2 100644 --- a/src/mistralai/models/tooltypes.py +++ b/src/mistralai/models/tooltypes.py @@ -5,4 +5,4 @@ from typing import Literal, Union -ToolTypes = Union[Literal["function"], UnrecognizedStr] +ToolTypes = Union[Literal["function",], UnrecognizedStr] diff --git a/src/mistralai/models/transcriptionsegmentchunk.py b/src/mistralai/models/transcriptionsegmentchunk.py index 53f1b397..aa30f053 100644 --- a/src/mistralai/models/transcriptionsegmentchunk.py +++ b/src/mistralai/models/transcriptionsegmentchunk.py @@ -8,7 +8,7 @@ from typing_extensions import NotRequired, TypedDict -Type = Literal["transcription_segment"] +Type = Literal["transcription_segment",] class TranscriptionSegmentChunkTypedDict(TypedDict): diff --git a/src/mistralai/models/transcriptionstreamdone.py b/src/mistralai/models/transcriptionstreamdone.py index ffd0e080..e1b1ab3d 100644 --- a/src/mistralai/models/transcriptionstreamdone.py +++ b/src/mistralai/models/transcriptionstreamdone.py @@ -13,7 +13,7 @@ from typing_extensions import NotRequired, TypedDict -TranscriptionStreamDoneType = Literal["transcription.done"] +TranscriptionStreamDoneType = Literal["transcription.done",] class TranscriptionStreamDoneTypedDict(TypedDict): diff --git a/src/mistralai/models/transcriptionstreamlanguage.py b/src/mistralai/models/transcriptionstreamlanguage.py index 8fc2aa6e..15b75144 100644 --- a/src/mistralai/models/transcriptionstreamlanguage.py +++ b/src/mistralai/models/transcriptionstreamlanguage.py @@ -8,7 +8,7 @@ from typing_extensions import NotRequired, TypedDict -TranscriptionStreamLanguageType = Literal["transcription.language"] +TranscriptionStreamLanguageType = Literal["transcription.language",] class TranscriptionStreamLanguageTypedDict(TypedDict): diff --git a/src/mistralai/models/transcriptionstreamsegmentdelta.py b/src/mistralai/models/transcriptionstreamsegmentdelta.py index 61b396b4..d779ed83 100644 --- a/src/mistralai/models/transcriptionstreamsegmentdelta.py +++ b/src/mistralai/models/transcriptionstreamsegmentdelta.py @@ -8,7 +8,7 @@ from typing_extensions import NotRequired, TypedDict -TranscriptionStreamSegmentDeltaType = Literal["transcription.segment"] +TranscriptionStreamSegmentDeltaType = Literal["transcription.segment",] class TranscriptionStreamSegmentDeltaTypedDict(TypedDict): diff --git a/src/mistralai/models/transcriptionstreamtextdelta.py b/src/mistralai/models/transcriptionstreamtextdelta.py index 8f0b0e59..daee151f 100644 --- a/src/mistralai/models/transcriptionstreamtextdelta.py +++ b/src/mistralai/models/transcriptionstreamtextdelta.py @@ -8,7 +8,7 @@ from typing_extensions import NotRequired, TypedDict -TranscriptionStreamTextDeltaType = Literal["transcription.text.delta"] +TranscriptionStreamTextDeltaType = Literal["transcription.text.delta",] class TranscriptionStreamTextDeltaTypedDict(TypedDict): diff --git a/src/mistralai/models/unarchiveftmodelout.py b/src/mistralai/models/unarchiveftmodelout.py index 6b2f730d..55c0ea8a 100644 --- a/src/mistralai/models/unarchiveftmodelout.py +++ b/src/mistralai/models/unarchiveftmodelout.py @@ -6,7 +6,7 @@ from typing_extensions import NotRequired, TypedDict -UnarchiveFTModelOutObject = Literal["model"] +UnarchiveFTModelOutObject = Literal["model",] class UnarchiveFTModelOutTypedDict(TypedDict): diff --git a/src/mistralai/models/usermessage.py b/src/mistralai/models/usermessage.py index 049bc755..61590bed 100644 --- a/src/mistralai/models/usermessage.py +++ b/src/mistralai/models/usermessage.py @@ -16,7 +16,7 @@ UserMessageContent = TypeAliasType("UserMessageContent", Union[str, List[ContentChunk]]) -UserMessageRole = Literal["user"] +UserMessageRole = Literal["user",] class UserMessageTypedDict(TypedDict): diff --git a/src/mistralai/models/wandbintegration.py b/src/mistralai/models/wandbintegration.py index 0789b648..69053896 100644 --- a/src/mistralai/models/wandbintegration.py +++ b/src/mistralai/models/wandbintegration.py @@ -7,7 +7,7 @@ from typing_extensions import NotRequired, TypedDict -WandbIntegrationType = Literal["wandb"] +WandbIntegrationType = Literal["wandb",] class WandbIntegrationTypedDict(TypedDict): diff --git a/src/mistralai/models/wandbintegrationout.py b/src/mistralai/models/wandbintegrationout.py index a1c2f570..f5a9ba80 100644 --- a/src/mistralai/models/wandbintegrationout.py +++ b/src/mistralai/models/wandbintegrationout.py @@ -7,7 +7,7 @@ from typing_extensions import NotRequired, TypedDict -WandbIntegrationOutType = Literal["wandb"] +WandbIntegrationOutType = Literal["wandb",] class WandbIntegrationOutTypedDict(TypedDict): diff --git a/src/mistralai/models/websearchpremiumtool.py b/src/mistralai/models/websearchpremiumtool.py index 70fc5626..3bbe753a 100644 --- a/src/mistralai/models/websearchpremiumtool.py +++ b/src/mistralai/models/websearchpremiumtool.py @@ -6,7 +6,7 @@ from typing_extensions import NotRequired, TypedDict -WebSearchPremiumToolType = Literal["web_search_premium"] +WebSearchPremiumToolType = Literal["web_search_premium",] class WebSearchPremiumToolTypedDict(TypedDict): diff --git a/src/mistralai/models/websearchtool.py b/src/mistralai/models/websearchtool.py index 3dfd1c53..eeafecb4 100644 --- a/src/mistralai/models/websearchtool.py +++ b/src/mistralai/models/websearchtool.py @@ -6,7 +6,7 @@ from typing_extensions import NotRequired, TypedDict -WebSearchToolType = Literal["web_search"] +WebSearchToolType = Literal["web_search",] class WebSearchToolTypedDict(TypedDict): diff --git a/src/mistralai/models_.py b/src/mistralai/models_.py index b712c545..b6cc3186 100644 --- a/src/mistralai/models_.py +++ b/src/mistralai/models_.py @@ -5,6 +5,7 @@ from mistralai._hooks import HookContext from mistralai.types import OptionalNullable, UNSET from mistralai.utils import get_security_from_env +from mistralai.utils.unmarshal_json_response import unmarshal_json_response from typing import Any, Mapping, Optional @@ -78,31 +79,20 @@ def list( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ModelList) + return unmarshal_json_response(models.ModelList, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def list_async( self, @@ -171,31 +161,20 @@ async def list_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ModelList) + return unmarshal_json_response(models.ModelList, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def retrieve( self, @@ -271,34 +250,23 @@ def retrieve( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json( - http_res.text, + return unmarshal_json_response( models.RetrieveModelV1ModelsModelIDGetResponseRetrieveModelV1ModelsModelIDGet, + http_res, ) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def retrieve_async( self, @@ -374,34 +342,23 @@ async def retrieve_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json( - http_res.text, + return unmarshal_json_response( models.RetrieveModelV1ModelsModelIDGetResponseRetrieveModelV1ModelsModelIDGet, + http_res, ) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def delete( self, @@ -477,31 +434,20 @@ def delete( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.DeleteModelOut) + return unmarshal_json_response(models.DeleteModelOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def delete_async( self, @@ -577,31 +523,20 @@ async def delete_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.DeleteModelOut) + return unmarshal_json_response(models.DeleteModelOut, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def update( self, @@ -687,29 +622,17 @@ def update( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json( - http_res.text, - models.JobsAPIRoutesFineTuningUpdateFineTunedModelResponse, + return unmarshal_json_response( + models.JobsAPIRoutesFineTuningUpdateFineTunedModelResponse, http_res ) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def update_async( self, @@ -795,29 +718,17 @@ async def update_async( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json( - http_res.text, - models.JobsAPIRoutesFineTuningUpdateFineTunedModelResponse, + return unmarshal_json_response( + models.JobsAPIRoutesFineTuningUpdateFineTunedModelResponse, http_res ) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def archive( self, @@ -892,26 +803,15 @@ def archive( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ArchiveFTModelOut) + return unmarshal_json_response(models.ArchiveFTModelOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def archive_async( self, @@ -986,26 +886,15 @@ async def archive_async( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.ArchiveFTModelOut) + return unmarshal_json_response(models.ArchiveFTModelOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def unarchive( self, @@ -1080,26 +969,15 @@ def unarchive( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.UnarchiveFTModelOut) + return unmarshal_json_response(models.UnarchiveFTModelOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def unarchive_async( self, @@ -1174,23 +1052,12 @@ async def unarchive_async( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.UnarchiveFTModelOut) + return unmarshal_json_response(models.UnarchiveFTModelOut, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) diff --git a/src/mistralai/ocr.py b/src/mistralai/ocr.py index a7b1d04a..bed8b7be 100644 --- a/src/mistralai/ocr.py +++ b/src/mistralai/ocr.py @@ -5,6 +5,7 @@ from mistralai._hooks import HookContext from mistralai.types import Nullable, OptionalNullable, UNSET from mistralai.utils import get_security_from_env +from mistralai.utils.unmarshal_json_response import unmarshal_json_response from typing import Any, List, Mapping, Optional, Union @@ -118,31 +119,20 @@ def process( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.OCRResponse) + return unmarshal_json_response(models.OCRResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def process_async( self, @@ -251,28 +241,17 @@ async def process_async( response_data: Any = None if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.OCRResponse) + return unmarshal_json_response(models.OCRResponse, http_res) if utils.match_response(http_res, "422", "application/json"): - response_data = utils.unmarshal_json( - http_res.text, models.HTTPValidationErrorData + response_data = unmarshal_json_response( + models.HTTPValidationErrorData, http_res ) - raise models.HTTPValidationError(data=response_data) + raise models.HTTPValidationError(response_data, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) diff --git a/src/mistralai/sdk.py b/src/mistralai/sdk.py index 23d31cc7..c83b53e0 100644 --- a/src/mistralai/sdk.py +++ b/src/mistralai/sdk.py @@ -10,6 +10,7 @@ from mistralai import models, utils from mistralai._hooks import SDKHooks from mistralai.types import OptionalNullable, UNSET +import sys from typing import Any, Callable, Dict, Optional, TYPE_CHECKING, Union, cast import weakref @@ -91,7 +92,7 @@ def __init__( """ client_supplied = True if client is None: - client = httpx.Client() + client = httpx.Client(follow_redirects=True) client_supplied = False assert issubclass( @@ -100,7 +101,7 @@ def __init__( async_client_supplied = True if async_client is None: - async_client = httpx.AsyncClient() + async_client = httpx.AsyncClient(follow_redirects=True) async_client_supplied = False if debug_logger is None: @@ -135,6 +136,7 @@ def __init__( timeout_ms=timeout_ms, debug_logger=debug_logger, ), + parent_ref=self, ) hooks = SDKHooks() @@ -159,13 +161,24 @@ def __init__( self.sdk_configuration.async_client_supplied, ) + def dynamic_import(self, modname, retries=3): + for attempt in range(retries): + try: + return importlib.import_module(modname) + except KeyError: + # Clear any half-initialized module and retry + sys.modules.pop(modname, None) + if attempt == retries - 1: + break + raise KeyError(f"Failed to import module '{modname}' after {retries} attempts") + def __getattr__(self, name: str): if name in self._sub_sdk_map: module_path, class_name = self._sub_sdk_map[name] try: - module = importlib.import_module(module_path) + module = self.dynamic_import(module_path) klass = getattr(module, class_name) - instance = klass(self.sdk_configuration) + instance = klass(self.sdk_configuration, parent_ref=self) setattr(self, name, instance) return instance except ImportError as e: diff --git a/src/mistralai/transcriptions.py b/src/mistralai/transcriptions.py index 24975cb2..3e2de6f5 100644 --- a/src/mistralai/transcriptions.py +++ b/src/mistralai/transcriptions.py @@ -5,6 +5,7 @@ from mistralai._hooks import HookContext from mistralai.types import OptionalNullable, UNSET from mistralai.utils import eventstreaming, get_security_from_env +from mistralai.utils.unmarshal_json_response import unmarshal_json_response from typing import List, Mapping, Optional, Union @@ -103,26 +104,15 @@ def complete( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.TranscriptionResponse) + return unmarshal_json_response(models.TranscriptionResponse, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) async def complete_async( self, @@ -216,26 +206,15 @@ async def complete_async( ) if utils.match_response(http_res, "200", "application/json"): - return utils.unmarshal_json(http_res.text, models.TranscriptionResponse) + return unmarshal_json_response(models.TranscriptionResponse, http_res) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") - http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res) def stream( self, @@ -337,26 +316,17 @@ def stream( return eventstreaming.EventStream( http_res, lambda raw: utils.unmarshal_json(raw, models.TranscriptionStreamEvents), + client_ref=self, ) if utils.match_response(http_res, "4XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") http_res_text = utils.stream_to_text(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res, http_res_text) async def stream_async( self, @@ -458,23 +428,14 @@ async def stream_async( return eventstreaming.EventStreamAsync( http_res, lambda raw: utils.unmarshal_json(raw, models.TranscriptionStreamEvents), + client_ref=self, ) if utils.match_response(http_res, "4XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) if utils.match_response(http_res, "5XX", "*"): http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - "API error occurred", http_res.status_code, http_res_text, http_res - ) + raise models.SDKError("API error occurred", http_res, http_res_text) - content_type = http_res.headers.get("Content-Type") http_res_text = await utils.stream_to_text_async(http_res) - raise models.SDKError( - f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", - http_res.status_code, - http_res_text, - http_res, - ) + raise models.SDKError("Unexpected response received", http_res, http_res_text) diff --git a/src/mistralai/utils/__init__.py b/src/mistralai/utils/__init__.py index 3d078198..87192dde 100644 --- a/src/mistralai/utils/__init__.py +++ b/src/mistralai/utils/__init__.py @@ -2,6 +2,8 @@ from typing import TYPE_CHECKING from importlib import import_module +import builtins +import sys if TYPE_CHECKING: from .annotations import get_discriminator @@ -161,6 +163,18 @@ } +def dynamic_import(modname, retries=3): + for attempt in range(retries): + try: + return import_module(modname, __package__) + except KeyError: + # Clear any half-initialized module and retry + sys.modules.pop(modname, None) + if attempt == retries - 1: + break + raise KeyError(f"Failed to import module '{modname}' after {retries} attempts") + + def __getattr__(attr_name: str) -> object: module_name = _dynamic_imports.get(attr_name) if module_name is None: @@ -169,9 +183,8 @@ def __getattr__(attr_name: str) -> object: ) try: - module = import_module(module_name, __package__) - result = getattr(module, attr_name) - return result + module = dynamic_import(module_name) + return getattr(module, attr_name) except ImportError as e: raise ImportError( f"Failed to import {attr_name} from {module_name}: {e}" @@ -183,5 +196,5 @@ def __getattr__(attr_name: str) -> object: def __dir__(): - lazy_attrs = list(_dynamic_imports.keys()) - return sorted(lazy_attrs) + lazy_attrs = builtins.list(_dynamic_imports.keys()) + return builtins.sorted(lazy_attrs) diff --git a/src/mistralai/utils/annotations.py b/src/mistralai/utils/annotations.py index 387874ed..12e0aa4f 100644 --- a/src/mistralai/utils/annotations.py +++ b/src/mistralai/utils/annotations.py @@ -3,6 +3,7 @@ from enum import Enum from typing import Any, Optional + def get_discriminator(model: Any, fieldname: str, key: str) -> str: """ Recursively search for the discriminator attribute in a model. @@ -25,31 +26,54 @@ def get_field_discriminator(field: Any) -> Optional[str]: if isinstance(field, dict): if key in field: - return f'{field[key]}' + return f"{field[key]}" if hasattr(field, fieldname): attr = getattr(field, fieldname) if isinstance(attr, Enum): - return f'{attr.value}' - return f'{attr}' + return f"{attr.value}" + return f"{attr}" if hasattr(field, upper_fieldname): attr = getattr(field, upper_fieldname) if isinstance(attr, Enum): - return f'{attr.value}' - return f'{attr}' + return f"{attr.value}" + return f"{attr}" return None + def search_nested_discriminator(obj: Any) -> Optional[str]: + """Recursively search for discriminator in nested structures.""" + # First try direct field lookup + discriminator = get_field_discriminator(obj) + if discriminator is not None: + return discriminator + + # If it's a dict, search in nested values + if isinstance(obj, dict): + for value in obj.values(): + if isinstance(value, list): + # Search in list items + for item in value: + nested_discriminator = search_nested_discriminator(item) + if nested_discriminator is not None: + return nested_discriminator + elif isinstance(value, dict): + # Search in nested dict + nested_discriminator = search_nested_discriminator(value) + if nested_discriminator is not None: + return nested_discriminator + + return None if isinstance(model, list): for field in model: - discriminator = get_field_discriminator(field) + discriminator = search_nested_discriminator(field) if discriminator is not None: return discriminator - discriminator = get_field_discriminator(model) + discriminator = search_nested_discriminator(model) if discriminator is not None: return discriminator - raise ValueError(f'Could not find discriminator field {fieldname} in {model}') + raise ValueError(f"Could not find discriminator field {fieldname} in {model}") diff --git a/src/mistralai/utils/eventstreaming.py b/src/mistralai/utils/eventstreaming.py index 74a63f75..0969899b 100644 --- a/src/mistralai/utils/eventstreaming.py +++ b/src/mistralai/utils/eventstreaming.py @@ -17,6 +17,9 @@ class EventStream(Generic[T]): + # Holds a reference to the SDK client to avoid it being garbage collected + # and cause termination of the underlying httpx client. + client_ref: Optional[object] response: httpx.Response generator: Generator[T, None, None] @@ -25,9 +28,11 @@ def __init__( response: httpx.Response, decoder: Callable[[str], T], sentinel: Optional[str] = None, + client_ref: Optional[object] = None, ): self.response = response self.generator = stream_events(response, decoder, sentinel) + self.client_ref = client_ref def __iter__(self): return self @@ -43,6 +48,9 @@ def __exit__(self, exc_type, exc_val, exc_tb): class EventStreamAsync(Generic[T]): + # Holds a reference to the SDK client to avoid it being garbage collected + # and cause termination of the underlying httpx client. + client_ref: Optional[object] response: httpx.Response generator: AsyncGenerator[T, None] @@ -51,9 +59,11 @@ def __init__( response: httpx.Response, decoder: Callable[[str], T], sentinel: Optional[str] = None, + client_ref: Optional[object] = None, ): self.response = response self.generator = stream_events_async(response, decoder, sentinel) + self.client_ref = client_ref def __aiter__(self): return self diff --git a/src/mistralai/utils/serializers.py b/src/mistralai/utils/serializers.py index 76e44d71..378a14c0 100644 --- a/src/mistralai/utils/serializers.py +++ b/src/mistralai/utils/serializers.py @@ -192,7 +192,9 @@ def is_union(obj: object) -> bool: """ Returns True if the given object is a typing.Union or typing_extensions.Union. """ - return any(obj is typing_obj for typing_obj in _get_typing_objects_by_name_of("Union")) + return any( + obj is typing_obj for typing_obj in _get_typing_objects_by_name_of("Union") + ) def stream_to_text(stream: httpx.Response) -> str: @@ -245,4 +247,3 @@ def _get_typing_objects_by_name_of(name: str) -> Tuple[Any, ...]: f"Neither typing nor typing_extensions has an object called {name!r}" ) return result - diff --git a/src/mistralai/utils/unmarshal_json_response.py b/src/mistralai/utils/unmarshal_json_response.py new file mode 100644 index 00000000..c0ce7e0f --- /dev/null +++ b/src/mistralai/utils/unmarshal_json_response.py @@ -0,0 +1,24 @@ +"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.""" + +from typing import Any, Optional + +import httpx + +from .serializers import unmarshal_json +from mistralai import models + + +def unmarshal_json_response( + typ: Any, http_res: httpx.Response, body: Optional[str] = None +) -> Any: + if body is None: + body = http_res.text + try: + return unmarshal_json(body, typ) + except Exception as e: + raise models.ResponseValidationError( + "Response validation failed", + http_res, + e, + body, + ) from e