TruChain集成时出现Queue类型相关PyDantic验证错误求助
问题:TruChain初始化时的Pydantic Queue验证错误
问题背景
我正在实现一个处理多选择题列表的process_questions函数,流程包括加载Embeddings模型、基于Redis搭建向量存储、创建处理链、遍历处理问题并记录结果。但在将处理链传入TruChain类时触发了Pydantic验证错误。
错误详情
self.__pydantic_validator__.validate_python(data, self_instance=self) pydantic_core._pydantic_core.ValidationError: 1 validation error for TruChain records_with_pending_feedback_results Input should be an instance of Queue [type=is_instance_of, input_value=<queue.Queue object at 0x124dd68b0>, input_type=Queue]
关联代码
from trulens_eval.feedback import Feedback, Groundedness from trulens_eval import OpenAI as fOpenAI from trulens_eval import TruChain from trulens_eval.app import App from typing import List def process_questions(questions: List[QuestionType]): # Load the base embeddings model; specifics are abstracted embeddings = load_embeddings_model() # Initialize the vector store with the embeddings model vector_store = VectorStore(embeddings, "dimension_spec", chunk_size=1000, chunk_overlap=200) # Initialize the processing mechanism with the vector store's retriever processing_mechanism = ProcessingMechanism(vector_store.get_retriever()) print("Processing Mechanism Type: ", type(processing_mechanism)) # Construct and configure the processing chain processing_chain = processing_mechanism.construct_chain() processing_chain.bind(tags="ConfigurationTags") tru_recorder = TruChain(chain) ## 错误发生在此处 # Iterate through the questions, process them, and handle responses counter = 1 for question in questions: print(counter) counter += 1 # Invoke the processing chain on the question data and record the process response, record = recorder.with_record(processing_chain.invoke, question.to_dict()) print(f"Correct answer: {question.answer}") print('Response: ', response) # Output the recorded process in a structured format json_like = record.layout_as_app() print("Record Output: ", json_like)
修复/规避方案
1. 修正变量名错误
代码中TruChain(chain)里的chain变量未定义,这是明显的语法问题,先修正为实际创建的处理链变量:
tru_recorder = TruChain(processing_chain)
2. 升级trulens_eval版本
该错误多由版本兼容性问题导致,升级到最新稳定版通常能解决:
pip install --upgrade trulens_eval
3. 显式传入Queue实例
如果升级后问题仍存在,可手动创建并传入符合要求的Queue对象:
from queue import Queue # ... 其他代码不变 ... processing_chain = processing_mechanism.construct_chain() processing_chain.bind(tags="ConfigurationTags") tru_recorder = TruChain(processing_chain, records_with_pending_feedback_results=Queue())
4. 使用标准初始化流程
遵循TruChain官方推荐的初始化方式,先创建App实例再绑定到TruChain:
# ... 其他代码不变 ... processing_chain = processing_mechanism.construct_chain() processing_chain.bind(tags="ConfigurationTags") app = App(processing_chain) tru_recorder = TruChain(app)
内容的提问来源于stack exchange,提问作者JP1990
相关产品推荐
相关产品推荐

