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

# Streaming & Diffusion

> Stream Mercury 3 chat completions or enable the diffusion mode to visualize how the model iteratively denoises text into the final answer.

Inception API supports streaming output and diffusion effect modes:

* **Streaming:** Get responses block-by-block for instant feedback—ideal for chat and live applications. A "block" is a chunk of refined text the model emits as it decodes, rather than one token at a time.
* **Diffusing:** Optionally visualize how noisy outputs are refined into final text, showcasing the model's iterative denoising process.

Set your [API key](https://platform.inceptionlabs.ai/dashboard/api-keys) as an environment variable and install the SDK.

```bash macOS / Linux theme={null}
export INCEPTION_API_KEY="your_api_key_here"
```

```cmd Windows (Command Prompt) theme={null}
set INCEPTION_API_KEY=your_api_key_here
```

```powershell Windows (PowerShell) theme={null}
$env:INCEPTION_API_KEY="your_api_key_here"
```

```bash Python theme={null}
pip install inceptionai
```

```bash TypeScript theme={null}
npm install inceptionai
```

## Streaming

Stream the response block-by-block as it is generated.

```bash cURL theme={null}
curl https://api.inceptionlabs.ai/v1/chat/completions \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $INCEPTION_API_KEY" \
    -d '{
      "model": "mercury-3",
      "messages": [
        {"role": "user", "content": "What is a diffusion model?"}
      ],
      "max_completion_tokens": 1000,
      "stream": true
    }'
```

```python Python theme={null}
from inceptionai import Inception

client = Inception()  # reads INCEPTION_API_KEY from the environment

stream = client.chat.completions.create(
    model="mercury-3",
    messages=[{"role": "user", "content": "What is a diffusion model?"}],
    max_completion_tokens=1000,
    stream=True,
)
for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="", flush=True)
```

```typescript TypeScript theme={null}
import Inception from 'inceptionai';

const client = new Inception(); // reads INCEPTION_API_KEY from the environment

const stream = await client.chat.completions.create({
  model: 'mercury-3',
  messages: [{ role: 'user', content: 'What is a diffusion model?' }],
  max_completion_tokens: 1000,
  stream: true,
});
for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta.content ?? '');
}
```

## Diffusion

Set `diffusing` to `true` to stream the model's intermediate denoising steps. Each chunk contains the full text as it is refined into the final answer.

```bash cURL theme={null}
curl https://api.inceptionlabs.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $INCEPTION_API_KEY" \
  -d '{
    "model": "mercury-3",
    "messages": [
      {"role": "user", "content": "What is a diffusion model?"}
    ],
    "max_completion_tokens": 1000,
    "stream": true,
    "diffusing": true
  }'
```

```python Python theme={null}
from inceptionai import Inception

client = Inception()  # reads INCEPTION_API_KEY from the environment

stream = client.chat.completions.create(
    model="mercury-3",
    messages=[{"role": "user", "content": "What is a diffusion model?"}],
    max_completion_tokens=1000,
    stream=True,
    diffusing=True,
)
for chunk in stream:
    # In diffusing mode each chunk is the full text as it is denoised
    print(chunk.choices[0].delta.content or "", flush=True)
```

```typescript TypeScript theme={null}
import Inception from 'inceptionai';

const client = new Inception(); // reads INCEPTION_API_KEY from the environment

const stream = await client.chat.completions.create({
  model: 'mercury-3',
  messages: [{ role: 'user', content: 'What is a diffusion model?' }],
  max_completion_tokens: 1000,
  stream: true,
  diffusing: true,
});
for await (const chunk of stream) {
  // In diffusing mode each chunk is the full text as it is denoised
  console.log(chunk.choices[0]?.delta.content ?? '');
}
```

The diffusing effect is best shown by overwriting the displayed text on each step. Here is an example in a web app using JavaScript:

```javascript theme={null}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let fullContent = '';
while (true) {
    const { done, value } = await reader.read();
    if (done) break;
    
    const chunk = decoder.decode(value);
    const lines = chunk.split('\n');
    
    for (const line of lines) {
        const trimmed = line.trim();
        if (!trimmed || !trimmed.startsWith('data: ')) continue;
        if (trimmed === 'data: [DONE]') continue;
        const jsonStr = trimmed.substring(6);
        if (!jsonStr.startsWith('{')) continue;
        try {
            const data = JSON.parse(jsonStr);
            
            for (const choice of data.choices || []) {
                if (choice.delta && choice.delta.content !== null && choice.delta.content !== undefined) {
                    fullContent = choice.delta.content || '';
                    contentElement.textContent = fullContent;
                }
            }
        } catch (error) {
            console.error('Parsing error:', error);
        }
    }
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.