> ## 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 2 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.
* **Diffusing:** Optionally visualize how noisy outputs are refined into final text, showcasing the model's iterative denoising process.

Export your API key as an environment variable in your terminal.

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

  ```bash Windows theme={null}
  set INCEPTION_API_KEY="your_api_key_here"
  ```
</CodeGroup>

## Streaming

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

<CodeGroup>
  ```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-2",
        "messages": [
          {"role": "user", "content": "What is a diffusion model?"}
        ],
        "max_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-2",
      messages=[{"role": "user", "content": "What is a diffusion model?"}],
      max_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-2',
    messages: [{ role: 'user', content: 'What is a diffusion model?' }],
    max_tokens: 1000,
    stream: true,
  });
  for await (const chunk of stream) {
    process.stdout.write(chunk.choices[0]?.delta.content ?? '');
  }
  ```
</CodeGroup>

## 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.

<CodeGroup>
  ```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-2",
      "messages": [
        {"role": "user", "content": "What is a diffusion model?"}
      ],
      "max_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-2",
      messages=[{"role": "user", "content": "What is a diffusion model?"}],
      max_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-2',
    messages: [{ role: 'user', content: 'What is a diffusion model?' }],
    max_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 ?? '');
  }
  ```
</CodeGroup>

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);
        }
    }
}
```
