How to beginner · 3 min read

How to use ChatGPT for writing

Quick answer
Use the gpt-4o model via OpenAI's API to generate writing content by sending prompts with client.chat.completions.create(). Provide clear instructions in the messages parameter to get tailored text output for articles, stories, or any writing task.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the OpenAI Python SDK and set your API key as an environment variable for secure access.

bash
pip install openai>=1.0

Step by step

This example shows how to use the gpt-4o model to generate a short article based on a writing prompt.

python
import os
from openai import OpenAI

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

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Write a 200-word article about the benefits of remote work."}]
)

print(response.choices[0].message.content)
output
Remote work offers numerous benefits including increased flexibility, improved work-life balance, and reduced commuting time. Employees can tailor their work environment to maximize productivity and comfort. Companies benefit from access to a wider talent pool and lower overhead costs. Additionally, remote work can lead to higher job satisfaction and retention rates. Overall, embracing remote work fosters a more adaptable and efficient workforce.

Common variations

  • Use gpt-4o-mini for faster, lower-cost responses.
  • Implement async calls with asyncio for concurrent requests.
  • Stream responses for real-time output using the stream=True parameter.
  • Switch to other models like claude-3-5-sonnet-20241022 for advanced coding or creative writing.
python
import os
import asyncio
from openai import OpenAI

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

async def generate_text():
    response = await client.chat.completions.acreate(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": "Write a poem about spring."}]
    )
    print(response.choices[0].message.content)

asyncio.run(generate_text())
output
Spring whispers softly, blooms awake anew,
Colors burst in gardens kissed by dew.
Gentle breezes dance through skies so blue,
Nature's song begins, fresh and true.

Troubleshooting

  • If you get authentication errors, verify your OPENAI_API_KEY environment variable is set correctly.
  • For rate limit errors, reduce request frequency or upgrade your API plan.
  • If responses are incomplete, increase max_tokens parameter.
  • Check network connectivity if requests time out.

Key Takeaways

  • Use the gpt-4o model with OpenAI's Python SDK for effective writing generation.
  • Set your API key securely via environment variables to avoid credential leaks.
  • Leverage async and streaming features for scalable and real-time writing applications.
  • Adjust model choice and parameters like max_tokens to fit your writing needs.
Verified 2026-04 · gpt-4o, gpt-4o-mini, claude-3-5-sonnet-20241022
Verify ↗