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

Supabase LangChain Edge Functions中无法通过Import Map统一@langchain/core版本

解决Supabase LangChain Edge Functions中@langchain/core版本不一致导致的类型不匹配问题

问题描述

在Supabase Edge Functions中使用LangChain生态包(langchain/、@langchain/core、@langchain/groq、@langchain/openai)时,不同包依赖的@langchain/core版本不统一,引发类型不匹配错误,例如:

Type 'PromptTemplate<any, any>' does not satisfy the constraint 'Runnable<any, any, RunnableConfig>'

示例代码编译失败:

import { ChatGroq } from "@langchain/groq";
import { ChatOpenAI } from "@langchain/openai";
import { ChatPromptTemplate } from "@langchain/core/prompts";

const openAi = new ChatOpenAI({ model: "gpt-3.5-turbo" });    
const groq = new ChatGroq({ model: "llama3-70b-8192" });

const prompt = ChatPromptTemplate.fromMessages([
  ["system", "You are a helpful assistant."],
  ["user", "{input}"]
]);

// 类型不匹配错误
const wontCompileChain = prompt.pipe(openAi);
// 部分场景可能编译通过,但仍存在潜在类型风险
const compilingChain = prompt.pipe(groq);

已尝试的无效方案

  1. 全局覆盖esm.sh的@langchain/core导入
"https://esm.sh/": {
  "@langchain/core": "https://esm.sh/@langchain/core@0.2.29/"
},
  1. 针对langchain/和@langchain/openai单独覆盖版本
"https://esm.sh/langchain@0.2.27/": {
  "@langchain/core": "https://esm.sh/@langchain/core@0.2.27/"
},
"https://esm.sh/@langchain/openai@0.3.0/": {
  "@langchain/core/": "https://esm.sh/v135/@langchain/core@0.2.29/"
}
  1. 使用langchain文件内路径覆盖,重置缓存出现模块未找到错误
"https://esm.sh/langchain@0.2.12/": {
  "https://esm.sh/v135/@langchain/core@0.2.31/": "https://esm.sh/@langchain/core@0.2.29/"
},
  1. 统一使用0.2.27版本,仍出现类型不匹配,推测存在其他版本加载

解决方案

1. 精确锁定所有LangChain相关包的兼容版本组合

选择LangChain官方确认兼容的版本组(建议同一主版本号下的对应子版本),在import_map.json中同时全局指定所有LangChain包的版本,并强制所有依赖路径下的@langchain/core指向同一版本。

示例配置:

{
  "imports": {
    "@langchain/core": "https://esm.sh/@langchain/core@0.2.29",
    "@langchain/groq": "https://esm.sh/@langchain/groq@0.2.9",
    "@langchain/openai": "https://esm.sh/@langchain/openai@0.2.10",
    "langchain": "https://esm.sh/langchain@0.2.29"
  },
  "scopes": {
    "https://esm.sh/": {
      "@langchain/core": "https://esm.sh/@langchain/core@0.2.29"
    }
  }
}

说明:确保@langchain/groq、@langchain/openai等子包的版本与@langchain/core版本严格兼容,避免跨版本依赖。

2. 清理Deno缓存

修改配置后必须清理缓存,避免残留旧版本模块:

  • 部署时添加--no-cache参数:
supabase functions deploy <your-function-name> --no-cache
  • 本地开发时,删除.supabase/functions/<function-name>/.deno_cache目录后重新部署。

3. 验证依赖版本一致性

本地用Deno检查依赖树,确认所有@langchain/core引用统一:

deno info https://esm.sh/@langchain/openai@0.2.10 --import-map=import_map.json

查看输出中的依赖部分,确保@langchain/core只有指定版本的引用。

关键注意事项

  • LangChain子包版本必须与@langchain/core版本严格匹配,可参考LangChain发布日志确认兼容版本。
  • import map的scopes配置需覆盖所有可能加载@langchain/core的源(如esm.sh),强制统一版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 01:01:13