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

# Update guardrail policy

PUT http://localhost:5173/api/v1/private/guardrails/policies/{id}
Content-Type: application/json

Replace a guardrail policy, rules included: a rule left out of the request is removed

Reference: https://www.comet.com/docs/opik/reference/rest-api/guardrail-policies/update-guardrail-policy

## Servers

- `http://localhost:5173/api` (Local server, default)
- `https://www.comet.com/opik/api` (Opik Cloud)

## Request

### Path parameters

- `id` (string, required)

### Body (application/json)

This endpoint expects a GuardrailPolicy_Write.

- `name` (string, required) — The SDK names policies by name, so it is unique in the workspace, ignoring case
- `rules` (list of GuardrailPolicyRule_Write, required) — At most one rule per type
- `id` (string, optional) — Optional on create, generated when missing, as for prompts and datasets. Ignored on update, where the path names the policy
- `description` (string, optional)
- `execution_scope` (enum, optional, default: on_request) — on_request applies the policy when the SDK names it, global applies it to every guardrail built from stored policies in the workspace. Missing or null means on_request
  - Allowed values: `on_request`, `global`

## Response

### 204

No Content

## Errors

### 400 Bad Request Error

Bad Request

- `code` (integer, optional)
- `message` (string, optional)
- `details` (string, optional)

### 403 Forbidden Error

The caller lacks the workspace settings permission, or guardrails are not enabled for this deployment or the workspace's organization

- `code` (integer, optional)
- `message` (string, optional)
- `details` (string, optional)

### 404 Not Found Error

Guardrail policy not found

- `code` (integer, optional)
- `message` (string, optional)
- `details` (string, optional)

### 409 Conflict Error

A policy with this name already exists

- `code` (integer, optional)
- `message` (string, optional)
- `details` (string, optional)

### 422 Unprocessable Entity Error

Unprocessable Content

- `code` (integer, optional)
- `message` (string, optional)
- `details` (string, optional)

### 429 Too Many Requests Error

Too many requests

- `code` (integer, optional)
- `message` (string, optional)
- `details` (string, optional)

### 503 Service Unavailable Error

Could not verify with the platform that guardrails are enabled for the workspace's organization; retry later

- `code` (integer, optional)
- `message` (string, optional)
- `details` (string, optional)

## Types

### GuardrailPolicyRule_Write

At most one rule per type

- `type` (enum, required)
  - Allowed values: `TOPIC`, `PII`, `LLM_JUDGE`, `PROMPT_INJECTION`, `CUSTOM_CLASSIFIER`
- `config` (JsonNode, required)

### JsonNode

## Examples

**Request**

```json
{
  "name": "string",
  "rules": [
    {
      "type": "TOPIC",
      "config": {}
    }
  ]
}
```

**SDK Code**

```python
import requests

url = "http://localhost:5173/api/v1/private/guardrails/policies/id"

payload = {
    "name": "string",
    "rules": [
        {
            "type": "TOPIC",
            "config": {}
        }
    ]
}
headers = {"Content-Type": "application/json"}

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

print(response.json())
```

```javascript
const url = 'http://localhost:5173/api/v1/private/guardrails/policies/id';
const options = {
  method: 'PUT',
  headers: {'Content-Type': 'application/json'},
  body: '{"name":"string","rules":[{"type":"TOPIC","config":{}}]}'
};

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 := "http://localhost:5173/api/v1/private/guardrails/policies/id"

	payload := strings.NewReader("{\n  \"name\": \"string\",\n  \"rules\": [\n    {\n      \"type\": \"TOPIC\",\n      \"config\": {}\n    }\n  ]\n}")

	req, _ := http.NewRequest("PUT", 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("http://localhost:5173/api/v1/private/guardrails/policies/id")

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

request = Net::HTTP::Put.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n  \"name\": \"string\",\n  \"rules\": [\n    {\n      \"type\": \"TOPIC\",\n      \"config\": {}\n    }\n  ]\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.put("http://localhost:5173/api/v1/private/guardrails/policies/id")
  .header("Content-Type", "application/json")
  .body("{\n  \"name\": \"string\",\n  \"rules\": [\n    {\n      \"type\": \"TOPIC\",\n      \"config\": {}\n    }\n  ]\n}")
  .asString();
```

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

$client = new \GuzzleHttp\Client();

$response = $client->request('PUT', 'http://localhost:5173/api/v1/private/guardrails/policies/id', [
  'body' => '{
  "name": "string",
  "rules": [
    {
      "type": "TOPIC",
      "config": {}
    }
  ]
}',
  'headers' => [
    'Content-Type' => 'application/json',
  ],
]);

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

```csharp
using RestSharp;

var client = new RestClient("http://localhost:5173/api/v1/private/guardrails/policies/id");
var request = new RestRequest(Method.PUT);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"name\": \"string\",\n  \"rules\": [\n    {\n      \"type\": \"TOPIC\",\n      \"config\": {}\n    }\n  ]\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let headers = ["Content-Type": "application/json"]
let parameters = [
  "name": "string",
  "rules": [
    [
      "type": "TOPIC",
      "config": []
    ]
  ]
] as [String : Any]

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

let request = NSMutableURLRequest(url: NSURL(string: "http://localhost:5173/api/v1/private/guardrails/policies/id")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "PUT"
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()
```