DEV Community

shashank ms
shashank ms

Posted on

Using LLM for Financial Text Analysis: A Practical Guide

We are going to build a structured financial news analyzer that reads raw market text and returns sentiment, tickers, risk flags, and a brief summary. This is useful for developers and analysts who need to process earnings reports, headlines, or filings at scale without maintaining a custom NLP pipeline. We will run it entirely on Oxlo.ai using the standard OpenAI SDK.

What you'll need

  • Python 3.10 or newer
  • The openai Python package (pip install openai)
  • An Oxlo.ai API key from https://portal.oxlo.ai

If you do not have an account yet, sign up and copy your key. Oxlo.ai offers a free tier with 60 requests per day, which is enough to prototype this project.

Step 1: Initialize the Oxlo.ai client

Because Oxlo.ai is fully compatible with the OpenAI SDK, we only need to change the base_url and api_key. Create a file named analyzer.py and add the following.

from openai import OpenAI

client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")

Replace YOUR_OXLO_API_KEY with the key from your portal. I usually keep this in an environment variable, but a hardcoded constant works for a quick prototype.

Step 2: Define the system prompt

The system prompt locks the model into a structured financial analyst role and forces JSON output. This removes the need for regex parsing.

SYSTEM_PROMPT = """You are a financial text analyst. Read the user message, which contains financial news or an earnings snippet. Respond ONLY with a valid JSON object containing these keys:
- sentiment: one of ["bullish", "bearish", "neutral"]
- confidence: a float between 0.0 and 1.0
- tickers: an array of mentioned stock tickers, e.g. ["AAPL", "TSLA"]; use [] if none are found
- risk_factors: an array of short strings describing risks mentioned, e.g. ["supply chain", "regulatory delay"]; use [] if none
- summary: a single sentence summarizing the core financial implication

Do not include markdown formatting, explanations, or text outside the JSON object."""

Step 3: Build the analysis function

This helper sends text to Oxlo.ai and parses the JSON response. We will use llama-3.3-70b as the general-purpose workhorse, though you can swap in kimi-k2.6 for longer filings or deepseek-v3.2 if you are testing on the free tier.

import json

def analyze_financial_text(text: str) -> dict:
    response = client.chat.completions.create(
        model="llama-3.3-70b",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": text},
        ],
    )
    raw = response.choices[0].message.content
    return json.loads(raw)

Step 4: Process a batch of headlines

Real workloads involve more than one item. Here we loop over a list of strings, call the analyzer for each, and print the aggregated results.

headlines = [
    "Fed signals rate cuts ahead as inflation cools; tech stocks rally on renewed optimism.",
    "XYZ Corp misses Q3 revenue targets by 12%, cites supply chain disruptions in Asia.",
    "ABC Pharma receives FDA fast-track designation for novel oncology therapy.",
]

results = []
for headline in headlines:
    result = analyze_financial_text(headline)
    results.append(result)

print(json.dumps(results, indent=2))

Run it

Save everything into analyzer.py and run the script.

python analyzer.py

You should see structured JSON similar to this.

[
  {
    "sentiment": "bullish",
    "confidence": 0.85,
    "tickers": [],
    "risk_factors": [],
    "summary": "Anticipated rate cuts are driving optimism in technology stocks."
  },
  {
    "sentiment": "bearish",
    "confidence": 0.92,
    "tickers": ["XYZ"],
    "risk_factors": ["supply chain", "revenue miss"],
    "summary": "XYZ Corp reported a significant revenue shortfall due to Asian supply chain issues."
  },
  {
    "sentiment": "bullish",
    "confidence": 0.78,
    "tickers": ["ABC"],
    "risk_factors": [],
    "summary": "ABC Pharma gained FDA fast-track status, accelerating its oncology drug path."
  }
]

Next steps

Add pydantic validation to enforce schema correctness before you insert these records into a database or downstream pipeline. If you are scanning full 10-K filings instead of short headlines, switch to kimi-k2.6 on Oxlo.ai to take advantage of its 131K context window and reasoning capabilities. For cost-sensitive batch jobs, look at the request-based pricing on https://oxlo.ai/pricing. It stays flat regardless of input length, which makes long-document analysis significantly cheaper than token-based providers.

Top comments (0)