Skip to content

Commit 97285b5

Browse files
Jcasttropccurme
authored andcommitted
community: Fix YahooFinanceNewsTool to handle updated yfinance data structure (#29498)
*Description:** Updates the YahooFinanceNewsTool to handle the current yfinance news data structure. The tool was failing with a KeyError due to changes in the yfinance API's response format. This PR updates the code to correctly extract news URLs from the new structure. **Issue:** #29495 **Dependencies:** No new dependencies required. Works with existing yfinance package. The changes maintain backwards compatibility while fixing the KeyError that users were experiencing. The modified code properly handles the new data structure where: - News type is now at `content.contentType` - News URL is now at `content.canonicalUrl.url` --------- Co-authored-by: Chester Curme <[email protected]>
1 parent 3890a0f commit 97285b5

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

libs/community/langchain_community/tools/yahoo_finance_news.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,16 @@ def _run(
3636
query: str,
3737
run_manager: Optional[CallbackManagerForToolRun] = None,
3838
) -> str:
39-
"""Use the Yahoo Finance News tool."""
39+
"""
40+
Use the Yahoo Finance News tool.
41+
42+
Args:
43+
query: Company ticker symbol (e.g., 'AAPL' for Apple).
44+
run_manager: Optional callback manager.
45+
46+
Returns:
47+
str: Formatted news results or error message.
48+
"""
4049
try:
4150
import yfinance
4251
except ImportError:
@@ -53,7 +62,11 @@ def _run(
5362

5463
links = []
5564
try:
56-
links = [n["link"] for n in company.news if n["type"] == "STORY"]
65+
links = [
66+
n["content"]["canonicalUrl"]["url"]
67+
for n in company.news
68+
if n["content"]["contentType"] == "STORY"
69+
]
5770
except (HTTPError, ReadTimeout, ConnectionError):
5871
if not links:
5972
return f"No news found for company that searched with {query} ticker."
@@ -69,8 +82,9 @@ def _run(
6982
@staticmethod
7083
def _format_results(docs: Iterable[Document], query: str) -> str:
7184
doc_strings = [
72-
"\n".join([doc.metadata["title"], doc.metadata["description"]])
85+
"\n".join([doc.metadata["title"], doc.metadata.get("description", "")])
7386
for doc in docs
74-
if query in doc.metadata["description"] or query in doc.metadata["title"]
87+
if query in doc.metadata.get("description", "")
88+
or query in doc.metadata["title"]
7589
]
7690
return "\n\n".join(doc_strings)

libs/community/tests/integration_tests/tools/test_yahoo_finance_news.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
def test_success() -> None:
1010
"""Test that the tool runs successfully."""
1111
tool = YahooFinanceNewsTool()
12-
query = "Microsoft"
12+
query = "AAPL"
1313
result = tool.run(query)
1414
assert result is not None
1515
assert f"Company ticker {query} not found." not in result

0 commit comments

Comments
 (0)