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

# Chat completions

> POST /v1/chat/completions — OpenAI-compatible text and multimodal chat.

Drop-in OpenAI-compatible chat endpoint. Supports streaming, tool calls, JSON mode, vision, PDF, audio input and **Files API `file_id` references**.

## Minimal example

<CodeGroup>
  ```bash curl theme={null}
  curl https://api.infery.ai/v1/chat/completions \
    -H "Authorization: Bearer $INFERY_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "gpt-4o",
      "messages": [
        {"role": "user", "content": "Summarise Node.js streams in 2 sentences"}
      ]
    }'
  ```

  ```python python theme={null}
  from openai import OpenAI
  client = OpenAI(api_key=API_KEY, base_url="https://api.infery.ai/v1")
  resp = client.chat.completions.create(
      model="gpt-4o",
      messages=[{"role": "user", "content": "Hi"}],
  )
  ```

  ```typescript node theme={null}
  import OpenAI from 'openai';
  const client = new OpenAI({ apiKey: process.env.INFERY_API_KEY, baseURL: 'https://api.infery.ai/v1' });
  const resp = await client.chat.completions.create({
    model: 'gpt-4o',
    messages: [{ role: 'user', content: 'Hi' }],
  });
  ```
</CodeGroup>

## Streaming

Add `"stream": true`. Response is Server-Sent Events (`text/event-stream`). Each chunk is `data: {...}\n\n`; the stream ends with `data: [DONE]\n\n`.

The **final chunk before `[DONE]`** carries usage info and Infery-specific `credits_used`:

```
data: {"choices":[{"delta":{"content":"..."}}]}
data: {"choices":[],"usage":{"prompt_tokens":42,"completion_tokens":128}}
data: {"choices":[],"usage":{...},"credits_used":2}
data: [DONE]
```

OpenAI SDKs ignore chunks with empty `choices`, so `credits_used` is a non-breaking extension.

## Multimodal content

Pass arrays in `content`:

```json theme={null}
{
  "model": "gpt-4o",
  "messages": [{
    "role": "user",
    "content": [
      {"type": "text", "text": "What's in this image?"},
      {"type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}}
    ]
  }]
}
```

Supported block types:

* `text`
* `image_url` — HTTP URL or base64 `data:` URI
* `input_audio` — inline base64 audio with `format` (wav/mp3/pcm16/webm)
* `file` — inline `data` + `mime_type` **or** `file_id` reference ([Files API](/api-reference/files))

### Using a Files API reference

```json theme={null}
{
  "content": [
    {"type": "text", "text": "Review this PDF"},
    {"type": "file", "file_id": "file_abc123..."}
  ]
}
```

The gateway resolves `file_id` to bytes on the server, injects them into the provider call, and returns a clear 400 if the id doesn't exist or is out of your workspace.

## Tool calls

Works exactly like OpenAI's spec — `tools`, `tool_choice`, `function` schema and `tool` role messages. Every chat-capable model on Infery that supports tools honours the same format.

## JSON mode

```json theme={null}
{"response_format": {"type": "json_object"}}
```

Or with a schema (Structured Outputs):

```json theme={null}
{
  "response_format": {
    "type": "json_schema",
    "json_schema": { "schema": { ... }, "strict": true }
  }
}
```

## Vision and PDFs

Models with `supportsVision: true` accept images directly. For PDFs, models with `supportsPdf: true` read them natively. Others get an **automatic PDF-to-image conversion** on the gateway (plus text extraction) — you pay a small extra fee per page (see billing), no code changes required.

## Parameters

Full OpenAI parameter set: `temperature`, `top_p`, `presence_penalty`, `frequency_penalty`, `max_tokens`, `stop`, `seed`, `stream`, `tools`, `tool_choice`, `response_format`. Plus model-specific:

* `top_k` — Gemini and some OSS models

## Response headers

* `x-request-id`
* `x-credits-used`
* `x-model-used` (when fallback fires)
* `x-fallback-from`, `x-fallback-depth`


## OpenAPI

````yaml openapi.json POST /v1/chat/completions
openapi: 3.0.0
info:
  title: Infery Gateway
  description: >-
    Infery Inference Gateway — OpenAI-compatible API for LLMs, embeddings,
    images, audio, and video
  version: '1.0'
  contact: {}
