LlamaIndex
News search as a tool for LlamaIndex agents and workflows.
A tool over the HTTP API
A news search tool is one request to our API. Write it as a function with a docstring — the agent reads it
to decide when to call it — and wrap it in a FunctionTool:
pip install llama-index httpximport os
import httpx
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.core.tools import FunctionTool
from llama_index.llms.openai import OpenAI
def search_news(query: str, days: int = 7) -> list[dict]:
"""Search recent news articles. Returns the title, URL, source, publication time
and a relevance score (0-1) of each one."""
res = httpx.post(
"https://api.typesearch.ai/v1/search",
headers={"Authorization": f"Bearer {os.environ['TYPESEARCH_API_KEY']}"},
json={"query": query, "days": days, "mode": "fast", "max_results": 8},
timeout=60,
)
res.raise_for_status()
fields = ("title", "url", "source", "published_at", "score", "snippet")
return [{k: r.get(k) for k in fields} for r in res.json()["results"]]
agent = FunctionAgent(
tools=[FunctionTool.from_defaults(fn=search_news)],
llm=OpenAI(model="gpt-4.1"),
system_prompt="Answer with recent news. Cite the source and link of every fact.",
)
response = await agent.run("What changed in EU AI Act enforcement this week?")The search runs in fast mode, $1.40 per 1,000 searches. Change it to "normal" to have the top results
read before they’re ranked ($2.20); every parameter is in the API reference.
As a retriever
To feed articles into a query engine instead of an agent, turn each result into a Document with its
excerpt as the text and the rest as metadata:
from llama_index.core import Document
docs = [
Document(text=r["snippet"] or r["title"], metadata={k: r[k] for k in ("title", "url", "source", "published_at")})
for r in search_news("EU AI Act enforcement")
]We return short excerpts, never full pages. For more text about your question, add "highlights": true
and use normal or deep mode: the top results come back with verbatim excerpts chosen for your query.