How to classify text with AI in Python
Direct answer
Use the OpenAI Python SDK to send your text as a prompt to a classification model like gpt-4o and parse the response for the predicted class.
Setup
Install
pip install openai Env vars
OPENAI_API_KEY Imports
import os
from openai import OpenAI
import json Examples
inI love this product, it works great!
outPositive
inThe movie was boring and too long.
outNegative
inThe weather forecast says it will rain tomorrow, so bring an umbrella.
outNeutral
Integration steps
- Install the OpenAI Python SDK and set your API key in the environment variable OPENAI_API_KEY.
- Import OpenAI and initialize the client with your API key from os.environ.
- Create a prompt that instructs the model to classify the input text into categories.
- Call client.chat.completions.create with the classification prompt and the gpt-4o model.
- Extract the classification label from response.choices[0].message.content.
Full code
import os
from openai import OpenAI
import json
# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Function to classify text
def classify_text(text: str) -> str:
prompt = (
"Classify the sentiment of the following text as Positive, Negative, or Neutral.\n"
f"Text: {text}\n"
"Sentiment:"
)
messages = [{"role": "user", "content": prompt}]
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
max_tokens=10,
temperature=0
)
classification = response.choices[0].message.content.strip()
return classification
if __name__ == "__main__":
samples = [
"I love this product, it works great!",
"The movie was boring and too long.",
"The weather forecast says it will rain tomorrow, so bring an umbrella."
]
for text in samples:
label = classify_text(text)
print(f"Input: {text}\nClassification: {label}\n") output
Input: I love this product, it works great! Classification: Positive Input: The movie was boring and too long. Classification: Negative Input: The weather forecast says it will rain tomorrow, so bring an umbrella. Classification: Neutral
API trace
Request
{"model": "gpt-4o", "messages": [{"role": "user", "content": "Classify the sentiment of the following text as Positive, Negative, or Neutral.\nText: I love this product, it works great!\nSentiment:"}], "max_tokens": 10, "temperature": 0} Response
{"choices": [{"message": {"content": "Positive"}}], "usage": {"prompt_tokens": 30, "completion_tokens": 2, "total_tokens": 32}} Extract
response.choices[0].message.content.strip()Variants
Streaming classification ›
Use streaming when you want to display classification results as they are generated for better user experience.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
def classify_text_stream(text: str) -> None:
prompt = (
"Classify the sentiment of the following text as Positive, Negative, or Neutral.\n"
f"Text: {text}\n"
"Sentiment:"
)
messages = [{"role": "user", "content": prompt}]
stream = client.chat.completions.create(
model="gpt-4o",
messages=messages,
max_tokens=10,
temperature=0,
stream=True
)
print("Classification: ", end="", flush=True)
for chunk in stream:
delta = chunk.choices[0].delta.content or ""
print(delta, end="", flush=True)
print()
if __name__ == "__main__":
classify_text_stream("I love this product, it works great!") Async classification ›
Use async when integrating classification in an asynchronous application or to handle multiple requests concurrently.
import os
import asyncio
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
async def classify_text_async(text: str) -> str:
prompt = (
"Classify the sentiment of the following text as Positive, Negative, or Neutral.\n"
f"Text: {text}\n"
"Sentiment:"
)
messages = [{"role": "user", "content": prompt}]
response = await client.chat.completions.create(
model="gpt-4o",
messages=messages,
max_tokens=10,
temperature=0
)
return response.choices[0].message.content.strip()
async def main():
label = await classify_text_async("The movie was boring and too long.")
print(f"Classification: {label}")
if __name__ == "__main__":
asyncio.run(main()) Use Anthropic Claude for classification ›
Use Anthropic Claude models if you prefer Claude's style or want an alternative to OpenAI for text classification.
import os
import anthropic
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
def classify_text_claude(text: str) -> str:
system_prompt = "You are a helpful assistant that classifies text sentiment as Positive, Negative, or Neutral."
user_message = f"Classify the sentiment of this text:\n{text}"
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=10,
system=system_prompt,
messages=[{"role": "user", "content": user_message}]
)
return response.content.strip()
if __name__ == "__main__":
print(classify_text_claude("I love this product, it works great!")) Performance
Latency~800ms for gpt-4o non-streaming classification
Cost~$0.002 per 100 tokens for gpt-4o
Rate limitsTier 1: 500 requests per minute / 30,000 tokens per minute
- Keep prompts concise to reduce token usage.
- Use max_tokens to limit output length.
- Set temperature=0 to get deterministic classification labels.
| Approach | Latency | Cost/call | Best for |
|---|---|---|---|
| OpenAI GPT-4o standard | ~800ms | ~$0.002 | Reliable, general-purpose classification |
| OpenAI GPT-4o streaming | ~800ms (streamed) | ~$0.002 | Better UX for interactive apps |
| Anthropic Claude-3-5-sonnet | ~900ms | ~$0.0018 | Alternative style and tone for classification |
Quick tip
Use a clear, explicit prompt instructing the model to output only the classification label to simplify parsing.
Common mistake
Beginners often forget to set temperature=0 for classification tasks, causing inconsistent or verbose outputs.