# Streaming Response

## OpenAPI Specification

```yaml
openapi: 3.0.1
info:
  title: ''
  description: ''
  version: 1.0.0
paths:
  /v1/chat/completions:
    post:
      summary: Streaming Response
      deprecated: false
      description: >-
        Streaming outputs is **supported by all models with text output
        capability** (Chat, Image Understanding, etc.). It is **not supported by
        models with image output capability** (Image Generation).


        Streaming outputs uses [Server-Sent Events
        (SSE)](https://en.wikipedia.org/wiki/Server-sent_events) that let the
        server send back the delta of content in event streams.


        Streaming responses are beneficial for providing real-time feedback,
        enhancing user interaction by allowing text to be displayed as it's
        generated.


        To enable streaming, you must set `"stream": true` in your request:


        ```bash

        curl https://api.x.ai/v1/chat/completions \
          -H "Content-Type: application/json" \
          -H "Authorization: Bearer $XAI_API_KEY" \
          -d '{
                "messages": [
                  {
                    "role": "system",
                    "content": "You are Grok, a chatbot inspired by the Hitchhikers Guide to the Galaxy."
                  },
                  {
                    "role": "user",
                    "content": "What is the meaning of life, the universe, and everything?"
                  }
                ],
                "model": "grok-3-latest",
                "stream": true,
                "temperature": 0
              }'
        ```


        You'll get the event streams like these:


        ```bash

        data: {

        "id":"<completion_id>","object":"chat.completion.chunk","created":<creation_time>,

        "model":"grok-3-beta",

        "choices":[{"index":0,"delta":{"content":"Ah","role":"assistant"}}],

        "usage":{"prompt_tokens":41,"completion_tokens":1,"total_tokens":42,

        "prompt_tokens_details":{"text_tokens":41,"audio_tokens":0,"image_tokens":0,"cached_tokens":0}},

        "system_fingerprint":"fp_xxxxxxxxxx"}


        data: {

        "id":"<completion_id>","object":"chat.completion.chunk","created":<creation_time>,

        "model":"grok-3-beta",

        "choices":[{"index":0,"delta":{"content":",","role":"assistant"}}],

        "usage":{"prompt_tokens":41,"completion_tokens":2,"total_tokens":43,

        "prompt_tokens_details":{"text_tokens":41,"audio_tokens":0,"image_tokens":0,"cached_tokens":0}},

        "system_fingerprint":"fp_xxxxxxxxxx"

        }


        data: [DONE]

        ```


        It is recommended that you use a client SDK to parse the event stream.


        Example streaming responses in Python/Javascript:


        ```bash

        Ah, the ultimate question! According to Douglas Adams, the answer is
        **42**. However, the trick lies in figuring out what the actual question
        is. If you're looking for a bit more context or a different perspective:


        - **Philosophically**: The meaning of life might be to seek purpose,
        happiness, or to fulfill one's potential.

        - **Biologically**: It could be about survival, reproduction, and
        passing on genes.

        - **Existentially**: You create your own meaning through your
        experiences and choices.


        But let's not forget, the journey to find this meaning might just be as
        important as the answer itself! Keep exploring, questioning, and
        enjoying the ride through the universe. And remember, don't panic!

        ```
      tags:
        - Guides
      parameters:
        - name: Content-Type
          in: header
          description: ''
          required: true
          example: application/json
          schema:
            type: string
        - name: Authorization
          in: header
          description: ''
          required: true
          example: Bearer {{XAI_API_KEY}}
          schema:
            type: string
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                messages:
                  type: array
                  items:
                    type: object
                    properties:
                      role:
                        type: string
                      content:
                        type: string
                    required:
                      - role
                      - content
                model:
                  type: string
                stream:
                  type: boolean
                temperature:
                  type: integer
              required:
                - messages
                - model
                - stream
                - temperature
            example:
              messages:
                - role: system
                  content: >-
                    You are Grok, a chatbot inspired by the Hitchhikers Guide to
                    the Galaxy.
                - role: user
                  content: What is the meaning of life, the universe, and everything?
              model: grok-3-latest
              stream: true
              temperature: 0
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                type: object
                properties: {}
          headers: {}
          x-apidog-name: Success
      security: []
      x-apidog-folder: Guides
      x-apidog-status: released
      x-run-in-apidog: https://app.apidog.com/web/project/875787/apis/api-15799248-run
components:
  schemas: {}
  securitySchemes: {}
servers:
  - url: https://api.x.ai/v1/
    description: Prod Env
security: []

```