servers:
  - url: https://api.infery.ai
    description: Production
  - url: http://localhost:3001
    description: Local
security: []
tags: []
paths:
  /v1/chat/completions:
    post:
      tags:
        - Chat Completions
      summary: Create chat completion
      operationId: ChatController_completions
      parameters:
        - name: x-request-id
          in: header
          description: Optional request ID for tracking
          required: false
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - model
                - messages
              properties:
                model:
                  type: string
                  description: Model ID
                  example: gpt-4o
                messages:
                  type: array
                  items:
                    type: object
                    properties:
                      role:
                        type: string
                        enum:
                          - system
                          - user
                          - assistant
                          - tool
                      content:
                        type: string
                    required:
                      - role
                      - content
                temperature:
                  type: number
                  minimum: 0
                  maximum: 2
                max_tokens:
                  type: integer
                top_p:
                  type: number
                  minimum: 0
                  maximum: 1
                top_k:
                  type: integer
                  description: Top-K sampling (Google Gemini)
                presence_penalty:
                  type: number
                  minimum: -2
                  maximum: 2
                  description: Presence penalty (OpenAI, Google Gemini)
                frequency_penalty:
                  type: number
                  minimum: -2
                  maximum: 2
                  description: Frequency penalty (OpenAI, Google Gemini)
                seed:
                  type: integer
                  description: Seed for deterministic output (OpenAI, Google Gemini)
                stream:
                  type: boolean
                  default: false
                stop:
                  oneOf:
                    - type: string
                    - type: array
                      items:
                        type: string
                tools:
                  type: array
                  items:
                    type: object
                tool_choice: {}
                response_format:
                  type: object
      responses:
        '200':
          description: >-
            Chat completion result. When stream=true, returns SSE
            (text/event-stream) where each data chunk is a
            `chat.completion.chunk`; the final chunk before `[DONE]` carries
            `usage` and `credits_used`.
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: string
                    example: chatcmpl-abc123
                  object:
                    type: string
                    example: chat.completion
                  created:
                    type: integer
                    example: 1713204900
                  model:
                    type: string
                    example: gpt-4o
                  choices:
                    type: array
                    items:
                      type: object
                      properties:
                        index:
                          type: integer
                          example: 0
                        message:
                          type: object
                          properties:
                            role:
                              type: string
                              example: assistant
                            content:
                              type: string
                              example: Hello! How can I help you today?
                            tool_calls:
                              type: array
                              items:
                                type: object
                              description: Present when the model invokes tools
                        finish_reason:
                          type: string
                          enum:
                            - stop
                            - length
                            - tool_calls
                            - content_filter
                          example: stop
                  usage:
                    type: object
                    properties:
                      prompt_tokens:
                        type: integer
                        example: 42
                      completion_tokens:
                        type: integer
                        example: 128
                      total_tokens:
                        type: integer
                        example: 170
                  credits_used:
                    type: integer
                    example: 2
                    description: >-
                      Credits deducted from the workspace balance for this
                      request
        '400':
          description: Invalid request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponseDto'
        '401':
          description: Unauthorized — invalid or missing API key
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponseDto'
        '404':
          description: Model not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponseDto'
      security:
        - ApiKey: []
components:
  schemas:
    ErrorResponseDto:
      type: object
      properties:
        error:
          $ref: '#/components/schemas/ErrorDetailDto'
      required:
        - error
    ErrorDetailDto:
      type: object
      properties:
        message:
          type: string
          example: Model not found
          description: Human-readable error message
        type:
          type: string
          example: invalid_request_error
          description: Error category
          enum:
            - invalid_request_error
            - authentication_error
            - permission_error
            - quota_exceeded
            - rate_limit_error
            - server_error
        code:
          type: object
          example: model_not_found
          nullable: true
          description: Stable machine-readable error code
        param:
          type: object
          example: model
          nullable: true
          description: >-
            Name of the request parameter that triggered the error, if
            applicable
      required:
        - message
        - type
        - code
        - param
  securitySchemes:
    ApiKey:
      type: apiKey
      in: header
      name: Authorization
      description: 'API key in format: Bearer inf_***'

````