supabase / edge-runtime

A server based on Deno runtime, capable of running JavaScript, TypeScript, and WASM services.
MIT License
639 stars 55 forks source link

Receiving 404 when calling edge function with langchain libraries in import_map.json #401

Open iliskhan opened 3 weeks ago

iliskhan commented 3 weeks ago

Bug report

Describe the bug

When running the edge function locally, everything works as expected. However, after deploying it to production, I receive a 404 error in the invocations section of the monitoring dashboard. The logs section does not display any information, making it difficult to diagnose the issue.

I suspect that the problem is related to the Langchain libraries included in the import_map.json file. I identified this by commenting out all imports and then uncommenting them one by one until the error reappeared. Previously, I encountered a similar issue and resolved it by adjusting the import sources (some libraries were imported from npm, others from esm). However, this time, I have tried multiple sources, but the problem persists.

To Reproduce

Steps to reproduce the behavior:

  1. Deploy the edge function with Langchain libraries included in the import_map.json file.
  2. Attempt to invoke the function in the production environment.
  3. Check the invocations section in the monitoring dashboard and observe the 404 error.
  4. Notice that no logs are generated in the logs section.

Expected behavior

The edge function should execute successfully without a 404 error, and logs should be generated to assist in troubleshooting.

Screenshots

image

System information

Additional context

To rule out potential network issues related to accessing npm and esm in different regions, I created a separate project and deployed it in another country, but the issue persisted.

import_map.json

{
  "imports": {
    "axios": "npm:axios@1.6.8",

    "zod": "https://deno.land/x/zod@v3.23.4/index.ts",
    "djwt": "https://deno.land/x/djwt@v3.0.2/mod.ts",
    "zod/types": "https://deno.land/x/zod@v3.23.4/types.ts",

    "cron-parser": "https://esm.sh/cron-parser@4.9.0",

    "@langchain/openai": "https://esm.sh/@langchain/openai@0.2.5",

    "@langchain/langgraph": "npm:@langchain/langgraph@0.0.31",
    "@langchain/langgraph/prebuilt": "npm:@langchain/langgraph@0.0.31/prebuilt",

    "@langchain/core": "npm:@langchain/core@0.2.18",
    "@langchain/core/tools": "npm:@langchain/core@0.2.18/tools",
    "@langchain/core/agents": "npm:@langchain/core@0.2.18/agents",
    "@langchain/core/prompts": "npm:@langchain/core@0.2.18/prompts",
    "@langchain/core/messages": "npm:@langchain/core@0.2.18/messages",
    "@langchain/core/runnables": "npm:@langchain/core@0.2.18/runnables",
    "@langchain/core/callbacks/manager": "npm:@langchain/core@0.2.18/callbacks/manager",
    "@langchain/core/utils/function_calling": "npm:@langchain/core@0.2.18/utils/function_calling"
  }
}
danielpgauer commented 3 weeks ago

I have run into the same problem. After redeploying, everything works fine again, but the issue reappears after a few days

encima commented 3 weeks ago

Thanks for reporting! Which module in particular is causing the 404?

Transferring to edge functions repo for visibility

iliskhan commented 2 weeks ago

Which module in particular is causing the 404?

I noticed that the error mainly occurs because of @langchain/openai which under the hood simply uses openai

iliskhan commented 2 weeks ago

I also built all the libraries manually and linked to my builds on GitHub and now everything works fine in production

my current import_map.json

{
  "imports": {
    "zod": "https://deno.land/x/zod@v3.23.4/index.ts",
    "djwt": "https://deno.land/x/djwt@v3.0.2/mod.ts",
    "semver": "https://deno.land/std@0.224.0/semver/mod.ts",
    "p-queue": "https://deno.land/x/p_queue@1.0.1/mod.ts",
    "zod/types": "https://deno.land/x/zod@v3.23.4/types.ts",

    "openai": "https://raw.githubusercontent.com/openai/openai-deno-build/main/mod.ts",
    "openai/helpers/zod": "https://raw.githubusercontent.com/openai/openai-deno-build/main/helpers/zod.ts",

    "axios": "npm:axios@1.6.8",

    "uuid": "https://esm.sh/uuid@10.0.0",
    "p-retry": "https://esm.sh/p-retry@6.2.0",
    "mustache": "https://esm.sh/mustache@4.2.0",
    "camelcase": "https://esm.sh/camelcase@8.0.0",
    "decamelize": "https://esm.sh/decamelize@6.0.0",
    "ansi-styles": "https://esm.sh/ansi-styles@6.2.1",
    "js-tiktoken/lite": "https://esm.sh/js-tiktoken@1.0.14/lite",
    "zod-to-json-schema": "https://esm.sh/zod-to-json-schema@3.23.2",

    "cron-parser": "https://esm.sh/cron-parser@4.9.0",

    "langsmith": "https://raw.githubusercontent.com/iliskhan/langsmith-sdk/main/js/index.js",
    "langsmith/run_trees": "https://raw.githubusercontent.com/iliskhan/langsmith-sdk/main/js/run_trees.js",
    "langsmith/singletons/traceable": "https://raw.githubusercontent.com/iliskhan/langsmith-sdk/main/js/traceable.js",

    "@langchain/openai": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/libs/langchain-openai/index.js",

    "@langchain/langgraph": "https://raw.githubusercontent.com/iliskhan/langgraphjs/main/langgraph/index.js",
    "@langchain/langgraph/prebuilt": "https://raw.githubusercontent.com/iliskhan/langgraphjs/main/langgraph/prebuilt.js",

    "@langchain/core/load": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/load.js",
    "@langchain/core/tools": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/tools.js",
    "@langchain/core/agents": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/agents.js",
    "@langchain/core/prompts": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/prompts.js",
    "@langchain/core/outputs": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/outputs.js",
    "@langchain/core/messages": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/messages.js",
    "@langchain/core/singletons": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/singletons.js",
    "@langchain/core/runnables": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/runnables.js",
    "@langchain/core/embeddings": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/embeddings.js",
    "@langchain/core/output_parsers": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/output_parsers.js",
    "@langchain/core/runnables/graph": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/runnables/graph.js",
    "@langchain/core/output_parsers/openai_tools": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/output_parsers/openai_tools.js",

    "@langchain/core/callbacks/manager": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/dist/callbacks/manager.js",

    "@langchain/core/utils/env": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/utils/env.js",
    "@langchain/core/utils/stream": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/utils/stream.js",
    "@langchain/core/utils/chunk_array": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/utils/chunk_array.js",
    "@langchain/core/utils/function_calling": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/dist/utils/function_calling.js",

    "@langchain/core/language_models/base": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/language_models/base.js",
    "@langchain/core/language_models/llms": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/language_models/llms.js",
    "@langchain/core/language_models/chat_models": "https://raw.githubusercontent.com/iliskhan/langchainjs/main/langchain-core/language_models/chat_models.js"
  }
}
iliskhan commented 2 weeks ago

"openai": "https://raw.githubusercontent.com/openai/openai-deno-build/main/mod.ts", "openai/helpers/zod": "https://raw.githubusercontent.com/openai/openai-deno-build/main/helpers/zod.ts",

and everything breaks if I don't specify these two libraries from the openai repository, although logically there should be the same code on npm, esm, and github. Perhaps the problem is with how edge runtime works with these cdn