Writer SDK for Python
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
-
ModuleNotFoundError: No module named 'writerai'
cause The `writer-sdk` package is either not installed, or the import statement is incorrect.fixEnsure the library is installed with `pip install writer-sdk` and that your import statement is `from writerai import Writer`. -
writerai.APIStatusError: 401 Unauthorized
cause The provided API key is invalid, missing, or expired.fixVerify that your `WRITER_API_KEY` environment variable is set correctly with a valid API key from your Writer AI Studio account. Ensure there are no leading/trailing spaces or incorrect characters. -
writerai.APIConnectionError: Connection error
cause The client could not connect to the Writer API, possibly due to network issues, a timeout, or a firewall blocking the connection.fixCheck your internet connection, firewall settings, and ensure that `https://api.writer.com/v1` is accessible. Increase client timeout if necessary. -
AttributeError: 'Writer' object has no attribute 'some_method'
cause You are attempting to call an API method that does not exist in your installed SDK version, or the method name has changed in a newer version. This can also happen if a service/feature was deprecated.fixRefer to the official Writer SDK documentation for the correct API calls for your installed version. Upgrade your `writer-sdk` to the latest version to access new features, and review changelogs for breaking changes if upgrading across major versions.
Warnings
- breaking Version 3.0.0-rc1 deprecates several API features, including AI Detection, Medical Comprehend, and Context-Aware Text Splitting. Applications relying on these specific endpoints will break upon upgrade to v3.0.0.
- breaking The 2.0.0 release introduced breaking changes to the underlying models used in chat completion. This primarily affects users directly importing specific types related to chat completion models.
- gotcha The `writer-sdk` library should not be confused with the `writer` PyPI package, which corresponds to `writer-framework`. They are distinct libraries with different purposes, where `writer-framework` is for building UI-based AI applications.
- gotcha API keys should not be hard-coded in your application. The library automatically infers the API key from the `WRITER_API_KEY` environment variable.
Install
-
pip install writer-sdk -
pip install --pre writer-sdk
Imports
- Writer
from writer_sdk import Writer
from writerai import Writer
Quickstart
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}")