---
type: api
title: Perform streaming inference
description: Get real-time responses for completion tasks by delivering answers incrementally, reducing response times during computation.
url: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch/v8/operation/operation-inference-stream-completion
resource: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch/v8/operation/operation-inference-stream-completion
products:
  - Elasticsearch
---

# Perform streaming inference

Availability: 8.16+

## Paths

- `POST` `/_inference/completion/{inference_id}/_stream`

- [Api key auth](/elastic/docs-builder/docs/4371/api/doc/elasticsearch/v8/authentication#apikeyauth)
- [Basic auth](/elastic/docs-builder/docs/4371/api/doc/elasticsearch/v8/authentication#basicauth)
- [Bearer auth](/elastic/docs-builder/docs/4371/api/doc/elasticsearch/v8/authentication#bearerauth)

#### Path Parameters

- `inference_id` (string Id)
  The unique identifier for the inference endpoint.

## Description

Get real-time responses for completion tasks by delivering answers incrementally, reducing response times during computation.
This API works only with the completion task type.

IMPORTANT: The inference APIs enable you to use certain services, such as built-in machine learning models (ELSER, E5), models uploaded through Eland, Cohere, OpenAI, Azure, Google AI Studio, Google Vertex AI, Anthropic, Watsonx.ai, or Hugging Face. For built-in models and models uploaded through Eland, the inference APIs offer an alternative way to use and manage trained models. However, if you do not plan to use the inference APIs to use these models or if you want to use non-NLP models, use the machine learning trained model APIs.

This API requires the `monitor_inference` cluster privilege (the built-in `inference_admin` and `inference_user` roles grant this privilege). You must use a client that supports streaming.

## Required authorization

* Cluster privileges: `monitor_inference`

## Query String Parameters

- `timeout` (string Duration · default: 30s)
  The amount of time to wait for the inference request to complete.

## Request Body

`application/json`

- `input` (union string | [] string) — required
  The text on which you want to perform the inference task.
It can be a single string or an array.

NOTE: Inference endpoints for the completion task type currently only support a single string as input.
- `task_settings` (TaskSettings) — optional
  Optional task settings

## Response

`application/json`

Type: `StreamResult`

## Code Examples

#### Console

```console
POST _inference/completion/openai-completion/_stream
{
  "input": "What is Elastic?"
}
```

#### JavaScript

```javascript
const response = await client.inference.streamCompletion({
  inference_id: "openai-completion",
  input: "What is Elastic?",
});
```

#### Python

```python
resp = client.inference.stream_completion(
    inference_id="openai-completion",
    input="What is Elastic?",
)
```

#### curl

```curl
curl -X POST "$ELASTICSEARCH_URL/_inference/completion/openai-completion/_stream" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "input": "What is Elastic?"
  }'
```

#### Java

```java
client.inference().streamCompletion(s -> s
    .inferenceId("openai-completion")
    .input("What is Elastic?")
);
```

#### C#

```c#
var response = await client.Inference
    .StreamCompletionAsync(inferenceId: "openai-completion", d1 => d1
        .Input("What is Elastic?")
    );
```

#### PHP

```php
$resp = $client->inference()->streamCompletion([
    "inference_id" => "openai-completion",
    "body" => [
        "input" => "What is Elastic?",
    ],
]);
```

#### Ruby

```ruby
response = client.inference.stream_completion(
  inference_id: "openai-completion",
  body: {
    "input": "What is Elastic?"
  }
)
```

