Skip to main content

Task Status

tasks.get(job_id)

Get current task status:
task = client.tasks.get(job_id="job-123")
print(f"Status: {task.status}")

tasks.wait_for_completion(job_id, timeout=300, poll_interval=3, on_progress=None)

Wait for task completion:
result = client.tasks.wait_for_completion(
    job_id="job-123",
    timeout=600,
    on_progress=lambda t: print(f"Progress: {t.progress}%")
)
Parameters:
  • job_id (str): Job ID to wait for
  • timeout (float, optional): Max wait time in seconds (default: 300)
  • poll_interval (float, optional): Seconds between checks (default: 3)
  • on_progress (callable, optional): Callback on each check

Async/Await

All methods support async using AsyncClient:
import asyncio
from legnext import AsyncClient

async def main():
    async with AsyncClient(api_key="your-api-key") as client:
        response = await client.midjourney.diffusion(text="...")
        result = await client.tasks.wait_for_completion(response.job_id)
        print(result.output.image_urls)

asyncio.run(main())
Batch processing example:
async with AsyncClient(api_key="your-api-key") as client:
    # Start multiple jobs
    responses = await asyncio.gather(*[
        client.midjourney.diffusion(text=f"prompt {i}")
        for i in range(5)
    ])
    
    # Wait for all
    results = await asyncio.gather(*[
        client.tasks.wait_for_completion(r.job_id)
        for r in responses
    ])

Error Handling

Handle different error types:
from legnext.exceptions import (
    ValidationError,
    AuthenticationError,
    RateLimitError,
    LegnextAPIError
)

try:
    response = client.midjourney.diffusion(text="...")
except AuthenticationError:
    print("Invalid API key")
except RateLimitError:
    print("Rate limited - retry later")
except ValidationError as e:
    print(f"Invalid parameters: {e.message}")
except LegnextAPIError as e:
    print(f"API error: {e.message}")
Exception Types:
ExceptionHTTPDescription
ValidationError400Invalid request parameters
AuthenticationError401Invalid API key
NotFoundError404Resource not found
RateLimitError429Rate limit exceeded
ServerError5xxServer error
LegnextAPIError-Base exception

Learn More