You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用llama-index from_vector_store遇Pydantic验证错误求助

问题分析与解决:LlamaIndex PGVectorStore ValidationError问题

报错信息

pydantic_core._pydantic_core.ValidationError: NodeWithScore存在1个验证错误
node
输入应为有效字典或BaseNode实例 [type=model_type, input_value=TextNode(id='dcb36e46-7a...metadata_seperator='\n'), input_type=TextNode]

报错代码

import textwrap

from llama_index.core import VectorStoreIndex, StorageContext
from llama_index.legacy.vector_stores import PGVectorStore
from llama_index.llms.openai import OpenAI
from sqlalchemy import make_url
import os
import openai

# Get openAI api key by reading local .env file
openai.api_key = ("my-api-key")
os.environ["OPENAI_API_KEY"] = openai.api_key

print("Connecting to new vector db...")
connection_string = "postgresql://user:user@localhost:5432"

db_name = "vector_db"

url = make_url(connection_string)
vector_store = PGVectorStore.from_params(
    database=db_name,
    host=url.host,
    password=url.password,
    port=url.port,
    user=url.username,
    table_name="table_name",
    embed_dim=1536,  # openai embedding dimension
)

index = VectorStoreIndex.from_vector_store(
    vector_store=vector_store)

llm = OpenAI(model="gpt-3.5-turbo", temperature=0.1)

query_engine = index.as_query_engine(llm=llm)

response = query_engine.query("Give me a summary of the data with code AA10B")

print(textwrap.fill(str(response), 100))

问题描述

使用VectorStoreIndex.from_vector_store()方法连接PGVectorStore时触发上述验证错误,但改用VectorStoreIndex.from_documents()直接传入文档列表时代码可正常运行,不清楚问题原因。

可正常运行的代码

import textwrap

from llama_index.core import StorageContext
from llama_index.llms.openai import OpenAI
from llama_index.readers.database import DatabaseReader
from llama_index.core import VectorStoreIndex
from llama_index.vector_stores.postgres import PGVectorStore
import openai
import os
from sqlalchemy import make_url, create_engine, text
import psycopg2

# Get openAI api key by reading local .env file
openai.api_key = ("my-api-key")
os.environ["OPENAI_API_KEY"] = openai.api_key

engine = create_engine("postgresql+psycopg2://user:user@localhost/db")

reader = DatabaseReader(
    engine=engine
)

query = """query"""

documents = reader.load_data(query=query)

# Recreate database if exists
conn = psycopg2.connect("postgres://user:user@localhost:5432/db")
conn.autocommit = True
cur = conn.cursor()
cur.execute("DROP DATABASE IF EXISTS vector_db;")
cur.execute("CREATE DATABASE vector_db;")
conn.close()

conn = psycopg2.connect("postgres://user:user@localhost:5432/vector_db")
conn.autocommit = True
cur = conn.cursor()
cur.execute("CREATE EXTENSION vector;")
conn.close()

connection_string = "postgresql://user:user@localhost:5432"

db_name = "vector_db"

url = make_url(connection_string)
vector_store = PGVectorStore.from_params(
    database=db_name,
    host=url.host,
    password=url.password,
    port=url.port,
    user=url.username,
    table_name="table",
    embed_dim=1536,  # openai embedding dimension
    hnsw_kwargs={
        "hnsw_m": 16,
        "hnsw_ef_construction": 64,
        "hnsw_ef_search": 40,
        "hnsw_dist_method": "vector_cosine_ops",
    },
)

storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
    documents, storage_context=storage_context, show_progress=True
)

llm = OpenAI(model="gpt-3.5-turbo", temperature=0.1)

query_engine = index.as_query_engine(llm=llm)

response = query_engine.query("Give me a summary of the data with code AA10B")

print(textwrap.fill(str(response), 100))

问题根源与解决方案

核心原因

报错本质是新旧版本PGVectorStore模块不兼容:

  • 报错代码导入的是llama_index.legacy.vector_stores.PGVectorStore(旧版遗留模块)
  • 正常代码导入的是llama_index.vector_stores.postgres.PGVectorStore(新版核心模块)

LlamaIndex迭代中重构了PGVectorStore的实现,旧版模块与新版VectorStoreIndex.from_vector_store()方法存在类型解析冲突,导致查询时无法正确识别存储的TextNode,触发Pydantic验证错误。

解决步骤

  1. 替换导入路径
    将报错代码中的旧版导入:

    from llama_index.legacy.vector_stores import PGVectorStore
    

    替换为新版导入:

    from llama_index.vector_stores.postgres import PGVectorStore
    
  2. 确保数据库结构兼容
    若之前用旧版PGVectorStore创建过数据表,需参考正常代码中的逻辑重建数据库(删除旧库→新建库→创建vector扩展),因为新旧版本的表结构存在差异。

  3. 补充必要参数
    新版PGVectorStore建议添加hnsw_kwargs索引参数,提升查询性能和兼容性(参考正常代码中的配置)。

修正后的完整代码

import textwrap

from llama_index.core import VectorStoreIndex, StorageContext
from llama_index.vector_stores.postgres import PGVectorStore
from llama_index.llms.openai import OpenAI
from sqlalchemy import make_url
import os
import openai

# 设置OpenAI API密钥
openai.api_key = "my-api-key"
os.environ["OPENAI_API_KEY"] = openai.api_key

print("Connecting to new vector db...")
connection_string = "postgresql://user:user@localhost:5432"

db_name = "vector_db"

url = make_url(connection_string)
vector_store = PGVectorStore.from_params(
    database=db_name,
    host=url.host,
    password=url.password,
    port=url.port,
    user=url.username,
    table_name="table_name",
    embed_dim=1536,  # OpenAI Embedding维度
    hnsw_kwargs={
        "hnsw_m": 16,
        "hnsw_ef_construction": 64,
        "hnsw_ef_search": 40,
        "hnsw_dist_method": "vector_cosine_ops",
    },
)

storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_vector_store(
    vector_store=vector_store, storage_context=storage_context
)

llm = OpenAI(model="gpt-3.5-turbo", temperature=0.1)

query_engine = index.as_query_engine(llm=llm)

response = query_engine.query("Give me a summary of the data with code AA10B")

print(textwrap.fill(str(response), 100))

内容的提问来源于stack exchange,提问作者asemprini87

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 13:35:12