> ## Documentation Index
> Fetch the complete documentation index at: https://docs.legnext.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Task Management

> Task status, async/await, and error handling

## tasks.get(job\_id)

Get current task status:

```python theme={null}
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:

```python theme={null}
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`:

```python theme={null}
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:**

```python theme={null}
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:

```python theme={null}
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:**

| Exception             | HTTP | Description                |
| --------------------- | ---- | -------------------------- |
| `ValidationError`     | 400  | Invalid request parameters |
| `AuthenticationError` | 401  | Invalid API key            |
| `NotFoundError`       | 404  | Resource not found         |
| `RateLimitError`      | 429  | Rate limit exceeded        |
| `ServerError`         | 5xx  | Server error               |
| `LegnextAPIError`     | -    | Base exception             |

***

## Learn More

* [Image Generation API](/sdk/python/image-generation)
* [Video Generation API](/sdk/python/video-generation)
* [Quick Start](/sdk/python/quickstart)
