> For clean Markdown of any page, append `.md` to the page URL.
> For a complete documentation index, see https://docs.sarvam.ai/llms.txt.
> For full documentation content in one file, see https://docs.sarvam.ai/llms-full.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.sarvam.ai/_mcp/server.

# Chat Completion

POST https://api.sarvam.ai/v2/chat/completions
Content-Type: application/json

Creates a model response for the given chat conversation. OpenAI-compatible request and response shape; differences from OpenAI are called out in the Open-Source Models documentation.

Requires `api-subscription-key`. Keys without beta access to this endpoint receive `400 invalid_request_error` before the model is called. Set `"stream": true` to receive the reply incrementally as server-sent events. Reasoning models return their chain-of-thought in a separate `reasoning_content` field on the message (`delta.reasoning_content` when streaming); reasoning tokens count against `max_tokens`.

Reference: https://docs.sarvam.ai/api-reference/open-source/chat-completions

## OpenAPI Specification

```yaml
openapi: 3.1.0
info:
  title: sarvam
  version: 1.0.0
paths:
  /v2/chat/completions:
    post:
      operationId: chat_completions_v2
      summary: Chat Completion
      description: >-
        Creates a model response for the given chat conversation.
        OpenAI-compatible request and response shape; differences from OpenAI
        are called out in the Open-Source Models documentation.


        Requires `api-subscription-key`. Keys without beta access to this
        endpoint receive `400 invalid_request_error` before the model is called.
        Set `"stream": true` to receive the reply incrementally as server-sent
        events. Reasoning models return their chain-of-thought in a separate
        `reasoning_content` field on the message (`delta.reasoning_content` when
        streaming); reasoning tokens count against `max_tokens`.
      tags:
        - openSourceModels
      parameters:
        - name: api-subscription-key
          in: header
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CreateChatCompletionV2Response'
        '400':
          description: >-
            Invalid request — a malformed field, a capability the model lacks
            (e.g. an image sent to a text-only model), or a remote image URL.
          content:
            application/json:
              schema:
                description: Any type
        '401':
          description: >-
            Missing or invalid API key — returned as 403 invalid_api_key_error
            in practice.
          content:
            application/json:
              schema:
                description: Any type
        '403':
          description: Missing or invalid API key (`invalid_api_key_error`).
          content:
            application/json:
              schema:
                description: Any type
        '413':
          description: Request larger than 10 MB (text and inline images combined).
          content:
            application/json:
              schema:
                description: Any type
        '422':
          description: >-
            Request that cannot be served — e.g. estimated prompt tokens +
            max_tokens exceed the model's context window.
          content:
            application/json:
              schema:
                description: Any type
        '429':
          description: Rate limit exceeded.
          content:
            application/json:
              schema:
                description: Any type
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateChatCompletionV2Request'
servers:
  - url: https://api.sarvam.ai
    description: Production
components:
  schemas:
    ChatCompletionRequestSystemMessage:
      type: object
      properties:
        content:
          type: string
          description: The contents of the system message.
        role:
          type: string
          enum:
            - system
          description: The role of the messages author, in this case `system`.
      required:
        - content
        - role
      title: ChatCompletionRequestSystemMessage
    ImageUrlDetail:
      type: string
      enum:
        - auto
        - low
        - high
      default: auto
      title: ImageUrlDetail
    ImageURL:
      type: object
      properties:
        url:
          type: string
        detail:
          oneOf:
            - $ref: '#/components/schemas/ImageUrlDetail'
            - type: 'null'
          default: auto
      required:
        - url
      title: ImageURL
    ChatCompletionRequestUserMessageContentOneOf1Items:
      oneOf:
        - type: object
          properties:
            type:
              type: string
              enum:
                - image_url
            image_url:
              $ref: '#/components/schemas/ImageURL'
          required:
            - type
            - image_url
          description: ImagePart variant
        - type: object
          properties:
            type:
              type: string
              enum:
                - text
            text:
              type: string
          required:
            - type
            - text
          description: TextPart variant
      discriminator:
        propertyName: type
      title: ChatCompletionRequestUserMessageContentOneOf1Items
    ChatCompletionRequestUserMessageContent1:
      type: array
      items:
        $ref: >-
          #/components/schemas/ChatCompletionRequestUserMessageContentOneOf1Items
      title: ChatCompletionRequestUserMessageContent1
    ChatCompletionRequestUserMessageContent:
      oneOf:
        - type: string
        - $ref: '#/components/schemas/ChatCompletionRequestUserMessageContent1'
      description: |
        The contents of the user message.
      title: ChatCompletionRequestUserMessageContent
    ChatCompletionRequestUserMessage:
      type: object
      properties:
        role:
          type: string
          enum:
            - user
          description: The role of the messages author, in this case `user`.
        content:
          $ref: '#/components/schemas/ChatCompletionRequestUserMessageContent'
          description: |
            The contents of the user message.
      required:
        - role
        - content
      title: ChatCompletionRequestUserMessage
    FunctionCall:
      type: object
      properties:
        name:
          type: string
          description: The name of the function to call.
        arguments:
          type: string
          description: The arguments to call the function with, as a JSON string.
      required:
        - name
        - arguments
      title: FunctionCall
    ChatCompletionMessageToolCall:
      type: object
      properties:
        id:
          type: string
          description: The ID of the tool call.
        type:
          type: string
          enum:
            - function
          description: The type of the tool call, currently only `function`.
        function:
          $ref: '#/components/schemas/FunctionCall'
      required:
        - id
        - type
        - function
      title: ChatCompletionMessageToolCall
    ChatCompletionRequestAssistantMessage:
      type: object
      properties:
        content:
          type:
            - string
            - 'null'
          description: |
            The contents of the assistant message
        role:
          type: string
          enum:
            - assistant
          description: The role of the messages author, in this case `assistant`.
        tool_calls:
          type:
            - array
            - 'null'
          items:
            $ref: '#/components/schemas/ChatCompletionMessageToolCall'
          description: The tool calls generated by the model.
      required:
        - role
      title: ChatCompletionRequestAssistantMessage
    ChatCompletionRequestToolMessage:
      type: object
      properties:
        role:
          type: string
          enum:
            - tool
          description: The role of the messages author, in this case `tool`.
        content:
          type: string
          description: The contents of the tool message.
        tool_call_id:
          type: string
          description: Tool call that this message is responding to.
      required:
        - role
        - content
        - tool_call_id
      title: ChatCompletionRequestToolMessage
    ChatCompletionRequestGenericMessageContentOneOf1Items:
      oneOf:
        - type: object
          properties:
            type:
              type: string
              enum:
                - image_url
            image_url:
              $ref: '#/components/schemas/ImageURL'
          required:
            - type
            - image_url
          description: ImagePart variant
        - type: object
          properties:
            type:
              type: string
              enum:
                - text
            text:
              type: string
          required:
            - type
            - text
          description: TextPart variant
      discriminator:
        propertyName: type
      title: ChatCompletionRequestGenericMessageContentOneOf1Items
    ChatCompletionRequestGenericMessageContent1:
      type: array
      items:
        $ref: >-
          #/components/schemas/ChatCompletionRequestGenericMessageContentOneOf1Items
      title: ChatCompletionRequestGenericMessageContent1
    ChatCompletionRequestGenericMessageContent:
      oneOf:
        - type: string
        - $ref: '#/components/schemas/ChatCompletionRequestGenericMessageContent1'
      description: The contents of the message.
      title: ChatCompletionRequestGenericMessageContent
    ChatCompletionRequestGenericMessage:
      type: object
      properties:
        role:
          type: string
          description: The role of the messages author.
        content:
          $ref: '#/components/schemas/ChatCompletionRequestGenericMessageContent'
          description: The contents of the message.
        name:
          type:
            - string
            - 'null'
          description: An optional name for the participant.
      required:
        - role
        - content
      title: ChatCompletionRequestGenericMessage
    ChatCompletionRequestMessage:
      oneOf:
        - $ref: '#/components/schemas/ChatCompletionRequestSystemMessage'
        - $ref: '#/components/schemas/ChatCompletionRequestUserMessage'
        - $ref: '#/components/schemas/ChatCompletionRequestAssistantMessage'
        - $ref: '#/components/schemas/ChatCompletionRequestToolMessage'
        - $ref: '#/components/schemas/ChatCompletionRequestGenericMessage'
      title: ChatCompletionRequestMessage
    V2ModelIds:
      type: string
      enum:
        - sarvam-105b
        - glm5.2
        - gemma4
      description: >-
        Model IDs from `GET /v2/models`:


        - **`sarvam-105b`** — Sarvam flagship chat model.

        - **`glm5.2`** — open-source; 512K context, tool calling, visible
        reasoning.

        - **`gemma4`** — open-source; image input, tool calling.
      title: V2ModelIds
    ReasoningEffort:
      type: string
      enum:
        - low
        - medium
        - high
      title: ReasoningEffort
    StopConfiguration:
      oneOf:
        - type: string
        - type: array
          items:
            type: string
      description: |
        Up to 4 sequences where the API will stop generating further tokens. The
        returned text will not contain the stop sequence.
      title: StopConfiguration
    FunctionDefinition:
      type: object
      properties:
        name:
          type: string
          description: The name of the function to be called.
        description:
          type:
            - string
            - 'null'
          description: A description of what the function does.
        parameters:
          type:
            - object
            - 'null'
          additionalProperties:
            description: Any type
          description: >-
            The parameters the function accepts, described as a JSON Schema
            object.
      required:
        - name
      title: FunctionDefinition
    ChatCompletionTool:
      type: object
      properties:
        type:
          type: string
          enum:
            - function
          description: The type of the tool. Currently, only `function` is supported.
        function:
          $ref: '#/components/schemas/FunctionDefinition'
      required:
        - type
        - function
      title: ChatCompletionTool
    ToolChoiceOption0:
      type: string
      enum:
        - none
        - auto
        - required
      title: ToolChoiceOption0
    ChatCompletionNamedToolChoiceFunction:
      type: object
      properties:
        name:
          type: string
          description: The name of the function to call.
      required:
        - name
      title: ChatCompletionNamedToolChoiceFunction
    ChatCompletionNamedToolChoice:
      type: object
      properties:
        type:
          type: string
          enum:
            - function
          description: The type of the tool. Currently, only `function` is supported.
        function:
          $ref: '#/components/schemas/ChatCompletionNamedToolChoiceFunction'
      required:
        - type
        - function
      title: ChatCompletionNamedToolChoice
    ToolChoiceOption:
      oneOf:
        - $ref: '#/components/schemas/ToolChoiceOption0'
        - $ref: '#/components/schemas/ChatCompletionNamedToolChoice'
      title: ToolChoiceOption
    JsonSchemaDefinition:
      type: object
      properties:
        name:
          type: string
          description: >-
            The name of the response format. Must contain only alphanumeric
            characters, underscores and dashes.
        description:
          type:
            - string
            - 'null'
          description: >-
            A description of what the response format is for, used by the model
            to determine how to respond in the format.
        schema:
          type: object
          additionalProperties:
            description: Any type
          description: >-
            The schema for the response format, described as a JSON Schema
            object.
        strict:
          type:
            - boolean
            - 'null'
          default: false
          description: >-
            Whether to enable strict schema adherence when generating the
            output.
      required:
        - name
        - schema
      description: The `json_schema` body of a `response_format` of type `json_schema`.
      title: JsonSchemaDefinition
    ResponseFormat:
      oneOf:
        - type: object
          properties:
            type:
              type: string
              enum:
                - json_object
              description: The type of response format being defined. Always `json_object`.
          required:
            - type
          description: ResponseFormatJsonObject variant
        - type: object
          properties:
            type:
              type: string
              enum:
                - json_schema
              description: The type of response format being defined. Always `json_schema`.
            json_schema:
              $ref: '#/components/schemas/JsonSchemaDefinition'
              description: >-
                Structured Outputs configuration options, including a JSON
                Schema.
          required:
            - type
            - json_schema
          description: ResponseFormatJsonSchema variant
        - type: object
          properties:
            type:
              type: string
              enum:
                - text
              description: The type of response format being defined. Always `text`.
          required:
            - type
          description: ResponseFormatText variant
      discriminator:
        propertyName: type
      title: ResponseFormat
    CreateChatCompletionV2Request:
      type: object
      properties:
        messages:
          type: array
          items:
            $ref: '#/components/schemas/ChatCompletionRequestMessage'
          description: A list of messages comprising the conversation so far.
        model:
          $ref: '#/components/schemas/V2ModelIds'
        temperature:
          type:
            - number
            - 'null'
          format: double
          description: >
            What sampling temperature to use, between 0 and 2. Higher values
            like 0.8 will make the output more random, while lower values like
            0.2 will make it more focused and deterministic.

            We generally recommend altering this or `top_p` but not both.
        top_p:
          type:
            - number
            - 'null'
          format: double
          default: 1
          description: >
            An alternative to sampling with temperature, called nucleus
            sampling,

            where the model considers the results of the tokens with top_p
            probability

            mass. So 0.1 means only the tokens comprising the top 10%
            probability mass

            are considered.


            We generally recommend altering this or `temperature` but not both.
        reasoning_effort:
          oneOf:
            - $ref: '#/components/schemas/ReasoningEffort'
            - type: 'null'
          description: The effort to use for reasoning
        max_tokens:
          type:
            - integer
            - 'null'
          default: 2048
          description: >-
            The maximum number of tokens that can be generated in the chat
            completion.
        stream:
          type:
            - boolean
            - 'null'
          default: false
          description: >-
            If set to true, the model response data will be streamed to the
            client

            as it is generated using [server-sent
            events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format).
        stop:
          oneOf:
            - $ref: '#/components/schemas/StopConfiguration'
            - type: 'null'
        'n':
          type:
            - integer
            - 'null'
          default: 1
          description: >-
            How many chat completion choices to generate for each input message.
            Note that you will be charged based on the number of generated
            tokens across all of the choices. Keep `n` as `1` to minimize costs.
        seed:
          type:
            - integer
            - 'null'
          description: >
            This feature is in Beta.

            If specified, our system will make a best effort to sample
            deterministically, such that repeated requests with the same `seed`
            and parameters should return the same result.

            Determinism is not guaranteed, and you should refer to the
            `system_fingerprint` response parameter to monitor changes in the
            backend.
        frequency_penalty:
          type:
            - number
            - 'null'
          format: double
          default: 0
          description: >
            Number between -2.0 and 2.0. Positive values penalize new tokens
            based on

            their existing frequency in the text so far, decreasing the model's

            likelihood to repeat the same line verbatim.
        presence_penalty:
          type:
            - number
            - 'null'
          format: double
          default: 0
          description: >
            Number between -2.0 and 2.0. Positive values penalize new tokens
            based on

            whether they appear in the text so far, increasing the model's
            likelihood

            to talk about new topics.
        tools:
          type:
            - array
            - 'null'
          items:
            $ref: '#/components/schemas/ChatCompletionTool'
          description: >-
            A list of tools the model may call. Currently, only functions are
            supported as a tool.
        tool_choice:
          oneOf:
            - $ref: '#/components/schemas/ToolChoiceOption'
            - type: 'null'
          description: Controls which (if any) tool is called by the model.
        response_format:
          oneOf:
            - $ref: '#/components/schemas/ResponseFormat'
            - type: 'null'
          description: >-
            An object specifying the format that the model must output. Setting
            `{ "type": "json_schema", "json_schema": {...} }` enables Structured
            Outputs which guarantees the model generates output matching the
            supplied JSON Schema. Setting `{ "type": "json_object" }` enables
            the older JSON mode, which guarantees valid JSON but not a specific
            schema.
        extra_body:
          type: object
          additionalProperties:
            description: Any type
          default: {}
          description: Extra body to be sent to the model.
      required:
        - messages
        - model
      title: CreateChatCompletionV2Request
    FinishReason:
      type: string
      enum:
        - stop
        - length
        - tool_calls
        - content_filter
        - function_call
      title: FinishReason
    Role:
      type: string
      enum:
        - assistant
      title: Role
    ChatCompletionResponseMessage:
      type: object
      properties:
        content:
          type:
            - string
            - 'null'
          description: The contents of the message.
        refusal:
          type:
            - string
            - 'null'
        reasoning_content:
          type:
            - string
            - 'null'
          description: The contents of the reasoning message.
        role:
          $ref: '#/components/schemas/Role'
          description: The role of the author of this message.
        tool_calls:
          type:
            - array
            - 'null'
          items:
            $ref: '#/components/schemas/ChatCompletionMessageToolCall'
          description: The tool calls generated by the model.
      required:
        - content
        - role
      title: ChatCompletionResponseMessage
    Choice:
      type: object
      properties:
        finish_reason:
          $ref: '#/components/schemas/FinishReason'
          description: >
            The reason the model stopped generating tokens. This will be `stop`
            if the model hit a natural stop point or a provided stop sequence,

            `length` if the maximum number of tokens specified in the request
            was reached,

            `content_filter` if content was omitted due to a flag from our
            content filters,

            `tool_calls` if the model called a tool, or `function_call`
            (deprecated) if the model called a function.
        index:
          type: integer
          description: The index of the choice in the list of choices.
        logprobs:
          type:
            - object
            - 'null'
          additionalProperties:
            description: Any type
        message:
          $ref: '#/components/schemas/ChatCompletionResponseMessage'
      required:
        - finish_reason
        - index
        - message
      title: Choice
    CompletionUsage:
      type: object
      properties:
        completion_tokens:
          type: integer
          description: Number of tokens in the generated completion.
        prompt_tokens:
          type: integer
          description: Number of tokens in the prompt.
        total_tokens:
          type: integer
          description: Total number of tokens used in the request (prompt + completion).
        completion_tokens_details:
          type:
            - object
            - 'null'
          additionalProperties:
            description: Any type
        prompt_tokens_details:
          type:
            - object
            - 'null'
          additionalProperties:
            description: Any type
      required:
        - completion_tokens
        - prompt_tokens
        - total_tokens
      title: CompletionUsage
    CreateChatCompletionV2Response:
      type: object
      properties:
        id:
          type: string
          description: A unique identifier for the chat completion.
        choices:
          type: array
          items:
            $ref: '#/components/schemas/Choice'
          description: >-
            A list of chat completion choices. Can be more than one if `n` is
            greater than 1.
        created:
          type: integer
          description: >-
            The Unix timestamp (in seconds) of when the chat completion was
            created.
        model:
          type: string
          description: The model used for the chat completion.
        object:
          type: string
          enum:
            - chat.completion
          description: The object type, which is always `chat.completion`.
        service_tier:
          type:
            - string
            - 'null'
        system_fingerprint:
          type:
            - string
            - 'null'
        usage:
          oneOf:
            - $ref: '#/components/schemas/CompletionUsage'
            - type: 'null'
      required:
        - id
        - choices
        - created
        - model
        - object
      title: CreateChatCompletionV2Response
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: api-subscription-key
    HTTPBearer:
      type: http
      scheme: bearer

```

