From 0d644c1d7fec8676fb2614d65bc22b6676bb38df Mon Sep 17 00:00:00 2001 From: Adnane Guettaf Date: Fri, 3 Jan 2025 15:54:43 +0100 Subject: [PATCH] [fix] jina json decode error --- edenai_apis/apis/jina/jina_api.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/edenai_apis/apis/jina/jina_api.py b/edenai_apis/apis/jina/jina_api.py index 6ffe1d4e..e655376b 100644 --- a/edenai_apis/apis/jina/jina_api.py +++ b/edenai_apis/apis/jina/jina_api.py @@ -1,3 +1,4 @@ +from json import JSONDecodeError from typing import List, Dict, Optional import requests @@ -32,16 +33,25 @@ def text__embeddings( model = model or "jina-embeddings-v2-base-en" resp = self.session.post( # type: ignore self.api_url, json={"input": texts, "model": model} - ).json() - if "data" not in resp: - raise ProviderException(resp["detail"]) - embeddings = resp["data"] + ) + try: + original_resp = resp.json() + except JSONDecodeError as exp: + raise ProviderException( + message="Internal server error", code=resp.status_code + ) from exp + if "data" not in original_resp: + raise ProviderException(original_resp["detail"], resp.status_code) + embeddings = original_resp["data"] # Sort resulting embeddings by index sorted_embeddings = sorted(embeddings, key=lambda e: e["index"]) # type: ignore # Return just the embeddings - items = [EmbeddingDataClass(embedding=result["embedding"]) for result in sorted_embeddings] + items = [ + EmbeddingDataClass(embedding=result["embedding"]) + for result in sorted_embeddings + ] standardized_response = EmbeddingsDataClass(items=items) return ResponseType[EmbeddingsDataClass]( - original_response=resp, + original_response=original_resp, standardized_response=standardized_response, )