How to analyze financial reports with AI
Quick answer
Use
large language models (LLMs) like gpt-4o to parse and extract insights from financial reports by feeding them the report text and prompting for summaries, key metrics, or risk factors. Combine Python with the OpenAI API to automate this process efficiently.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the openai Python package and set your API key as an environment variable for secure access.
pip install openai output
Collecting openai Downloading openai-1.x.x-py3-none-any.whl Installing collected packages: openai Successfully installed openai-1.x.x
Step by step
This example shows how to load a financial report text, send it to gpt-4o for analysis, and receive a summary with key financial metrics.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
financial_report = '''
Company XYZ
Income Statement 2025
Revenue: $10,000,000
Net Income: $1,200,000
Operating Expenses: $5,000,000
Assets: $15,000,000
Liabilities: $7,000,000
'''
prompt = f"Analyze the following financial report and provide a summary with key metrics and risk factors:\n\n{financial_report}"
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}]
)
print("Financial report analysis:\n", response.choices[0].message.content) output
Financial report analysis: The financial report for Company XYZ shows a revenue of $10 million and a net income of $1.2 million, indicating a profit margin of 12%. Operating expenses are $5 million, which is 50% of revenue. The company has assets totaling $15 million and liabilities of $7 million, suggesting a healthy equity position. Key risk factors include monitoring operating expenses and liabilities growth.
Common variations
- Use
asynccalls withasynciofor concurrent report analysis. - Stream responses for large reports using
stream=Truein the API call. - Try different models like
claude-3-5-haiku-20241022for alternative perspectives.
import os
import asyncio
from openai import OpenAI
async def analyze_report_async(report_text: str):
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
prompt = f"Analyze this financial report:\n\n{report_text}"
response = await client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}]
)
print("Async analysis result:\n", response.choices[0].message.content)
financial_report = """Company XYZ Income Statement 2025 Revenue: $10M Net Income: $1.2M"""
asyncio.run(analyze_report_async(financial_report)) output
Async analysis result: The financial report indicates a strong revenue stream of $10 million with a net income of $1.2 million, reflecting a solid profit margin. Further analysis should consider expense trends and liabilities.
Troubleshooting
- If you get
AuthenticationError, verify yourOPENAI_API_KEYenvironment variable is set correctly. - For
RateLimitError, reduce request frequency or upgrade your plan. - If the model output is incomplete, increase
max_tokensor use streaming.
Key Takeaways
- Use
gpt-4o-minior similar LLMs to extract key financial metrics and insights from reports. - Automate report analysis with Python and the
OpenAI APIfor scalable workflows. - Leverage async and streaming API features for handling large or multiple reports efficiently.