## Examples



**Request**

```json
{
  "messages": [
    {
      "content": "Summarise the causes of the 2008 financial crisis.",
      "role": "user"
    }
  ],
  "model": "glm5.2"
}
```

**Response**

```json
{
  "id": "20250101_0a1b2c3d-1234-5678-9abc-def012345678",
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "The 2008 financial crisis was driven by a housing bubble, subprime lending, and the collapse of mortgage-backed securities.",
        "role": "assistant",
        "refusal": null,
        "reasoning_content": null,
        "tool_calls": null
      },
      "logprobs": {}
    }
  ],
  "created": 1735689600,
  "model": "glm5.2",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 24,
    "prompt_tokens": 18,
    "total_tokens": 42
  }
}
```

**SDK Code**

```python Open-Source Models_chat_completions_v2_example
from sarvamai import SarvamAI, ChatCompletionRequestSystemMessage

client = SarvamAI(
    api_subscription_key="YOUR_API_KEY_HERE",
)

client.open_source_models.chat_completions_v2(
    messages=[
        ChatCompletionRequestSystemMessage(
            content="Summarise the causes of the 2008 financial crisis.",
            role="user",
        )
    ],
    model="glm5.2",
)

```

```javascript Open-Source Models_chat_completions_v2_example
const url = 'https://api.sarvam.ai/v2/chat/completions';
const options = {
  method: 'POST',
  headers: {
    'api-subscription-key': '<apiSubscriptionKey>',
    'Content-Type': 'application/json'
  },
  body: '{"messages":[{"content":"Summarise the causes of the 2008 financial crisis.","role":"user"}],"model":"glm5.2"}'
};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```go Open-Source Models_chat_completions_v2_example
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://api.sarvam.ai/v2/chat/completions"

	payload := strings.NewReader("{\n  \"messages\": [\n    {\n      \"content\": \"Summarise the causes of the 2008 financial crisis.\",\n      \"role\": \"user\"\n    }\n  ],\n  \"model\": \"glm5.2\"\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("api-subscription-key", "<apiSubscriptionKey>")
	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby Open-Source Models_chat_completions_v2_example
require 'uri'
require 'net/http'

url = URI("https://api.sarvam.ai/v2/chat/completions")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["api-subscription-key"] = '<apiSubscriptionKey>'
request["Content-Type"] = 'application/json'
request.body = "{\n  \"messages\": [\n    {\n      \"content\": \"Summarise the causes of the 2008 financial crisis.\",\n      \"role\": \"user\"\n    }\n  ],\n  \"model\": \"glm5.2\"\n}"

response = http.request(request)
puts response.read_body
```

```java Open-Source Models_chat_completions_v2_example
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://api.sarvam.ai/v2/chat/completions")
  .header("api-subscription-key", "<apiSubscriptionKey>")
  .header("Content-Type", "application/json")
  .body("{\n  \"messages\": [\n    {\n      \"content\": \"Summarise the causes of the 2008 financial crisis.\",\n      \"role\": \"user\"\n    }\n  ],\n  \"model\": \"glm5.2\"\n}")
  .asString();
```

```php Open-Source Models_chat_completions_v2_example
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.sarvam.ai/v2/chat/completions', [
  'body' => '{
  "messages": [
    {
      "content": "Summarise the causes of the 2008 financial crisis.",
      "role": "user"
    }
  ],
  "model": "glm5.2"
}',
  'headers' => [
    'Content-Type' => 'application/json',
    'api-subscription-key' => '<apiSubscriptionKey>',
  ],
]);

echo $response->getBody();
```

```csharp Open-Source Models_chat_completions_v2_example
using RestSharp;

var client = new RestClient("https://api.sarvam.ai/v2/chat/completions");
var request = new RestRequest(Method.POST);
request.AddHeader("api-subscription-key", "<apiSubscriptionKey>");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"messages\": [\n    {\n      \"content\": \"Summarise the causes of the 2008 financial crisis.\",\n      \"role\": \"user\"\n    }\n  ],\n  \"model\": \"glm5.2\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift Open-Source Models_chat_completions_v2_example
import Foundation

let headers = [
  "api-subscription-key": "<apiSubscriptionKey>",
  "Content-Type": "application/json"
]
let parameters = [
  "messages": [
    [
      "content": "Summarise the causes of the 2008 financial crisis.",
      "role": "user"
    ]
  ],
  "model": "glm5.2"
] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://api.sarvam.ai/v2/chat/completions")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```