langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
95.57k stars 15.52k forks source link

TransportQueryError when using GraphQL tool #7488

Closed Ori-Shahar closed 11 months ago

Ori-Shahar commented 1 year ago

System Info

When running the following code:

from langchain import OpenAI
from langchain.agents import load_tools, initialize_agent, AgentType
from langchain.utilities import GraphQLAPIWrapper
from langchain.memory import ConversationBufferMemory

llm = OpenAI(temperature=0, openai_api_key=openai_api_key)
token = "..."
tools = load_tools(
    ["graphql"],
    custom_headers={"Authorization": token, "Content-Type": "application/json"},
    graphql_endpoint="...",
    llm=llm
)

memory = ConversationBufferMemory(memory_key="chat_history")
agent = initialize_agent(
    tools, llm, agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=memory
)

graphql_fields = """query getCompanies {get_companies}"""

suffix = "Call the API with schema "

agent.run(f"{suffix} {graphql_fields}")

Im getting the error: TransportQueryError: Error while fetching schema: {'errorType': 'UnauthorizedException', 'message': 'You are not authorized to make this call.'} If you don't need the schema, you can try with: "fetch_schema_from_transport=False"

It doesn't matter what value is provided under custom_headers, or if it is passed as a parameter at all. The error is always the same. Playground code from https://python.langchain.com/docs/modules/agents/tools/integrations/graphql worked as intended.

Any idea of what the problem is?

Who can help?

No response

Information

Related Components

Reproduction

from langchain import OpenAI
from langchain.agents import load_tools, initialize_agent, AgentType
from langchain.utilities import GraphQLAPIWrapper
from langchain.memory import ConversationBufferMemory

llm = OpenAI(temperature=0, openai_api_key=openai_api_key)
token = "..."
tools = load_tools(
    ["graphql"],
    custom_headers={"Authorization": token, "Content-Type": "application/json"},
    graphql_endpoint="...",
    llm=llm
)

memory = ConversationBufferMemory(memory_key="chat_history")
agent = initialize_agent(
    tools, llm, agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=memory
)

graphql_fields = """query getCompanies {get_companies}"""

suffix = "Call the API with schema "

agent.run(f"{suffix} {graphql_fields}")

TransportQueryError: Error while fetching schema: {'errorType': 'UnauthorizedException', 'message': 'You are not authorized to make this call.'}
If you don't need the schema, you can try with: "fetch_schema_from_transport=False"

Expected behavior

An allowed API call that doesn't cause authentication issues

dosubot[bot] commented 1 year ago

Answer generated by a 🤖

Answer

I understand that you're encountering a TransportQueryError when trying to use the GraphQL tool in LangChain. The error message suggests an authorization issue when fetching the schema from the GraphQL endpoint.

From your code, I see that you're passing an "Authorization" token in the custom_headers parameter of the load_tools function. However, the error message indicates that this token might not be valid or accepted by the GraphQL endpoint.

Firstly, I would recommend verifying the validity of your token and its permissions. If you're confident that the token is valid, it might be worth reaching out to the provider of the GraphQL API to understand if there are any additional requirements for authorization.

In LangChain, the GraphQLAPIWrapper class is responsible for setting up the GraphQL client. It uses the gql package to create a Client instance with the provided graphql_endpoint and custom_headers. By default, the fetch_schema_from_transport parameter is set to True, which means the client will attempt to fetch the schema from the transport during initialization.

The error message suggests that you could try setting fetch_schema_from_transport=False to bypass the schema fetching process. However, this parameter is currently hardcoded in the GraphQLAPIWrapper class and cannot be changed externally. If you're comfortable with it, you might consider modifying the GraphQLAPIWrapper class to allow this parameter to be set during instantiation.

Please note that while this could potentially resolve the authorization error, it might introduce other issues if your code relies on the schema being fetched from the transport.

This is just one potential solution based on the information provided. If you find this solution to be correct and believe it could benefit other users, you're encouraged to make a pull request with the necessary changes.

If this solution doesn't align with your experience, it might be worth checking if you're using the latest version of LangChain, as the issue might have been addressed in a more recent update.

Sources


This response is meant to be useful, save you time, and share context. 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.

Ori-Shahar commented 1 year ago

Solved: GraphQLAPIWrapper overrides "client" when initializing, and by that it nullifies custom_headers. To override it back its possible to explicitly do: tools[0].graphql_wrapper.gql_client.transport = client.transport And the TransportQueryError is solved.

Full code sample:

headers = custom_headers
transport = RequestsHTTPTransport(url=graphql_endpoint, headers=headers)
client = Client(transport=transport, fetch_schema_from_transport=True)

tools = load_tools(
    ["graphql"],
    custom_headers=custom_headers,
    graphql_endpoint=graphql_endpoint,
    llm=llm
)

tools[0].graphql_wrapper.gql_client.transport = client.transport
aerrober commented 1 year ago

This is fixed in this PR here: https://github.com/langchain-ai/langchain/pull/8187

slamer59 commented 1 year ago

Hi, I have the same issue. Graphql endpoint is public so I don't know why I should authenticate ?

Will the pull request solve this ?

Thanks :)

dosubot[bot] commented 1 year ago

Hi, @Ori-Shahar! I'm Dosu, and I'm here to help the LangChain team manage our backlog. I wanted to let you know that we are marking this issue as stale.

Based on my understanding, you encountered a TransportQueryError when using the GraphQL tool in the langchain library. The error message suggested an authorization issue when fetching the schema from the GraphQL endpoint. I provided a potential solution by suggesting to verify the validity of the token and its permissions, and to consider modifying the GraphQLAPIWrapper class.

It seems that you were able to solve the TransportQueryError by overriding the client and you even shared a code sample to demonstrate the solution. Great job! Additionally, a pull request has been made to fix the issue. However, it's worth noting that another user, slamer59, has reported encountering the same issue.

Now, we would like to confirm if this issue is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on this issue. Otherwise, feel free to close the issue yourself or it will be automatically closed in 7 days.

Thank you for your contribution and for helping us improve LangChain! If you have any further questions or concerns, please don't hesitate to reach out.