How to beginner · 3 min read

AI for contract review

Quick answer
Use large language models (LLMs) like gpt-4o-mini to automate contract review by extracting key clauses, summarizing terms, and identifying risks. Implement this with the OpenAI Python SDK to parse contract text and generate structured insights 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.

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 shows how to load a contract text, send it to gpt-4o-mini for clause extraction, and print the key points.

python
import os
from openai import OpenAI

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

contract_text = '''\nThis Agreement is made between Acme Corp and Beta LLC. The term is 2 years with automatic renewal. Payment terms are net 30 days. Confidentiality must be maintained by both parties. Termination requires 60 days notice.\n'''

messages = [
    {"role": "system", "content": "You are a legal assistant that extracts key contract clauses."},
    {"role": "user", "content": f"Extract key clauses from this contract:\n{contract_text}"}
]

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

print("Extracted clauses:\n", response.choices[0].message.content)
output
Extracted clauses:
- Parties: Acme Corp and Beta LLC
- Term: 2 years with automatic renewal
- Payment terms: net 30 days
- Confidentiality: mutual obligation
- Termination: requires 60 days notice

Common variations

You can use asynchronous calls with asyncio for batch contract processing or switch to claude-3-5-haiku-20241022 for alternative LLMs. Streaming responses help with large contracts.

python
import os
import asyncio
from openai import OpenAI

async def extract_clauses_async(contract_text: str):
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    messages = [
        {"role": "system", "content": "You are a legal assistant that extracts key contract clauses."},
        {"role": "user", "content": f"Extract key clauses from this contract:\n{contract_text}"}
    ]
    response = await client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages,
        max_tokens=500
    )
    return response.choices[0].message.content

async def main():
    contract = """This Agreement is between Alpha Inc and Omega Ltd. The contract lasts 3 years. Payment is due within 45 days. Confidentiality applies. Termination requires 90 days notice."""
    clauses = await extract_clauses_async(contract)
    print("Extracted clauses (async):\n", clauses)

if __name__ == "__main__":
    asyncio.run(main())
output
Extracted clauses (async):
- Parties: Alpha Inc and Omega Ltd
- Term: 3 years
- Payment terms: due within 45 days
- Confidentiality: applies
- Termination: requires 90 days notice

Troubleshooting

  • If you get AuthenticationError, verify your OPENAI_API_KEY environment variable is set correctly.
  • If responses are incomplete, increase max_tokens or use streaming.
  • For noisy contract text, preprocess with OCR cleanup or text normalization before sending to the model.

Key Takeaways

  • Use gpt-4o-mini with the OpenAI Python SDK to extract contract clauses efficiently.
  • Set OPENAI_API_KEY in your environment to authenticate API calls securely.
  • Async calls and streaming improve performance for large or batch contract reviews.
  • Preprocess contract text to improve extraction accuracy and reduce noise.
  • Alternative models like claude-3-5-haiku-20241022 offer competitive legal NLP capabilities.
Verified 2026-04 · gpt-4o-mini, claude-3-5-haiku-20241022
Verify ↗