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

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions libs/community/langchain_community/tools/yahoo_finance_news.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ def _run(
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:
Expand All @@ -53,7 +62,11 @@ def _run(

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."
Expand All @@ -69,8 +82,9 @@ def _run(
@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)
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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
Expand Down
Loading