langchain-ai / langchain

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

using YahooFinanceNewsTool() results to KeyError: 'description' #15656

Closed abdinal1 closed 5 months ago

abdinal1 commented 9 months ago

System Info

Langchain Version: 0.0.354 (also tried with 0.1.0) Python version: 3.9.18 yfinance version: 0.2.35 OS: Windows 10

Who can help?

@hwchase17 , @agola11

Information

Related Components

Reproduction

Just exectuing the bottom of this page (the tool soley): https://python.langchain.com/docs/integrations/tools/yahoo_finance_news

Returned this error (see): from langchain.tools.yahoo_finance_news import YahooFinanceNewsTool tool = YahooFinanceNewsTool() res = tool.run("AAPL") print(res)

updating langchain to newest version didnt change anything for me.

Im also using a poetry installed file with a clean fresh enviroment, same error.

Expected behavior

To do exactly whats written in the docs and to not drop an error

dosubot[bot] commented 9 months ago

🤖

Hello @abdinal1! I'm Dosu, a friendly bot here to help you with LangChain issues and questions. While we're waiting for a human maintainer, I'll do my best to assist you. Let's tackle these bugs together!

The error you're encountering, KeyError: 'description', is likely due to the 'description' key not being present in the metadata of every Document object in the 'docs' iterable. This could occur if the WebBaseLoader.load() method does not always include a 'description' key in the metadata of the Document objects it creates.

To resolve this issue, you can modify the _format_results method to check if the 'description' key is present in the metadata of each Document object before trying to access it. Here's how you can do it:

@staticmethod
def _format_results(docs: Iterable[Document], query: str) -> str:
    doc_strings = [
        "\n".join([doc.metadata["title"], doc.metadata.get("description", "")])
        for doc in docs
        if query in doc.metadata.get("description", "") or query in doc.metadata["title"]
    ]
    return "\n\n".join(doc_strings)

In this modified version of the method, the get method is used to access the 'description' key in the metadata dictionary. The get method returns the value for the key if it is in the dictionary, and returns a default value if it is not. In this case, the default value is an empty string (""). This ensures that the KeyError will not be raised even if the 'description' key is not present in the metadata.

You can find the original implementation of the _format_results method in the libs/langchain/langchain/tools/yahoo_finance_news.py file in the LangChain repository.

Please note that this modification should be done in your local copy of the LangChain codebase. If this solution works for you, I recommend reaching out to the maintainers of the LangChain repository to let them know about this issue and the potential fix. They may want to incorporate this fix into a future version of LangChain.

Let me know if you have any other questions or if there's anything else I can help with.

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.