Python调用Pinecone SDK执行upsert失败,寻求技术帮助
问题解决:Pinecone Upsert 抛出 ValueError 类型错误
错误原因
你的代码错误地将包含vectors和namespace的完整字典作为元素传入了vectors参数列表,导致Pinecone SDK无法解析向量类型。upsert方法的vectors参数需要的是单个或多个向量对象的列表,而非包含vectors键的外层字典。
修正后的代码
有两种正确的写法:
写法1:拆分参数传递
直接提取sample_doc中的vectors数组和namespace值,分别传入对应参数:
sample_doc = { "vectors": [ { "id": "item_0", "metadata": { "category": "sports", "colors": ["blue", "red", "green"], "time_stamp": 0 }, "values": [0.07446312652229216, 0.8866284618893006, 0.5244262265711986] }, ], "namespace": "example_namespace" } index = pinecone.Index(index_name) # 正确传递参数 index.upsert(vectors=sample_doc["vectors"], namespace=sample_doc["namespace"])
写法2:字典解包传递
利用Python的字典解包语法,直接将sample_doc的键值对作为参数传入:
index = pinecone.Index(index_name) # 字典解包,等价于传递vectors=...和namespace=... index.upsert(**sample_doc)
额外说明
Pinecone官方示例的upsert调用逻辑是:vectors参数接收向量对象列表,namespace是可选的独立参数。你之前尝试的index.upsert(sample_doc)也会报错,因为upsert不接受未命名的字典参数,必须明确指定参数名或使用解包语法。
内容的提问来源于stack exchange,提问作者SimonStern
相关产品推荐
相关产品推荐

