Skip to content

The agent engineering platform.

community: Fix YahooFinanceNewsTool to handle updated yfinance data structure (#29498)

Julian Castro Pulgarinb7e3e337b153
diff --git a/libs/community/langchain_community/tools/yahoo_finance_news.py b/libs/community/langchain_community/tools/yahoo_finance_news.py
index 7a4f7d7f76..e8b8f648e0 100644
--- a/libs/community/langchain_community/tools/yahoo_finance_news.py
+++ b/libs/community/langchain_community/tools/yahoo_finance_news.py
@@ -36,7 +36,16 @@ class YahooFinanceNewsTool(BaseTool): # type: ignore[override, override]
query: str,
run_manager: Optional[CallbackManagerForToolRun] = None,
) -> str:
- """Use the Yahoo Finance News tool."""
+ """
+ Use the Yahoo Finance News tool.
+
+ Args:
+ query: Company ticker symbol (e.g., 'AAPL' for Apple).
+ run_manager: Optional callback manager.
+
+ Returns:
+ str: Formatted news results or error message.
+ """
try:
import yfinance
except ImportError:
@@ -53,7 +62,11 @@ class YahooFinanceNewsTool(BaseTool): # type: ignore[override, override]
links = []
try:
- links = [n["link"] for n in company.news if n["type"] == "STORY"]
+ links = [
+ n["content"]["canonicalUrl"]["url"]
+ for n in company.news
+ if n["content"]["contentType"] == "STORY"
+ ]
except (HTTPError, ReadTimeout, ConnectionError):
if not links:
return f"No news found for company that searched with {query} ticker."
@@ -69,8 +82,9 @@ class YahooFinanceNewsTool(BaseTool): # type: ignore[override, override]
@staticmethod
def _format_results(docs: Iterable[Document], query: str) -> str:
doc_strings = [
- "\n".join([doc.metadata["title"], doc.metadata["description"]])
+ "\n".join([doc.metadata["title"], doc.metadata.get("description", "")])
for doc in docs
- if query in doc.metadata["description"] or query in doc.metadata["title"]
+ if query in doc.metadata.get("description", "")
+ or query in doc.metadata["title"]
]
return "\n\n".join(doc_strings)
diff --git a/libs/community/tests/integration_tests/tools/test_yahoo_finance_news.py b/libs/community/tests/integration_tests/tools/test_yahoo_finance_news.py
index 608f1c355f..164b7ce889 100644
--- a/libs/community/tests/integration_tests/tools/test_yahoo_finance_news.py
+++ b/libs/community/tests/integration_tests/tools/test_yahoo_finance_news.py
@@ -9,7 +9,7 @@ yfinance = pytest.importorskip("yfinance")
def test_success() -> None:
"""Test that the tool runs successfully."""
tool = YahooFinanceNewsTool()
- query = "Microsoft"
+ query = "AAPL"
result = tool.run(query)
assert result is not None
assert f"Company ticker {query} not found." not in result