Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createOpenAPIChain throws error if servers field is missing #3268

Closed
Manouchehri opened this issue Nov 14, 2023 · 3 comments
Closed

createOpenAPIChain throws error if servers field is missing #3268

Manouchehri opened this issue Nov 14, 2023 · 3 comments
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature auto:improvement Medium size change to existing code to handle new use-cases

Comments

@Manouchehri
Copy link
Contributor

[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 honojs/middleware#243.

Copy link

dosubot bot commented Nov 14, 2023

🤖

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 dosubot bot added auto:bug Related to a bug, vulnerability, unexpected error with an existing feature auto:improvement Medium size change to existing code to handle new use-cases labels Nov 14, 2023
Copy link

dosubot bot commented Nov 14, 2023

🤖

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.

Copy link

dosubot bot commented Feb 13, 2024

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!

@dosubot dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Feb 13, 2024
@dosubot dosubot bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 20, 2024
@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature auto:improvement Medium size change to existing code to handle new use-cases
Projects
None yet
Development

No branches or pull requests

1 participant