Back to Blog
Developer Tutorial

The Developer's Guide to News APIs in Python (Pandas, NLP & AI Pipelines)

Learn how to fetch, filter, and analyze real-time news data using Python. Includes practical code for Pandas DataFrame export, sentiment scoring, and LangChain RAG.

python pandas langchain tutorial

Last reviewed: 2026-08-31.

Python is the standard language for data engineering, quantitative trading, and AI workflows. Whether you are building an automated trading bot, training a sentiment classifier, or feeding fresh articles into an LLM Retrieval-Augmented Generation (RAG) system, this guide provides clean, production-ready code patterns for integrating news data in Python.


1. Quickstart: Fetching Latest Headlines

Install requests and fetch real-time news with category and language filters:

import requests

CURRENTS_API_KEY = "YOUR_API_KEY"

def get_latest_news(category="technology", language="en", count=10):
    url = "https://api.currentsapi.services/v1/latest-news"
    params = {
        "category": category,
        "language": language,
        "page_size": count,
        "apiKey": CURRENTS_API_KEY
    }
    resp = requests.get(url, params=params)
    resp.raise_for_status()
    return resp.json().get("news", [])

articles = get_latest_news(category="business")
for a in articles:
    print(f"[{a['published'][:10]}] {a['title']} -> {a['url']}")

2. Converting News Stream to Pandas DataFrame

For data science and quantitative research, convert JSON response arrays directly into structured Pandas DataFrames:

import requests
import pandas as pd

def search_news_to_df(keyword="semiconductor", days_back=7):
    url = "https://api.currentsapi.services/v1/search"
    params = {
        "keywords": keyword,
        "language": "en",
        "page_size": 50,
        "apiKey": "YOUR_API_KEY"
    }
    data = requests.get(url, params=params).json()
    articles = data.get("news", [])

    df = pd.DataFrame(articles)
    df["published"] = pd.to_datetime(df["published"])
    df = df[["published", "title", "description", "author", "url", "category"]]
    return df.sort_values(by="published", ascending=False)

df = search_news_to_df("NVIDIA")
print(f"Retrieved {len(df)} articles.")
print(df.head(3))

3. Feeding Live News into an AI/RAG Pipeline (LangChain / LlamaIndex)

Structure real-time headlines into document context for LLM prompt engineering:

import requests

def build_llm_news_context(topic="cybersecurity", max_articles=5):
    url = "https://api.currentsapi.services/v1/search"
    params = {
        "keywords": topic,
        "language": "en",
        "page_size": max_articles,
        "apiKey": "YOUR_API_KEY"
    }
    news = requests.get(url, params=params).json().get("news", [])

    context_blocks = []
    for idx, item in enumerate(news, 1):
        context_blocks.append(
            f"Source [{idx}]: {item['title']}\n"
            f"Summary: {item['description']}\n"
            f"Link: {item['url']}\n"
        )
    return "\n---\n".join(context_blocks)

prompt_context = build_llm_news_context("cloud computing")
print("Context ready for LLM prompt:\n", prompt_context[:300], "...")

Rate Limits & Best Practices

  • Free Tier: 250 requests/day (~7,500/month) — ideal for prototyping and daily Cron scripts.
  • Production Scaling: When running continuous pipelines across multiple workers, the Builder Plan ($69/mo) provides 2,500 calls/day with a 6-month historical lookback.