How to Use the WaveSpeedAI Python SDK
The WaveSpeedAI Python SDK provides a simple way to integrate AI image and video generation into your Python applications. This guide covers everything you need to get started.
Prerequisites
Before you begin, make sure you have:
- Python 3.8+ installed on your machine
- A WaveSpeedAI API key from wavespeed.ai/settings/api-keys
Installation
Install the SDK using pip:
pip install wavespeed
Setting Up Authentication
The SDK needs your API key to authenticate requests. You have two options:
Option 1: Environment Variable (Recommended)
Set the WAVESPEED_API_KEY environment variable:
export WAVESPEED_API_KEY="your-api-key-here"
Then use the SDK directly:
import wavespeed
output = wavespeed.run(
"wavespeed-ai/z-image/turbo",
{"prompt": "Cat"}
)
Option 2: Pass API Key Directly
Import the Client class and pass your API key to the constructor:
from wavespeed import Client
client = Client(api_key="your-api-key-here")
Generating Your First Image
Here’s a complete example that generates an image using Z-Image Turbo:
import wavespeed
output = wavespeed.run(
"wavespeed-ai/z-image/turbo",
{"prompt": "A serene mountain landscape at sunset with golden light"}
)
print(output["outputs"][0]) # URL to the generated image
The run() function handles the entire workflow: submitting the request, polling for completion, and returning the result.
Uploading Files
For workflows that require input images (like image-to-video), use the upload() function to get a URL that WaveSpeedAI can access:
import wavespeed
# Upload a local image file
image_url = wavespeed.upload("./my-image.png")
# Use the uploaded image for video generation
video = wavespeed.run(
"wavespeed-ai/wan-2.1/image-to-video",
{
"image": image_url,
"prompt": "Camera slowly zooms in while clouds move in the background"
}
)
print(video["outputs"][0]) # URL to the generated video
Configuration Options
Client Options
Configure retry behavior when initializing the client:
from wavespeed import Client
client = Client(
api_key="your-api-key",
max_retries=3, # Max retries for failed requests
max_connection_retries=5, # Max retries for connection errors
retry_interval=1.0 # Seconds between retries
)
Run Options
Configure individual run() calls:
import wavespeed
output = wavespeed.run(
"wavespeed-ai/z-image/turbo",
{"prompt": "A cute orange cat wearing a tiny hat"},
timeout=60.0, # Max seconds to wait for completion
poll_interval=0.5, # Seconds between status checks
enable_sync_mode=True # Use synchronous mode if available
)
Working with Different Models
Text-to-Image
Generate images from text descriptions:
import wavespeed
output = wavespeed.run(
"wavespeed-ai/z-image/turbo",
{
"prompt": "A futuristic cityscape with flying cars and neon lights",
"size": "1024x1024"
}
)
Image-to-Video
Transform static images into videos:
import wavespeed
image_url = wavespeed.upload("./landscape.jpg")
video = wavespeed.run(
"wavespeed-ai/wan-2.1/image-to-video",
{
"image": image_url,
"prompt": "Gentle wind blowing through the trees"
}
)
Text-to-Video
Generate videos directly from text:
import wavespeed
video = wavespeed.run(
"wavespeed-ai/wan-2.1/t2v-480p",
{"prompt": "A golden retriever running through a field of flowers"}
)
Async Support
The SDK supports async/await for non-blocking operations:
import asyncio
import wavespeed
async def generate_image():
output = await wavespeed.async_run(
"wavespeed-ai/z-image/turbo",
{"prompt": "An astronaut riding a horse on Mars"}
)
return output["outputs"][0]
# Run the async function
image_url = asyncio.run(generate_image())
print(image_url)
Error Handling
For production applications, configure retries and handle errors gracefully:
from wavespeed import Client
client = Client(
max_retries=3,
max_connection_retries=5,
retry_interval=1.0
)
try:
output = client.run(
"wavespeed-ai/z-image/turbo",
{"prompt": "A beautiful sunset over the ocean"},
timeout=120.0
)
print("Generated:", output["outputs"][0])
except Exception as e:
print(f"Generation failed: {e}")
Resources
- GitHub Repository: github.com/WaveSpeedAI/wavespeed-python
- PyPI Package: pypi.org/project/wavespeed
- API Documentation: docs.wavespeed.ai
- Model Library: wavespeed.ai/models
Start building with WaveSpeedAI today and bring AI-powered image and video generation to your Python applications.





