> This page is for version v0.25.0.
> For other versions, use one of these documentation indexes:
> - Latest (default): https://docs.nvidia.com/nemo/guardrails/latest/llms.txt
> - v0.25.0: https://docs.nvidia.com/nemo/guardrails/v0.25.0/llms.txt
> - v0.24.1: https://docs.nvidia.com/nemo/guardrails/v0.24.1/llms.txt
> - v0.24.0: https://docs.nvidia.com/nemo/guardrails/v0.24.0/llms.txt
> - v0.23.0: https://docs.nvidia.com/nemo/guardrails/v0.23.0/llms.txt
> - v0.22.0: https://docs.nvidia.com/nemo/guardrails/v0.22.0/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/guardrails/llms.txt.

# Create a Guarded Chat Completion

POST /v1/chat/completions
Content-Type: application/json

This page describes the built-in native OpenAI guard boundary at an
example URL. Provider mounts and path prefixes can change the route.
Set `stream` to `true` for SSE. Enabled Rails checks depend on configuration.

Inspect the [request](/nemo/guardrails/v0.25.0/reference/gateway-guard-contracts/chat-completions#request),
[response](/nemo/guardrails/v0.25.0/reference/gateway-guard-contracts/chat-completions#response), and
[stream](/nemo/guardrails/v0.25.0/reference/gateway-guard-contracts/chat-completions#stream) guard declarations.
The parameter list omits disabled fields; the request contract
retains their declarations.

See [field-policy meanings](/nemo/guardrails/v0.25.0/run-guardrailed-inference/using-gateway/guard-contracts#read-the-field-policy),
[supported input and output](/nemo/guardrails/v0.25.0/run-guardrailed-inference/using-gateway/providers-and-streaming#coverage-of-the-shipped-profiles),
and [streaming completion and errors](/nemo/guardrails/v0.25.0/run-guardrailed-inference/using-gateway/providers-and-streaming#completion-and-errors).
Field types describe gateway validation; consult the
[provider reference](https://developers.openai.com/api/reference/resources/chat/subresources/completions/methods/create)
for upstream requirements. Replace `example-model` with a model available
from your provider.


Reference: https://docs.nvidia.com/nemo/guardrails/reference/gateway-api/chat-completions

## Request

### Body (application/json)

This endpoint expects an object.

- `messages` (list of V1ChatCompletionsPostRequestBodyContentApplicationJsonSchemaMessagesItems, required) — Guarded Container leading to text selected for input checks when enabled in the Rails configuration.
- `frequency_penalty` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `logit_bias` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `logprobs` (false, optional, nullable) — Constrained The gateway enforces the declared value and shape restrictions.
- `max_completion_tokens` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `max_tokens` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `metadata` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `model` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `moderation` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `n` (integer, optional, default: 1) — Constrained The gateway enforces the declared value and shape restrictions.
- `presence_penalty` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `prompt_cache_key` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `prompt_cache_options` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `prompt_cache_retention` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `reasoning_effort` (enum, optional, nullable) — Constrained The gateway enforces the declared value and shape restrictions.
  - Allowed values: `none`, `minimal`, `low`, `medium`, `high`, `xhigh`, `max`
- `safety_identifier` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `seed` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `service_tier` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `stop` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `store` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `stream` (boolean, optional, nullable, default: false) — Constrained The gateway enforces the declared value and shape restrictions.
- `stream_options` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `temperature` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `top_p` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `user` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.
- `verbosity` (any, optional) — Opaque Forwarded without inspecting or validating its value or nested contents. Provider requirements still apply.

## Response

### 200

Provider-native JSON or SSE. The JSON example is an illustrative excerpt. Output restrictions apply only when output inspection is enabled. After SSE headers, failures are reported in the stream rather than by changing the HTTP status.

- `any`

## Errors

### 400 Bad Request Error

Invalid request path or framing, malformed JSON, or content rejected by Guardrails.

- `error` (OpenAIProxyErrorDetails, required) — Describe OpenAI-compatible details generated by the proxy.

### 405 Method Not Allowed Error

Request method rejected for a guarded or reserved operation.

- `error` (OpenAIProxyErrorDetails, required) — Describe OpenAI-compatible details generated by the proxy.

### 413 Content Too Large Error

Request body rejected by the proxy size limit.

- `error` (OpenAIProxyErrorDetails, required) — Describe OpenAI-compatible details generated by the proxy.

### 415 Unsupported Media Type Error

Request representation unsupported by the guarded proxy path.

- `error` (OpenAIProxyErrorDetails, required) — Describe OpenAI-compatible details generated by the proxy.

### 422 Unprocessable Entity Error

Request path, shape, or configured Guardrails unsupported by the guarded proxy path.

- `error` (OpenAIProxyErrorDetails, required) — Describe OpenAI-compatible details generated by the proxy.

### 502 Bad Gateway Error

Upstream forwarding or Guardrails processing failed in the proxy.

- `error` (OpenAIProxyErrorDetails, required) — Describe OpenAI-compatible details generated by the proxy.

## Types

### V1ChatCompletionsPostRequestBodyContentApplicationJsonSchemaMessagesItems

Accept one user text message with reviewed optional provider metadata. Unlisted fields and case aliases of declared fields are rejected by default.

- `content` (string, required) — Guarded Text selected for input checks when enabled in the Rails configuration.
- `role` ("user", required) — Constrained The gateway enforces the declared value and shape restrictions.

### OpenAIProxyErrorDetails

Describe OpenAI-compatible details generated by the proxy.

- `code` (string, required)
- `message` (string, required)
- `param` (string, required, nullable)
- `type` (string, required)

## Examples

**Request**

```json
{
  "messages": [
    {
      "content": "Hello!",
      "role": "user"
    }
  ],
  "model": "example-model"
}
```

**Response**

```json
{
  "choices": [
    {
      "message": {
        "content": "Hello!",
        "role": "assistant"
      }
    }
  ]
}
```

**SDK Code**

```python
import requests

url = "https://api.example.com/v1/chat/completions"

payload = {
    "messages": [
        {
            "content": "Hello!",
            "role": "user"
        }
    ],
    "model": "example-model"
}
headers = {"Content-Type": "application/json"}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```

```javascript
const url = 'https://api.example.com/v1/chat/completions';
const options = {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: '{"messages":[{"content":"Hello!","role":"user"}],"model":"example-model"}'
};

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

```go
package main

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

func main() {

	url := "https://api.example.com/v1/chat/completions"

	payload := strings.NewReader("{\n  \"messages\": [\n    {\n      \"content\": \"Hello!\",\n      \"role\": \"user\"\n    }\n  ],\n  \"model\": \"example-model\"\n}")

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

	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
require 'uri'
require 'net/http'

url = URI("https://api.example.com/v1/chat/completions")

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

request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n  \"messages\": [\n    {\n      \"content\": \"Hello!\",\n      \"role\": \"user\"\n    }\n  ],\n  \"model\": \"example-model\"\n}"

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

```java
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://api.example.com/v1/chat/completions")
  .header("Content-Type", "application/json")
  .body("{\n  \"messages\": [\n    {\n      \"content\": \"Hello!\",\n      \"role\": \"user\"\n    }\n  ],\n  \"model\": \"example-model\"\n}")
  .asString();
```

```php
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.example.com/v1/chat/completions', [
  'body' => '{
  "messages": [
    {
      "content": "Hello!",
      "role": "user"
    }
  ],
  "model": "example-model"
}',
  'headers' => [
    'Content-Type' => 'application/json',
  ],
]);

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

```csharp
using RestSharp;

var client = new RestClient("https://api.example.com/v1/chat/completions");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"messages\": [\n    {\n      \"content\": \"Hello!\",\n      \"role\": \"user\"\n    }\n  ],\n  \"model\": \"example-model\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let headers = ["Content-Type": "application/json"]
let parameters = [
  "messages": [
    [
      "content": "Hello!",
      "role": "user"
    ]
  ],
  "model": "example-model"
] as [String : Any]

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

let request = NSMutableURLRequest(url: NSURL(string: "https://api.example.com/v1/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()
```