How to Integrate OpenAI for lending with Pinecone for production AI

By Cyprian AaronsUpdated 2026-04-21
openai-for-lendingpineconeproduction-ai

Combining OpenAI for lending with Pinecone gives you a practical pattern for production AI agents that need both reasoning and retrieval. In lending workflows, that usually means answering questions from policy docs, pulling borrower history, and grounding every response in the right internal context instead of guessing.

The real win is speed plus control: OpenAI handles the language and decision support, while Pinecone stores embeddings for fast retrieval across loan policies, underwriting guides, KYC notes, and servicing records.

Prerequisites

  • Python 3.10+
  • An OpenAI API key with access to the model you plan to use
  • A Pinecone account and API key
  • A Pinecone index created with the right embedding dimension for your OpenAI embedding model
  • pip installed
  • Basic familiarity with vector search and REST-style APIs

Install the SDKs:

pip install openai pinecone

Set environment variables:

export OPENAI_API_KEY="your-openai-key"
export PINECONE_API_KEY="your-pinecone-key"
export PINECONE_INDEX_NAME="lending-docs"

Integration Steps

  1. Initialize both clients

Start by wiring up the OpenAI and Pinecone clients in one module. Keep this in a shared config file so your agent services use the same connection settings.

import os
from openai import OpenAI
from pinecone import Pinecone

openai_client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
pc = Pinecone(api_key=os.environ["PINECONE_API_KEY"])

index_name = os.environ["PINECONE_INDEX_NAME"]
index = pc.Index(index_name)
  1. Create embeddings for lending content

Use OpenAI embeddings to turn lending policy text into vectors. In production, chunk documents before embedding; here I’m keeping it simple so the integration is clear.

docs = [
    {
        "id": "policy-001",
        "text": "Debt-to-income ratio must not exceed 43% for standard mortgage approval.",
        "metadata": {"type": "policy", "topic": "mortgage"}
    },
    {
        "id": "policy-002",
        "text": "Applicants must provide two recent payslips and a valid government ID.",
        "metadata": {"type": "policy", "topic": "kyc"}
    }
]

embeddings = []
for doc in docs:
    resp = openai_client.embeddings.create(
        model="text-embedding-3-small",
        input=doc["text"]
    )
    vector = resp.data[0].embedding
    embeddings.append((doc["id"], vector, doc["metadata"]))
  1. Upsert vectors into Pinecone

Now store those vectors in Pinecone with metadata so you can filter by document type later.

vectors = []
for doc, (_, vector, metadata) in zip(docs, embeddings):
    vectors.append({
        "id": doc["id"],
        "values": vector,
        "metadata": {
            **metadata,
            "text": doc["text"]
        }
    })

index.upsert(vectors=vectors)
print("Upsert complete")
  1. Query Pinecone from your agent

When a user asks a lending question, embed the query with OpenAI, then run semantic search in Pinecone to fetch the most relevant policy snippets.

query = "What documents do I need for a mortgage application?"

query_resp = openai_client.embeddings.create(
    model="text-embedding-3-small",
    input=query
)
query_vector = query_resp.data[0].embedding

results = index.query(
    vector=query_vector,
    top_k=3,
    include_metadata=True
)

for match in results.matches:
    print(match.id, match.score, match.metadata["text"])
  1. Generate a grounded answer with OpenAI

Take the retrieved context and pass it into an OpenAI chat completion call. This is the production pattern: retrieval first, generation second.

context_chunks = [
    f"- {m.metadata['text']}"
    for m in results.matches
]

messages = [
    {
        "role": "system",
        "content": (
            "You are a lending assistant. Answer only using the provided context. "
            "If the answer is not in context, say you don't have enough information."
        )
    },
    {
        "role": "user",
        "content": (
            f"Question: {query}\n\n"
            f"Context:\n{chr(10).join(context_chunks)}"
        )
    }
]

response = openai_client.chat.completions.create(
    model="gpt-4o-mini",
    messages=messages,
    temperature=0.2
)

print(response.choices[0].message.content)

Testing the Integration

Run an end-to-end test that inserts one policy snippet, retrieves it, and generates an answer.

test_query = "What ID is required for loan application?"

q_emb = openai_client.embeddings.create(
    model="text-embedding-3-small",
    input=test_query
).data[0].embedding

res = index.query(vector=q_emb, top_k=1, include_metadata=True)

context = res.matches[0].metadata["text"]

answer = openai_client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "Answer using only context."},
        {"role": "user", "content": f"Context: {context}\n\nQuestion: {test_query}"}
    ],
).choices[0].message.content

print("Retrieved:", context)
print("Answer:", answer)

Expected output:

Retrieved: Applicants must provide two recent payslips and a valid government ID.
Answer: The applicant must provide two recent payslips and a valid government ID.

Real-World Use Cases

  • Loan officer copilot: Answer product and policy questions from internal lending docs without exposing raw databases to the model.
  • Underwriting assistant: Retrieve borrower-specific notes, compare them against policy thresholds, and draft a recommendation for human review.
  • Servicing support bot: Search payment plans, hardship policies, and collections scripts to generate consistent customer responses.

This pattern scales because each tool does one job well. OpenAI handles language and reasoning; Pinecone handles retrieval at low latency; your application layer enforces business rules, audit logging, and access control.


Keep learning

By Cyprian Aarons, AI Consultant at Topiax.

Want the complete 8-step roadmap?

Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.

Get the Starter Kit

Related Guides