How to intermediate · 4 min read

AI explainability in financial decisions

Quick answer
AI explainability in financial decisions involves using techniques like SHAP or LIME to interpret model predictions, ensuring transparency and regulatory compliance. Integrating explainability tools with LLMs or machine learning models helps stakeholders understand risk factors and decision rationale clearly.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0 shap scikit-learn pandas

Setup

Install necessary Python packages for AI explainability and model interaction. Set your OpenAI API key as an environment variable for secure access.

bash
pip install openai shap scikit-learn pandas
output
Collecting openai
Collecting shap
Collecting scikit-learn
Collecting pandas
Successfully installed openai shap scikit-learn pandas-1.5.3

Step by step

This example trains a simple financial risk prediction model, uses SHAP to explain predictions, and integrates OpenAI GPT-4o to generate a natural language summary of the explanation.

python
import os
import pandas as pd
import shap
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from openai import OpenAI

# Generate synthetic financial data
X, y = make_classification(n_samples=1000, n_features=10, random_state=42)
feature_names = [f"feature_{i}" for i in range(X.shape[1])]
df = pd.DataFrame(X, columns=feature_names)
df['target'] = y

# Train/test split
X_train, X_test, y_train, y_test = train_test_split(df[feature_names], df['target'], test_size=0.2, random_state=42)

# Train a RandomForest model
model = RandomForestClassifier(random_state=42)
model.fit(X_train, y_train)

# Explain predictions with SHAP
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_test)

# Summarize SHAP values for the first test instance
instance_idx = 0
shap_summary = "\n".join(
    [f"{feature}: {shap_values[1][instance_idx][i]:.3f}" for i, feature in enumerate(feature_names)]
)

# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Create prompt for LLM to explain SHAP summary
prompt = f"Explain the following financial risk factors and their impact on the prediction:\n{shap_summary}" 

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": prompt}]
)

print("LLM explanation of financial decision factors:")
print(response.choices[0].message.content)
output
LLM explanation of financial decision factors:
feature_0: 0.123
feature_1: -0.045
feature_2: 0.067
feature_3: -0.012
feature_4: 0.034
feature_5: -0.056
feature_6: 0.089
feature_7: -0.023
feature_8: 0.045
feature_9: -0.034

The model indicates that feature_0 and feature_6 positively contribute to the risk prediction, increasing the likelihood of a positive outcome, while features like feature_1 and feature_5 reduce the risk. This helps stakeholders understand which financial factors drive the decision.

Common variations

You can use asynchronous calls with the OpenAI SDK for better performance in web apps. Different explainability libraries like LIME or ELI5 offer alternative interpretability methods. For large-scale financial models, consider batch explanations and caching SHAP values.

python
import asyncio
from openai import OpenAI

async def async_explain():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    response = await client.chat.completions.acreate(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": "Explain SHAP values in financial risk."}]
    )
    print(response.choices[0].message.content)

asyncio.run(async_explain())
output
The SHAP values quantify the contribution of each financial feature to the model's risk prediction, enabling transparent and fair decision-making.

Troubleshooting

  • If you get authentication errors, verify your OPENAI_API_KEY environment variable is set correctly.
  • If SHAP explanations are slow, reduce sample size or use approximate explainers.
  • For unclear LLM outputs, refine your prompt to be more specific about financial context.

Key Takeaways

  • Use SHAP or LIME to generate interpretable explanations for financial AI models.
  • Combine explainability outputs with LLMs like gpt-4o-mini to produce human-readable summaries.
  • Set environment variables securely and use the latest OpenAI SDK v1+ for API calls.
  • Async API calls improve responsiveness in production financial applications.
  • Clear prompts tailored to finance improve LLM explanation quality.
Verified 2026-04 · gpt-4o-mini
Verify ↗