> 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.

# Upsert optimization

PUT http://localhost:5173/api/v1/private/optimizations
Content-Type: application/json

Upsert optimization

Reference: https://www.comet.com/docs/opik/reference/rest-api/optimizations/upsert-optimization

## Servers

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

## Request

### Body (application/json)

This endpoint expects an Optimization_Write.

- `dataset_name` (string, required)
- `objective_name` (string, required)
- `status` (enum, required)
  - Allowed values: `running`, `completed`, `cancelled`, `initialized`, `error`
- `id` (string, optional)
- `name` (string, optional)
- `project_name` (string, optional) — Project name. Creates project if it doesn't exist. Ignored when project_id is provided.
- `project_id` (string, optional) — Project ID. Takes precedence over project_name when both are provided.
- `metadata` (JsonListString_Write, optional)
- `studio_config` (OptimizationStudioConfig_Write, optional)
- `error_info` (ErrorInfo_Write, optional)
- `last_updated_at` (datetime, optional)

## Response

### 201

Created

## Types

### JsonListString_Write

### OptimizationStudioConfig_Write

- `dataset_name` (string, required)
- `prompt` (StudioPrompt_Write, required)
- `llm_model` (StudioLlmModel_Write, required)
- `evaluation` (StudioEvaluation_Write, required)
- `optimizer` (StudioOptimizer_Write, required)

### ErrorInfo_Write

- `exception_type` (string, required)
- `traceback` (string, required)
- `message` (string, optional)

### StudioPrompt_Write

- `messages` (list of StudioMessage_Write, required)

### StudioLlmModel_Write

- `model` (string, required)
- `parameters` (JsonNode_Write, optional)

### StudioEvaluation_Write

- `metrics` (list of StudioMetric_Write, required)

### StudioOptimizer_Write

- `type` (string, required)
- `parameters` (JsonNode_Write, optional)

### StudioMessage_Write

- `role` (string, required)
- `content` (string, required)

### JsonNode_Write

### StudioMetric_Write

- `type` (string, required)
- `parameters` (JsonNode_Write, optional)

## Examples

**Request**

```json
{
  "dataset_name": "string",
  "objective_name": "string",
  "status": "running"
}
```

**Response**

```json
{}
```

**SDK Code**

```python
import requests

url = "http://localhost:5173/api/v1/private/optimizations"

payload = {
    "dataset_name": "string",
    "objective_name": "string",
    "status": "running"
}
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/optimizations';
const options = {
  method: 'PUT',
  headers: {'Content-Type': 'application/json'},
  body: '{"dataset_name":"string","objective_name":"string","status":"running"}'
};

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/optimizations"

	payload := strings.NewReader("{\n  \"dataset_name\": \"string\",\n  \"objective_name\": \"string\",\n  \"status\": \"running\"\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/optimizations")

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

request = Net::HTTP::Put.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n  \"dataset_name\": \"string\",\n  \"objective_name\": \"string\",\n  \"status\": \"running\"\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/optimizations")
  .header("Content-Type", "application/json")
  .body("{\n  \"dataset_name\": \"string\",\n  \"objective_name\": \"string\",\n  \"status\": \"running\"\n}")
  .asString();
```

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

$client = new \GuzzleHttp\Client();

$response = $client->request('PUT', 'http://localhost:5173/api/v1/private/optimizations', [
  'body' => '{
  "dataset_name": "string",
  "objective_name": "string",
  "status": "running"
}',
  'headers' => [
    'Content-Type' => 'application/json',
  ],
]);

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

```csharp
using RestSharp;

var client = new RestClient("http://localhost:5173/api/v1/private/optimizations");
var request = new RestRequest(Method.PUT);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"dataset_name\": \"string\",\n  \"objective_name\": \"string\",\n  \"status\": \"running\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let headers = ["Content-Type": "application/json"]
let parameters = [
  "dataset_name": "string",
  "objective_name": "string",
  "status": "running"
] as [String : Any]

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

let request = NSMutableURLRequest(url: NSURL(string: "http://localhost:5173/api/v1/private/optimizations")! 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()
```