Writer SDK for Python

2.4.0 · active · verified Thu Apr 16

The writer-sdk is the official Python library for the Writer API, providing programmatic access to Writer's generative AI capabilities. It enables Python 3.9+ applications to integrate features like chat completions, text generation, and knowledge graph interactions. The library is actively maintained with frequent updates, including a recent release candidate for version 3.0.0.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Writer client and make a basic chat completion request. It emphasizes using the `WRITER_API_KEY` environment variable for secure authentication.

import os
from writerai import Writer

# Initialize the Writer client.
# The API key will be inferred from the `WRITER_API_KEY` environment variable.
# Ensure you have 'export WRITER_API_KEY="your-api-key"' in your environment
# or pass it explicitly: client = Writer(api_key="your-api-key")
client = Writer(api_key=os.environ.get("WRITER_API_KEY", ""))

if not client.api_key:
    print("WARNING: WRITER_API_KEY environment variable not set. Please set it for authentication.")
    print("You can obtain your API key from your Writer AI Studio account settings.")
else:
    try:
        response = client.chat.chat(
            messages=[{
                "content": "Write a short poem about Python",
                "role": 'user'
            }],
            model="palmyra-x5"
        )
        print(response.choices[0].message.content)
    except Exception as e:
        print(f"An error occurred: {e}")

view raw JSON →