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

Langchain+Azure认知搜索报错:无法从azure.search.documents.models导入Vector

导入错误:无法从azure.search.documents.models导入Vector

调用RAG链时触发导入错误,提示无法从azure.search.documents.models导入Vector,错误源自langchain/vectorstores/azuresearch.py第434行。

触发错误的代码片段

from langchain.schema.runnable import RunnablePassthrough
from langchain.prompts import ChatPromptTemplate
from langchain.chat_models.azure_openai import AzureChatOpenAI

question = "my question.."

# vector_store is initialized using AzureSearch(), not including that snippet here
retriever = vector_store.as_retriever()

template = ''' 
Answer the question based on the following context: 
{context}

Question: {question} 
'''

prompt = ChatPromptTemplate.from_template(template=template)

llm = AzureChatOpenAI( deployment_name='MY_DEPLOYMENT_NAME', model_name='MY_MODEL', openai_api_base=MY_AZURE_OPENAI_ENDPOINT, openai_api_key=MY_AZURE_OPENAI_KEY, openai_api_version='2023-05-15', openai_api_type='azure' )

rag_chain = {'context' : retriever, 'question' : RunnablePassthrough} | prompt | llm 
rag_chain.invoke(question)

当前依赖包版本

  • langchain==0.0.331
  • azure-search-documents==11.4.0b11
  • azure-core==1.29.5
  • openai==0.28.1

问题原因及解决办法

这个错误是因为langchain==0.0.331的AzureSearch向量存储实现,和你安装的azure-search-documents==11.4.0b11版本不兼容。在这个预览版的azure-search-documents里,Vector类已经从azure.search.documents.models迁移到了内部模块,而旧版langchain还在从原来的路径导入。

有两种解决方式:

  1. 降级azure-search-documents:把版本降到和langchain 0.0.331兼容的稳定版,比如11.3.0,这个版本里Vector类还在原来的路径。执行命令:
pip install azure-search-documents==11.3.0
  1. 升级langchain:把langchain升级到支持新版azure-search-documents的版本,比如>=0.1.0,新版本已经适配了Vector类的路径变化。执行命令:
pip install --upgrade langchain

注意:升级langchain后,部分API可能有变动,需要检查你的代码是否兼容(比如Runnable相关逻辑的使用方式)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 20:12:42