How to Integrate LangChain for healthcare with PostgreSQL for startups
LangChain for healthcare gives you the orchestration layer for clinical workflows: retrieval, tool calling, and agent logic around patient-facing or provider-facing tasks. PostgreSQL gives you the durable system of record for patient metadata, audit logs, conversation state, and structured outputs. Put them together and you get an AI agent that can answer clinical questions from approved sources while persisting every interaction in a database you control.
Prerequisites
- •Python 3.10+
- •A PostgreSQL instance running locally or in your cloud environment
- •A database user with permissions to create tables and write rows
- •
OPENAI_API_KEYor another model provider key supported by LangChain - •Access to your healthcare documents or approved knowledge base
- •Installed packages:
- •
langchain - •
langchain-community - •
langchain-openai - •
psycopg2-binary - •
sqlalchemy
- •
pip install langchain langchain-community langchain-openai psycopg2-binary sqlalchemy
Integration Steps
- •Set up PostgreSQL connection details.
Use SQLAlchemy for the connection string and create a simple table to store agent interactions. In healthcare systems, this table becomes your audit trail.
from sqlalchemy import create_engine, text
DATABASE_URL = "postgresql+psycopg2://postgres:password@localhost:5432/healthcare_ai"
engine = create_engine(DATABASE_URL)
with engine.begin() as conn:
conn.execute(text("""
CREATE TABLE IF NOT EXISTS agent_audit_log (
id SERIAL PRIMARY KEY,
session_id TEXT NOT NULL,
user_query TEXT NOT NULL,
agent_response TEXT NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
)
"""))
- •Load healthcare context into LangChain.
For startup use cases, keep this simple: load approved PDFs, guidelines, or policy docs into a vector store. LangChain’s PyPDFLoader, RecursiveCharacterTextSplitter, and Chroma are enough to get a production-shaped retrieval pipeline running.
from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings
loader = PyPDFLoader("clinical_guidelines.pdf")
documents = loader.load()
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
chunks = splitter.split_documents(documents)
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
vectorstore = Chroma.from_documents(
documents=chunks,
embedding=embeddings,
persist_directory="./chroma_healthcare"
)
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
- •Build the LangChain healthcare QA chain.
Use a chat model plus retrieval so the agent answers from approved sources instead of hallucinating. The standard pattern is create_retrieval_chain with a retriever-backed document chain.
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain.chains.retrieval import create_retrieval_chain
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
prompt = ChatPromptTemplate.from_messages([
("system", "You are a healthcare assistant. Answer only from the provided context."),
("human", "{input}")
])
document_chain = create_stuff_documents_chain(llm, prompt)
qa_chain = create_retrieval_chain(retriever, document_chain)
- •Persist every interaction in PostgreSQL.
This is where PostgreSQL earns its keep. After each response, write the query and answer to the audit table so you can review usage, trace issues, and support compliance workflows.
def ask_and_log(session_id: str, question: str):
result = qa_chain.invoke({"input": question})
answer = result["answer"]
with engine.begin() as conn:
conn.execute(
text("""
INSERT INTO agent_audit_log (session_id, user_query, agent_response)
VALUES (:session_id, :user_query, :agent_response)
"""),
{
"session_id": session_id,
"user_query": question,
"agent_response": answer,
}
)
return answer
response = ask_and_log("session-001", "What are the first-line recommendations for adult hypertension?")
print(response)
- •Add structured patient-safe outputs.
For startup systems, don’t return raw model text if you can avoid it. Use structured output so downstream services can consume normalized fields like summary, risk level, and escalation flag.
from pydantic import BaseModel
class ClinicalResponse(BaseModel):
summary: str
needs_escalation: bool
structured_llm = llm.with_structured_output(ClinicalResponse)
structured_result = structured_llm.invoke(
"Summarize when chest pain requires immediate escalation."
)
print(structured_result.summary)
print(structured_result.needs_escalation)
Testing the Integration
Run one query end-to-end and confirm two things: the model returns an answer and PostgreSQL stores the row.
test_answer = ask_and_log("session-test", "How should a clinician triage mild dehydration in adults?")
print("ANSWER:", test_answer)
with engine.connect() as conn:
rows = conn.execute(
text("""
SELECT session_id, user_query, agent_response
FROM agent_audit_log
WHERE session_id = 'session-test'
ORDER BY created_at DESC
LIMIT 1
""")
).fetchall()
print(rows[0])
Expected output:
ANSWER: [model response grounded in your loaded healthcare documents]
('session-test', 'How should a clinician triage mild dehydration in adults?', '[stored response]')
Real-World Use Cases
- •Clinical support agents that answer staff questions from approved hospital policies and log every interaction for auditability.
- •Patient intake assistants that collect symptoms, summarize them into structured fields, and store them in PostgreSQL for downstream triage.
- •Prior authorization helpers that retrieve payer rules from indexed documents and persist decision traces for operations review.
If you’re building this for a startup, keep the boundaries tight: LangChain handles reasoning and retrieval, PostgreSQL handles persistence and reporting. That split keeps your system explainable enough for healthcare workflows without turning every request into an ad hoc script.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
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