Issue related query passing though python

this output

python .\run.py

  • Serving Flask app ‘app’
  • Debug mode: on
    Press CTRL+C to quit
  • Restarting with stat
  • Debugger is active!
  • Debugger PIN: 476-859-145
    Query vector (normalized and flat): [-0.030657846480607986, 0.02327873185276985, 0.09296496957540512, 0.03751479834318161, -0.022969737648963928, -0.09222140908241272, 0.05966345593333244, -0.02869853377342224, -0.0949459820985794, 0.045810285955667496, -0.035543717443943024, 0.006742140278220177, 0.0005982430302537978, -0.045862361788749695, 0.03785587474703789, -0.018906425684690475, 0.09357763826847076, -0.06373903155326843, -0.14830976724624634, 0.041758231818675995, -0.003536430187523365, 0.019475894048810005, -0.015388336032629013, 0.03196140378713608, -0.05327703058719635, -0.05068724974989891, -0.01152938511222601, 0.03869820386171341, 0.06343596428632736, -0.07123268395662308, -0.04867156594991684, 0.04160059243440628, 0.01691952347755432, -0.0003820383280981332, -0.02836250700056553, -0.007636316120624542, -0.006018740125000477, -0.12701500952243805, -0.009458924643695354, -0.0009261403465643525, 0.01119549572467804, -0.06675643473863602, 0.010406231507658958, -0.052923161536455154, 0.06013902276754379, -0.03928477689623833, 0.0328642874956131, 0.025922369211912155, 0.10409489274024963, 0.051861174404621124, -0.09767232835292816, -0.055394891649484634, -0.029157474637031555, 0.029145793989300728, 0.06149646267294884, 0.02306339330971241, -0.053987614810466766, 0.020314080640673637, 0.055972855538129807, -0.013774366118013859, -0.016274387016892433, 0.04501296952366829, -0.05195438116788864, -0.02932881750166416, 0.04633684456348419, -0.057380370795726776, -0.0590924508869648, -0.04769640415906906, -0.013768310658633709, -0.09683901816606522, -0.01654052734375, -0.011805209331214428, -0.013172993436455727, 0.051926638931035995, -0.009673956781625748, -0.02613298036158085, 0.020000144839286804, -0.0032394679728895426, 0.06668073683977127, 0.041441064327955246, 0.06925486028194427, -0.08234747499227524, 0.0017486423021182418, 0.004835270810872316, -0.04913676157593727, -0.041545320302248, 0.048118870705366135, 0.032431669533252716, -0.004003739450126886, 0.010176300071179867, -0.05910792946815491, 0.0176469124853611, -0.028229841962456703, 0.018983235582709312, -0.016374045982956886, -0.016045665368437767, 0.15416978299617767, -0.011314365081489086, -0.11782149225473404, 0.16073086857795715, 0.0033058596309274435, 0.09643135219812393, 0.04101315513253212, 0.031106090173125267, 0.00908935908228159, 0.0524565614759922, -0.06281297653913498, 0.06384925544261932, -0.011151393875479698, -0.0009654647437855601, 0.03905538097023964, -0.015232997946441174, 0.011961715295910835, 0.031149446964263916, 0.08224347233772278, -0.008671360090374947, 0.02839709259569645, 0.09052134305238724, 0.02100292406976223, -0.007338672876358032, 0.001480964827351272, -0.0011319678742438555, 0.0007730416837148368, -0.01576906628906727, -0.07948347926139832, 0.022203128784894943, 0.03544200584292412, -1.817517406488758e-33, 0.07963573187589645, -0.011498484760522842, 0.039109569042921066, 0.10404252260923386, -0.05239269882440567, -0.02281246893107891, -0.02295990101993084, -0.034648437052965164, 0.04665736109018326, 0.009569908492267132, 0.018526427447795868, -0.002600867534056306, -0.03850094601511955, 0.04152604565024376, -0.0280019361525774, -0.017000863328576088, -0.07478683441877365, 0.04992421343922615, -0.036203425377607346, 0.1607857048511505, -0.04477846994996071, -0.08878524601459503, -0.03112381137907505, 0.04970306158065796, 0.04604976996779442, -0.002984842751175165, -0.050943728536367416, -0.08627233654260635, -0.01187453605234623, 0.007439433131366968, -0.00524876406416297, 0.0051955534145236015, 0.031060490757226944, -0.0058043962344527245, -0.01260053925216198, -0.01904771476984024, 0.019716810435056686, -0.04820215702056885, 0.014509988017380238, 0.003965016920119524, -0.026393895968794823, 0.005469133611768484, 0.05917887017130852, -0.08114610612392426, 0.0264375489205122, -0.03459426760673523, -0.0560588575899601, 0.05635004863142967, -0.0025323363952338696, -0.00825301930308342, -0.0486624501645565, -0.009158826433122158, -0.09112043678760529, 0.07056368142366409, -0.01928703486919403, -0.006017722189426422, 0.020879553630948067, -0.03496239334344864, 0.017168426886200905, 0.0886494517326355, 0.07126102596521378, 0.1457216888666153, -0.023575834929943085, -0.06201712414622307, -0.10377661138772964, -0.0659601092338562, -0.002416865434497595, -0.035005152225494385, 0.05569002404808998, -0.08871996402740479, -0.006482898257672787, -0.010618561878800392, 0.04404458403587341, 0.03466189652681351, 0.03834734857082367, 0.049498047679662704, 0.024058930575847626, -0.013621514663100243, 0.018440308049321175, -0.0711861103773117, 0.035537056624889374, 0.09913497418165207, -0.012507149949669838, 0.006620835512876511, -0.014709523878991604, 0.0011338837211951613, -0.03428293764591217, -0.07846491783857346, 0.0016858263406902552, 0.0005895794602110982, -0.04864183068275452, 0.04502968117594719, 0.046173177659511566, -0.05192609876394272, -0.03443292900919914, 1.67065499938786e-33, 0.12164623290300369, 0.02215985581278801, -0.062360599637031555, -0.05220939591526985, -0.10853396356105804, -0.02708366885781288, -0.03568684682250023, 0.10218632221221924, -0.020497575402259827, 0.023140335455536842, 0.05248352512717247, 0.03805714473128319, 0.04303612560033798, -0.0007052610162645578, 0.03572054207324982, -0.00632092310115695, 0.13923829793930054, 0.028214314952492714, -0.05129270255565643, -0.00293002650141716, -0.04916814714670181, 0.08183927088975906, -0.020964477211236954, 0.032213132828474045, -0.01039171777665615, -0.02173514850437641, 0.013788613490760326, 0.0897928848862648, -0.06784708052873611, -0.03894418105483055, 0.04693225771188736, -0.007831024937331676, -0.03871326893568039, 0.09171123057603836, 0.07626333832740784, 0.04944080114364624, -0.024817200377583504, -0.05192693695425987, -0.02544926479458809, -0.07635045051574707, -0.07215955853462219, 0.06378719955682755, -0.045347910374403, 0.07360532134771347, 0.0849994346499443, -0.041676443070173264, -0.002369796857237816, -0.02255825512111187, -0.0376722514629364, 0.05080721899867058, 0.021429378539323807, -0.020146293565630913, 0.00802614539861679, 0.017669828608632088, -0.02864638902246952, 0.02378714084625244, -0.030204351991415024, -0.039180923253297806, -0.0001939494104590267, -0.008225763216614723, -0.003648991696536541, 0.018728574737906456, 0.02208319678902626, 0.1012524887919426, 0.03236619755625725, 0.008048437535762787, -0.020233264192938805, -0.022725099697709084, -0.06700035184621811, -0.04178674519062042, -0.10393069684505463, 0.011881350539624691, -0.02232164703309536, -0.004506994504481554, 0.0250388253480196, -0.027613161131739616, -0.0030809789896011353, 0.0009975074790418148, -0.030588200315833092, -0.006533118896186352, -0.0186246857047081, 0.021906422451138496, 0.02837100625038147, -0.08817028254270554, -0.10883824527263641, -0.05058644339442253, 0.0759797915816307, 0.05713016167283058, 0.055651791393756866, -0.0016209529712796211, 0.013461396098136902, 0.039086200296878815, -0.002864943817257881, 0.014377754181623459, 0.025447942316532135, -1.658500714540878e-08, 0.013344410806894302, 0.004874559119343758, 0.04550299420952797, 0.07985270023345947, 0.010584447532892227, 0.06809470057487488, -0.0213527362793684, -0.0500074177980423, -0.024199655279517174, -0.00458211125805974, 0.11260370910167694, 0.04921748489141464, -0.11429904401302338, -0.06891262531280518, 0.029467815533280373, -0.0283175278455019, 0.06742500513792038, 0.09133146703243256, -0.0435008741915226, -0.10220030695199966, 0.06576360017061234, 0.003300831653177738, -0.015891999006271362, 0.02354699932038784, 0.0022898707538843155, 0.026777971535921097, 0.015999717637896538, 0.0354541651904583, -0.020445676520466805, 0.07534004747867584, 0.029460936784744263, 0.14780773222446442, 0.0070060668513178825, -0.011787126772105694, -0.007074207998812199, -0.03675960749387741, -0.025697879493236542, 0.002040622988715768, 0.04686463624238968, 0.019885310903191566, -0.007428965996950865, 0.031298425048589706, -0.09778998047113419, -0.031939584761857986, -0.037322573363780975, -0.020905468612909317, 0.043624892830848694, -0.1038685217499733, 0.01248362846672535, -0.024073302745819092, -0.050762999802827835, 0.030839700251817703, 0.09397433698177338, 0.03921334072947502, 0.01486858632415533, 0.08614114671945572, -0.018662352114915848, 0.05361615866422653, 9.514684506939375e-07, 0.017637627199292183, -0.01514291949570179, -0.0045655048452317715, -0.025550251826643944, 0.03014988824725151]
    127.0.0.1 - - [03/Jun/2024 18:26:40] “POST /query HTTP/1.1” 500 -
    Traceback (most recent call last):
    File “D:\quivvvy\venv\Lib\site-packages\flask\app.py”, line 1488, in call
    return self.wsgi_app(environ, start_response)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\flask\app.py”, line 1466, in wsgi_app
    response = self.handle_exception(e)
    ^^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\flask_restx\api.py”, line 671, in error_router
    return original_handler(f)
    ^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\flask_restx\api.py”, line 669, in error_router
    return self.handle_error(e)
    ^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\flask\app.py”, line 1463, in wsgi_app
    response = self.full_dispatch_request()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\flask\app.py”, line 872, in full_dispatch_request
    rv = self.handle_user_exception(e)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\flask_restx\api.py”, line 671, in error_router
    return original_handler(f)
    ^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\flask_restx\api.py”, line 669, in error_router
    return self.handle_error(e)
    ^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\flask\app.py”, line 870, in full_dispatch_request
    rv = self.dispatch_request()
    ^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\flask\app.py”, line 855, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\flask_restx\api.py”, line 402, in wrapper
    resp = resource(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\flask\views.py”, line 110, in view
    return current_app.ensure_sync(self.dispatch_request)(**kwargs) # type: ignore[no-any-return]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\flask_restx\resource.py”, line 41, in dispatch_request
    resp = meth(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\flask_jwt_extended\view_decorators.py”, line 170, in decorator
    return current_app.ensure_sync(fn)(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\quivvy\app\knowledge_query.py”, line 68, in post
    similar_chunks = get_top_k_results_from_pinecone(“hellow how are you”, index_name=‘pdf’,namespace=‘company-name’, top_k=5)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\quivvy\app\utils\pinecone_util.py”, line 148, in get_top_k_results_from_pinecone
    response = index.query(queries=[float_vector], top_k=top_k, namespace=namespace) # Use flat_list here
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\pinecone\utils\error_handling.py”, line 10, in inner_func
    return func(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\pinecone\data\index.py”, line 385, in query
    response = self._vector_api.query(
    ^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\pinecone\core\client\api_client.py”, line 771, in call
    return self.callable(self, *args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\pinecone\core\client\api\vector_operations_api.py”, line 711, in __query
    return self.call_with_http_info(**kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\pinecone\core\client\api_client.py”, line 833, in call_with_http_info
    return self.api_client.call_api(
    ^^^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\pinecone\core\client\api_client.py”, line 408, in call_api
    return self.__call_api(resource_path, method,
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\pinecone\core\client\api_client.py”, line 202, in __call_api
    raise e
    File “D:\quivvvy\venv\Lib\site-packages\pinecone\core\client\api_client.py”, line 195, in __call_api
    response_data = self.request(
    ^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\pinecone\core\client\api_client.py”, line 454, in request
    return self.rest_client.POST(url,
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\pinecone\core\client\rest.py”, line 301, in POST
    return self.request(“POST”, url,
    ^^^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\quivvvy\venv\Lib\site-packages\pinecone\core\client\rest.py”, line 260, in request
    raise PineconeApiException(http_resp=r)
    pinecone.core.client.exceptions.PineconeApiException: (400)
    Reason: Bad Request
    HTTP response headers: HTTPHeaderDict({‘Date’: ‘Mon, 03 Jun 2024 13:26:41 GMT’, ‘Content-Type’: ‘text/plain’, ‘Content-Length’: ‘89’, ‘Connection’: ‘keep-alive’, ‘server’: ‘envoy’})
    HTTP response body: queries[383]: invalid value 0.030149888247251511 for type type.googleapis.com/QueryVector

from this code

import os
import numpy as np
from pinecone import Pinecone, ServerlessSpec
from sentence_transformers import SentenceTransformer

Initialize Pinecone

api_key = os.getenv(‘PINECONE_KEY’) # Ensure the correct environment variable is used
pc = Pinecone(api_key=api_key)

Load the sentence model

model = SentenceTransformer(‘all-MiniLM-L6-v2’)

def split_text_into_chunks(text, max_length=250):
return [text[i:i+max_length] for i in range(0, len(text), max_length)]

def normalize_vector(vector):
norm = np.linalg.norm(vector)
if norm == 0:
return np.zeros_like(vector)
normalized_vector = vector / norm
# Ensure all values are within the bounds -1 to 1
if np.any((normalized_vector < -1) | (normalized_vector > 1)):
raise ValueError(“Vector normalization failed; values out of bounds [-1, 1]”)
return normalized_vector

def text_to_vector(text_chunks):
vectors = model.encode(text_chunks)
return np.array([normalize_vector(vec) for vec in vectors])

def store_vectors_in_pinecone(text_chunks, index_name, namespace=‘example-company’):
index_name = “”.join([c if c.isalnum() else “-” for c in index_name.lower()])
namespace = “”.join([c if c.isalnum() else “-” for c in namespace.lower()])

if index_name not in pc.list_indexes().names():
    pc.create_index(
        name=index_name,
        dimension=384,
        metric="cosine",
        spec=ServerlessSpec(
            cloud='aws',
            region='us-east-1'
        )
    )

index = pc.Index(name=index_name)
vectors = text_to_vector(text_chunks)
ids = [f"chunk-{i}" for i in range(len(text_chunks))]
vectors_tuples = list(zip(ids, [vector.tolist() for vector in vectors]))
index.upsert(vectors=vectors_tuples, namespace=namespace)
return ids

def get_top_k_results_from_pinecone(query, index_name, namespace, top_k=5):
index = pc.Index(name=index_name)
query_embedding = model.encode([query])[0] # This should produce a single vector
# Normalize the query vector
query_embedding = normalize_vector(query_embedding)
float_vector = [float(x) for x in query_embedding]

# Ensure it's a flat list
if isinstance(query_embedding.tolist(), list):
    flat_list = query_embedding.tolist()  # This should already be flat
    print("Query vector (normalized and flat):", flat_list)  # Debugging output

response = index.query(queries=[float_vector], top_k=top_k, namespace=namespace) # Use flat_list here
return response['matches']

def delete_index(index_name):
if index_name in pc.list_indexes().names():
pc.delete_index(index_name)
print(f"Index ‘{index_name}’ deleted successfully.“)
else:
print(f"Index ‘{index_name}’ does not exist.”)

Hi @attaashat6 and welcome to the Pinecone community forums!

Thank you for your question.

Based off the fact that your error message includes an HTTP status code of 400 - and the error message - it seems there’s an issue in the way you’re currently handling the query embedding.

Here are some thoughts to hopefully help unstick you after reading through your code briefly:

  • Add more logging: If you can add a print statement after every line to verify your output format is as expected and that the format of your vectors you’re planning to upsert matches what Pinecone is expecting.
  • Verify Vector Dimensions: Ensure that the vector dimensions align with the Pinecone index’s expected dimension. In the example, the index is created with a dimension of 384. Make sure the query vectors match this dimension.
  • Flatten the Vector: Ensure that the query vector is properly flattened before passing it to Pinecone’s query function. This can be done by converting it into a list of floats.

Hope that helps!

Best,
Zack