langchain-ai / langchainjs

πŸ¦œπŸ”— Build context-aware reasoning applications πŸ¦œπŸ”—
https://js.langchain.com/docs/
MIT License
11.81k stars 1.97k forks source link

`createOpenAPIChain` throws error if `servers` field is missing #3268

Closed Manouchehri closed 4 months ago

Manouchehri commented 7 months ago
[mf:err] TypeError: Invalid URL string.
    at checkURL (/private/var/folders/wd/0gfsfmwx4b91h52nfbbfrb5m0000gn/T/tmp-39976-cnk6UsL0PIOQ/checked-fetch.js:7:6)
    at Object.apply (/private/var/folders/wd/0gfsfmwx4b91h52nfbbfrb5m0000gn/T/tmp-39976-cnk6UsL0PIOQ/checked-fetch.js:27:3)
    at defaultExecutionMethod (/Users/dave/Work/langchain-workers/langchain-worker/node_modules/langchain/dist/chains/openai_functions/openapi.js:277:36)
    at SimpleRequestChain.requestMethod (/Users/dave/Work/langchain-workers/langchain-worker/node_modules/langchain/dist/chains/openai_functions/openapi.js:377:46)
    at SimpleRequestChain._call (/Users/dave/Work/langchain-workers/langchain-worker/node_modules/langchain/dist/chains/openai_functions/openapi.js:342:37)
    at SimpleRequestChain.call (/Users/dave/Work/langchain-workers/langchain-worker/node_modules/langchain/dist/chains/base.js:110:24)
    at async SequentialChain._call (/Users/dave/Work/langchain-workers/langchain-worker/node_modules/langchain/dist/chains/sequential_chain.js:109:21)
    at async SequentialChain.call (/Users/dave/Work/langchain-workers/langchain-worker/node_modules/langchain/dist/chains/base.js:101:28)
    at async SequentialChain.run (/Users/dave/Work/langchain-workers/langchain-worker/node_modules/langchain/dist/chains/base.js:66:30)
    at async Object.fetch (/Users/dave/Work/langchain-workers/langchain-worker/src/index.ts:357:20)
{
  "openapi": "3.0.0",
  "info": {
    "version": "1.0.0",
    "title": "Cayman Weather API (unofficial)"
  },
  "tags": [
    {
      "name": "language",
      "description": "en"
    }
  ],
  "components": {
    "schemas": {},
    "parameters": {}
  },
  "paths": {
    "/api/v1.0.0/weather": {
      "get": {
        "operationId": "get_weather",
        "responses": {
          "200": {
            "description": "Fetch weather data from the Cayman Weather site.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {}
                }
              }
            }
          },
          "404": {
            "description": "Unable to fetch the weather data",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "message": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "500": {
            "description": "Unexpected internal server error.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "status_code": {
                      "type": "number",
                      "description": "Original error code.",
                      "example": 504
                    },
                    "message": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

We should fix this by defaulting to the same server as the spec if the servers object is missing. e.g. if the spec is on https://caymanweather.aimoda.dev/openapi.json, we can safely assume that the server is also on https://caymanweather.aimoda.dev.

Related to #3267 and semi-related to https://github.com/honojs/middleware/issues/243.

dosubot[bot] commented 7 months ago

πŸ€–

Hello again @Manouchehri, good to see you! I hope you're doing well.

Thanks for detailed report. I'm diving into this issue that you've brought up about the createOpenAPIChain error, and I'll get back to you with a more comprehensive response shortly.

dosubot[bot] commented 7 months ago

πŸ€–

Thank you for bringing this to our attention. It seems like the createOpenAPIChain function is indeed throwing an error when the servers field is missing from the OpenAPI specification. This is because the function is trying to access the url property of the first server in the servers array, which is undefined when the servers field is missing.

To fix this issue, we can modify the baseUrl getter method in the OpenAPISpec class to extract the base URL from the spec URL when the servers field is not present. Here's how you can do it:

get baseUrl() {
  if (this.document.servers) {
    return this.document.servers[0].url;
  } else {
    const url = new URL(this.document.url);
    return url.origin;
  }
}

In the above code, URL is a built-in JavaScript class for parsing URLs. url.origin gives the origin of the URL (protocol + hostname + port), which is the base URL you need.

Please note that this change should be made in the openapi.ts file in the langchain/src/chains/openai_functions/ directory of the langchainjs repository.

I hope this helps! If you have any further questions or run into any issues, please don't hesitate to ask.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a πŸ‘ if this is helpful and πŸ‘Ž if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

dosubot[bot] commented 4 months ago

Hi, @Manouchehri,

I'm helping the langchainjs team manage their backlog and am marking this issue as stale. From what I understand, you raised an issue regarding a TypeError thrown by the createOpenAPIChain function when the servers field is missing in the OpenAPI spec. Dosubot responded with a detailed explanation of the issue, suggesting a modification to the baseUrl getter method in the OpenAPISpec class to handle the absence of the servers field. The issue was marked as resolved, and Dosubot provided code snippets and links to relevant files in the repository for making the necessary changes.

Could you please confirm if this issue is still relevant to the latest version of the langchainjs repository? If it is, please let the langchainjs team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days.

Thank you!