# Buggy code
def create_embedding(self, texts: [str]):
    """
    Create embeddings for each text in a list of texts using the retrievers model (`self.embedding_model`)
    :param texts: texts to embed
    :return: list of embeddings (one per input text). Each embedding is a list of floats.
    """

    # for backward compatibility: cast pure str input
    if type(texts) == str:
        texts = [texts]
    assert type(texts) == list, (
        "Expecting a list of texts, i.e. create_embeddings(texts=['text1',...])"
    )

    if self.model_format == "farm":
        res = self.embedding_model.inference_from_dicts(
            dicts=[{"text": t} for t in texts]
        )
        emb = [list(r["vec"]) for r in res]  # cast from numpy
    elif self.model_format == "sentence_transformers":
        # text is single string, sentence-transformers needs a list of strings
        res = self.embedding_model.encode(
            texts
        )  # get back list of numpy embedding vectors
        emb = [list(r) for r in res]  # cast from numpy
    return emb


# Traceback (TypeError)
Traceback (most recent call last):
File "/home/pedro/.local/lib/python3.8/site-packages/elasticsearch/serializer.py", line 50, in dumps
return json.dumps(
File "/home/pedro/.local/lib/python3.8/site-packages/simplejson/__init__.py", line 398, in dumps
return cls(
File "/home/pedro/.local/lib/python3.8/site-packages/simplejson/encoder.py", line 296, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/home/pedro/.local/lib/python3.8/site-packages/simplejson/encoder.py", line 378, in iterencode
return _iterencode(o, 0)
File "/home/pedro/.local/lib/python3.8/site-packages/elasticsearch/serializer.py", line 36, in default
raise TypeError("Unable to serialize %r (type: %s)" % (data, type(data)))
TypeError: Unable to serialize -0.065520875 (type: <class 'numpy.float32'>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "sdk/accuracy_retriever.py", line 75, in <module>
top_docs = haystack_retriever(q, doc_store)
File "/mnt/Documents/Projets/BotPress/R_D/R_D_q_a/sdk/retrievers.py", line 113, in haystack_retriever
top_docs = retriever.retrieve(query=q, top_k=10)
File "/mnt/Documents/Projets/git_clones/haystack/haystack/retriever/elasticsearch.py", line 92, in retrieve
documents = self.document_store.query_by_embedding(query_emb[0], top_k, candidate_doc_ids)
File "/mnt/Documents/Projets/git_clones/haystack/haystack/database/elasticsearch.py", line 184, in query_by_embedding
result = self.client.search(index=self.index, body=body)["hits"]["hits"]
File "/home/pedro/.local/lib/python3.8/site-packages/elasticsearch/client/utils.py", line 92, in _wrapped
return func(*args, params=params, headers=headers, **kwargs)
File "/home/pedro/.local/lib/python3.8/site-packages/elasticsearch/client/__init__.py", line 1622, in search
return self.transport.perform_request(
File "/home/pedro/.local/lib/python3.8/site-packages/elasticsearch/transport.py", line 321, in perform_request
body = self.serializer.dumps(body)
File "/home/pedro/.local/lib/python3.8/site-packages/elasticsearch/serializer.py", line 54, in dumps
raise SerializationError(data, e)
elasticsearch.exceptions.SerializationError: ({'size': 10, 'query': {'script_score': {'query': {'match_all': {}}, 'script': {'source': "cosineSimilarity(params.query_vector,doc['question_emb']) + 1.0", 'params': {'query_vector': [-0.065520875, 0.023728848, ... lot of numbers ..., 0.047961414]}}}}, '_source': {'excludes': ['question_emb']}}, TypeError("Unable to serialize -0.065520875 (type: <class 'numpy.float32'>)"))

# Fixed code
def create_embedding(self, texts: [str]):
    """
    Create embeddings for each text in a list of texts using the retrievers model (`self.embedding_model`)
    :param texts: texts to embed
    :return: list of embeddings (one per input text). Each embedding is a list of floats.
    """

    # for backward compatibility: cast pure str input
    if type(texts) == str:
        texts = [texts]
    assert type(texts) == list, (
        "Expecting a list of texts, i.e. create_embeddings(texts=['text1',...])"
    )

    if self.model_format == "farm":
        res = self.embedding_model.inference_from_dicts(
            dicts=[{"text": t} for t in texts]
        )
        emb = [list(r["vec"]) for r in res]  # cast from numpy
    elif self.model_format == "sentence_transformers":
        # text is single string, sentence-transformers needs a list of strings
        res = self.embedding_model.encode(
            texts
        )  # get back list of numpy embedding vectors
        emb = [list(r.astype("float64")) for r in res]  # cast from numpy
    return emb