路径参数
如果创建响应中包含 poll_url,建议优先调用该特定 URL 进行轮询。某些图像任务可能会在 /v1/tasks/{id} 下显示 poll_url,而不是图像特定的状态路径。你也可以用 /v1/images/{id} 轮询 ldtask_... 这类公开图片任务 ID。这个兼容 alias 会复用与 /v1/images/generations/{id} 完全相同的鉴权、ownership 检查、计费 header、终态快照和响应形状。如果该任务已不存在或无法再从公开任务记录中查询到,TokenLab 会返回 async_task_not_found,并附带消息 Task not found or no longer available.
任务状态:pending、processing、completed 或 failed。
生成的图像数组(当 status 为 completed 时填充)。每个对象包含:
url (string): 生成图像的 URL
revised_prompt (string): 用于生成的提示词
错误信息(仅在 status 为 failed 时存在)。
curl "https://api.tokenlab.sh/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
-H "Authorization: Bearer sk-your-api-key"
import requests
response = requests.get(
"https://api.tokenlab.sh/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
headers={"Authorization": "Bearer sk-your-api-key"}
)
data = response.json()
print(f"Status: {data['status']}")
if data["status"] == "completed":
print(f"Image URL: {data['data'][0]['url']}")
const response = await fetch(
'https://api.tokenlab.sh/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
{
headers: {
'Authorization': 'Bearer sk-your-api-key'
}
}
);
const data = await response.json();
console.log(`Status: ${data.status}`);
if (data.status === 'completed') {
console.log(`Image URL: ${data.data[0].url}`);
}
{
"created": 1706000000,
"id": "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"task_id": "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"poll_url": "/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"status": "pending",
"data": [
{
"url": "",
"revised_prompt": "a beautiful sunset over mountains"
}
]
}
{
"created": 1706000000,
"id": "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"task_id": "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"poll_url": "/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"status": "completed",
"data": [
{
"url": "https://cdn.example.com/generated/image-abc123.png",
"revised_prompt": "a beautiful sunset over mountains with vibrant orange and purple colors"
}
]
}
{
"created": 1706000000,
"id": "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"task_id": "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"poll_url": "/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"status": "failed",
"error": "Content policy violation",
"data": []
}
轮询最佳实践
建议轮询间隔:3-5 秒。大多数图像生成任务在 30-120 秒内完成,具体取决于模型和路由的提供商路径。
import requests
import time
def poll_image_task(task_id, api_key, max_wait=300, interval=3):
"""Poll for image generation result with timeout."""
url = f"https://api.tokenlab.sh/v1/tasks/{task_id}"
headers = {"Authorization": f"Bearer {api_key}"}
start_time = time.time()
while time.time() - start_time < max_wait:
response = requests.get(url, headers=headers)
data = response.json()
if data["status"] == "completed":
return data["data"][0]["url"]
elif data["status"] == "failed":
raise Exception(data.get("error", "Generation failed"))
time.sleep(interval)
raise TimeoutError(f"Task {task_id} did not complete within {max_wait}s")
# Usage
image_url = poll_image_task("ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "sk-your-api-key")
print(f"Generated image: {image_url}")