How to summarize text with AI in Python
Direct answer
Use the
OpenAI Python SDK to call client.chat.completions.create with a summarization prompt and extract the summary from response.choices[0].message.content.Setup
Install
pip install openai Env vars
OPENAI_API_KEY Imports
import os
from openai import OpenAI Examples
inSummarize: OpenAI develops powerful AI models that help developers build intelligent applications.
outOpenAI creates advanced AI models to enable intelligent app development.
inSummarize: The Python programming language is widely used for AI due to its simplicity and rich ecosystem.
outPython is popular for AI because of its ease of use and extensive libraries.
inSummarize: This text is very short.
outThe text is brief.
Integration steps
- Install the OpenAI Python SDK and set the OPENAI_API_KEY environment variable.
- Import
OpenAIand initialize the client with the API key fromos.environ. - Create a chat completion request with a model like
gpt-4oand a message prompting summarization. - Send the request and receive the response object.
- Extract the summary text from
response.choices[0].message.content. - Print or use the summary as needed.
Full code
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
text_to_summarize = (
"OpenAI develops powerful AI models that help developers build intelligent applications."
)
messages = [
{"role": "user", "content": f"Summarize the following text concisely:\n\n{text_to_summarize}"}
]
response = client.chat.completions.create(
model="gpt-4o",
messages=messages
)
summary = response.choices[0].message.content
print("Summary:", summary) output
Summary: OpenAI creates advanced AI models to enable intelligent app development.
API trace
Request
{"model": "gpt-4o", "messages": [{"role": "user", "content": "Summarize the following text concisely:\n\nOpenAI develops powerful AI models that help developers build intelligent applications."}]} Response
{"choices": [{"message": {"content": "OpenAI creates advanced AI models to enable intelligent app development."}}], "usage": {"prompt_tokens": 30, "completion_tokens": 15, "total_tokens": 45}} Extract
response.choices[0].message.contentVariants
Streaming Summarization ›
Use streaming to display the summary as it is generated for better user experience with longer texts.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
text_to_summarize = (
"OpenAI develops powerful AI models that help developers build intelligent applications."
)
messages = [
{"role": "user", "content": f"Summarize the following text concisely:\n\n{text_to_summarize}"}
]
stream = client.chat.completions.create(
model="gpt-4o",
messages=messages,
stream=True
)
print("Summary:", end=" ")
for chunk in stream:
delta = chunk.choices[0].delta.content or ""
print(delta, end="", flush=True)
print() Async Summarization ›
Use async when integrating summarization into asynchronous applications or web servers.
import os
import asyncio
from openai import OpenAI
async def summarize_text(text: str):
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
messages = [{"role": "user", "content": f"Summarize the following text concisely:\n\n{text}"}]
response = await client.chat.completions.acreate(model="gpt-4o", messages=messages)
return response.choices[0].message.content
async def main():
text = "OpenAI develops powerful AI models that help developers build intelligent applications."
summary = await summarize_text(text)
print("Summary:", summary)
asyncio.run(main()) Using Claude Model for Summarization ›
Use Anthropic Claude models if you prefer Claude's style or have an Anthropic API key.
import os
from anthropic import Anthropic
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
text_to_summarize = (
"OpenAI develops powerful AI models that help developers build intelligent applications."
)
prompt = f"Summarize the following text concisely:\n\n{text_to_summarize}"
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=200,
system="You are a helpful assistant.",
messages=[{"role": "user", "content": prompt}]
)
summary = response.content[0].text
print("Summary:", summary) Performance
Latency~800ms for <code>gpt-4o</code> non-streaming summarization
Cost~$0.002 for a 500-token summarization call on <code>gpt-4o</code>
Rate limitsTier 1: 500 requests per minute / 30,000 tokens per minute
- Keep input text concise to reduce prompt tokens.
- Use shorter model variants like <code>gpt-4o-mini</code> for cheaper summarization.
- Avoid unnecessary system or assistant messages to save tokens.
| Approach | Latency | Cost/call | Best for |
|---|---|---|---|
Standard gpt-4o call | ~800ms | ~$0.002 | High-quality, general summarization |
Streaming gpt-4o | ~800ms initial + streaming | ~$0.002 | Long texts with real-time output |
Async gpt-4o | ~800ms | ~$0.002 | Integration in async apps |
Anthropic claude-3-5-sonnet-20241022 | ~900ms | Check Anthropic pricing | Claude-style summaries |
Quick tip
Frame your prompt clearly with instructions like 'Summarize the following text concisely' to get focused summaries.
Common mistake
Beginners often forget to extract the summary from <code>response.choices[0].message.content</code> and instead print the entire response object.