Skip to main content

LangChain

This page shows you how to give LangChain and LangGraph agents news tools with langchain-gnews.

langchain-gnews gives LangChain and LangGraph agents two tools built on GNews API. Web search tools return the most relevant pages; these tools return dated news articles with their source, filterable by country and language, including over past periods.

ToolName seen by the modelUse it for
GNewsTopHeadlinesgnews_top_headlinesToday's top headlines, based on Google News rankings, by category, country, and language: news briefings, digests
GNewsSearchgnews_searchArticles about a topic, company, or person, filtered by language, country, and date, with historical data since 2020

Each tool call makes one API request with your API key: it counts toward your daily quota and follows your plan (articles per request, historical data, and freshness: see Article Availability).

Install​

pip install langchain-gnews

Python 3.10+. The package depends on langchain-core 1.x and on the official Python SDK.

Sign up to get a free API key, then put it in the GNEWS_API_KEY environment variable, or pass it to the tools with api_key="YOUR_API_KEY".

export GNEWS_API_KEY="YOUR_API_KEY"

Use with an agent​

Requires langchain and a model integration, for example pip install langchain langchain-anthropic.

from langchain.agents import create_agent
from langchain_gnews import GNewsSearch, GNewsTopHeadlines

agent = create_agent("anthropic:claude-sonnet-5-5", tools=[GNewsTopHeadlines(), GNewsSearch()])
agent.invoke({"messages": [{"role": "user", "content": "Summarize this morning's top business headlines in the United States"}]})

The model picks the tool from its description: headlines for a briefing, search for a topic or a period.

Use directly​

from langchain_gnews import GNewsSearch, GNewsTopHeadlines

GNewsTopHeadlines().invoke({"category": "business", "lang": "en"})
GNewsSearch().invoke({"q": "AI", "lang": "en"})

Both return {"total_articles": int, "articles": [...]}, each article with title, description, content, url, source, and published_at.

Available tools​

Searches articles by keyword, like the Search endpoint.

NameTypeRequiredDefaultDescription
qstringYesNoneWhat to search for, with the query syntax (quotes, AND, OR, NOT, parentheses)
langstringNoAny2-letter language code, for example en
countrystringNoAny2-letter country code of the source, for example us
from_datestringNoNoneOldest publication date, ISO 8601, for example 2025-03-01. The archive starts in 2020
to_datestringNoNoneNewest publication date, ISO 8601
sortbystringNopublishedAtpublishedAt (most recent first) or relevance

gnews_top_headlines​

Gets the top headlines of a category, like the Top Headlines endpoint.

NameTypeRequiredDefaultDescription
categorystringNogeneralgeneral, world, nation, business, technology, entertainment, sports, science, or health
langstringNoAny2-letter language code, for example en
countrystringNoAny2-letter country code, for example us
qstringNoNoneOptional query the headlines must match

Options​

Both tools accept these keyword arguments when created, for example GNewsSearch(max_results=5, include_content=False).

NameTypeRequiredDefaultDescription
api_keystringNoGNEWS_API_KEYGNews API key
max_resultsintegerNo10Articles per request, up to the limit of your plan
include_contentbooleanNoTrueInclude the article text. Set to False to save tokens

API errors (invalid API key, quota reached, rate limit) are returned to the model as tool errors instead of raising, so the agent can tell the user or try again.

What your plan returns​

The tools return what your plan allows: see Article Availability for historical data and freshness, JSON Response for truncated content, and the Pricing page for the plans.

Source code​

The package is open source, in the langchain-gnews repository on GitHub, and published on PyPI.