How to Beginner to Intermediate · 3 min read

AI in medical diagnosis explained

Quick answer
AI in medical diagnosis uses machine learning and large language models (LLMs) like gpt-4o to analyze patient data, medical images, and clinical notes to assist doctors in identifying diseases and recommending treatments. These models improve diagnostic accuracy by detecting patterns beyond human capability and providing explainable insights.

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 to access AI models for medical diagnosis assistance.

bash
pip install openai
output
Collecting openai
  Downloading openai-1.x.x-py3-none-any.whl (xx kB)
Installing collected packages: openai
Successfully installed openai-1.x.x

Step by step

This example demonstrates how to use gpt-4o to analyze a patient's symptoms and medical history to suggest possible diagnoses.

python
import os
from openai import OpenAI

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

patient_data = (
    "Patient is a 45-year-old male with persistent cough, fever, and shortness of breath. "
    "History of smoking and recent travel to a high-risk area."
)

messages = [
    {"role": "system", "content": "You are a medical diagnosis assistant. Provide possible diagnoses based on patient data."},
    {"role": "user", "content": patient_data}
]

response = client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    max_tokens=256
)

print("Possible diagnoses:", response.choices[0].message.content)
output
Possible diagnoses: Pneumonia, bronchitis, or COVID-19 infection are possible causes given the symptoms and history. Recommend further tests such as chest X-ray and PCR testing.

Common variations

You can use asynchronous calls for scalable applications or switch to other models like claude-3-5-sonnet-20241022 for alternative AI perspectives. Streaming responses enable real-time diagnosis suggestions.

python
import asyncio
import os
from openai import OpenAI

async def async_diagnosis():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    patient_data = (
        "Patient is a 60-year-old female with chest pain and dizziness. "
        "History of hypertension and diabetes."
    )
    messages = [
        {"role": "system", "content": "You are a medical diagnosis assistant."},
        {"role": "user", "content": patient_data}
    ]
    response = await client.chat.completions.create(
        model="claude-3-5-sonnet-20241022",
        messages=messages,
        max_tokens=256
    )
    print("Possible diagnoses:", response.choices[0].message.content)

asyncio.run(async_diagnosis())
output
Possible diagnoses: Angina, myocardial infarction, or arrhythmia are potential diagnoses. Recommend ECG and blood tests for confirmation.

Troubleshooting

  • If you receive authentication errors, verify your OPENAI_API_KEY environment variable is set correctly.
  • For incomplete responses, increase max_tokens or check model usage limits.
  • If latency is high, consider streaming responses or using smaller models like gpt-4o-mini.

Key Takeaways

  • Use gpt-4o or claude-3-5-sonnet-20241022 to assist medical diagnosis by analyzing patient data.
  • Set up environment variables and install the openai package for API access.
  • Async and streaming calls improve responsiveness in clinical applications.
  • Increase max_tokens for detailed diagnostic explanations.
  • Always validate AI suggestions with clinical tests and expert review.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022, gpt-4o-mini
Verify ↗