> 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.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://www.comet.com/docs/opik/_mcp/server.

# Expand dataset with synthetic samples

POST http://localhost:5173/api/v1/private/datasets/{id}/expansions
Content-Type: application/json

Generate synthetic dataset samples using LLM based on existing data patterns

Reference: https://www.comet.com/docs/opik/reference/rest-api/datasets/expand-dataset

## 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 an object.

- `model` (string, required) — The model to use for synthetic data generation
- `sample_count` (integer, optional) — Number of synthetic samples to generate
- `preserve_fields` (list of string, optional) — Fields to preserve patterns from original data
- `variation_instructions` (string, optional) — Additional instructions for data variation
- `custom_prompt` (string, optional) — Custom prompt to use for generation instead of auto-generated one
- `max_completion_tokens` (integer, optional) — Maximum number of tokens for the LLM response. Required by Anthropic, used as maxOutputTokens for Gemini. If not provided, defaults to 4000 for Anthropic models only.

## Response

### 200

Generated synthetic samples

- `generated_samples` (list of object, optional) — List of generated synthetic dataset items
  - `source` (enum, required)
    - Allowed values: `manual`, `trace`, `span`, `sdk`
  - `data` (object, required)
  - `id` (string, optional) — Stable item identifier. On write, used as the upsert key. If omitted, a new ID is generated. Remains the same across dataset versions
  - `dataset_item_id` (string, optional) — Deprecated. Always equals 'id'. Retained for backward compatibility and will be removed in a future version
  - `trace_id` (string, optional)
  - `span_id` (string, optional)
  - `description` (string, optional)
  - `tags` (list of string, optional)
  - `evaluators` (list of object, optional)
    - `name` (string, required)
    - `type` (enum, required)
      - Allowed values: `llm_judge`, `code_metric`
    - `config` (object, required)
  - `execution_policy` (object, optional)
    - `runs_per_item` (integer, optional)
    - `pass_threshold` (integer, optional)
  - `experiment_items` (list of object, optional)
    - `experiment_id` (string, required)
    - `dataset_item_id` (string, required)
    - `trace_id` (string, required)
    - `id` (string, optional)
    - `project_id` (string, optional)
    - `project_name` (string, optional)
    - `input` (map from string to any or list of object or string, optional)
    - `output` (map from string to any or list of object or string, optional)
    - `trace_metadata` (map from string to any or list of object or string, optional)
    - `feedback_scores` (list of object, optional)
      - `name` (string, required)
      - `value` (double, required)
      - `source` (enum, required)
        - Allowed values: `ui`, `sdk`, `online_scoring`
      - `category_name` (string, optional)
      - `reason` (string, optional)
      - `source_queue_id` (string, optional)
      - `created_at` (datetime, optional)
      - `last_updated_at` (datetime, optional)
      - `created_by` (string, optional)
      - `last_updated_by` (string, optional)
      - `value_by_author` (map from string to object, optional)
        - `value` (double, optional)
        - `reason` (string, optional)
        - `category_name` (string, optional)
        - `source` (enum, optional)
          - Allowed values: `ui`, `sdk`, `online_scoring`
        - `last_updated_at` (datetime, optional)
        - `span_type` (string, optional)
        - `span_id` (string, optional)
        - `source_queue_id` (string, optional)
        - `author` (string, optional)
    - `comments` (list of object, optional)
      - `text` (string, required)
      - `id` (string, optional)
      - `source_queue_id` (string, optional)
      - `created_at` (datetime, optional)
      - `last_updated_at` (datetime, optional)
      - `created_by` (string, optional)
      - `last_updated_by` (string, optional)
    - `total_estimated_cost` (double, optional)
    - `duration` (double, optional)
    - `usage` (map from string to long, optional)
    - `created_at` (datetime, optional)
    - `last_updated_at` (datetime, optional)
    - `created_by` (string, optional)
    - `last_updated_by` (string, optional)
    - `trace_visibility_mode` (enum, optional)
      - Allowed values: `default`, `hidden`
    - `description` (string, optional)
    - `execution_policy` (object, optional)
      - `runs_per_item` (integer, optional)
      - `pass_threshold` (integer, optional)
    - `assertion_results` (list of object, optional)
      - `value` (string, optional)
      - `passed` (boolean, optional)
      - `reason` (string, optional)
    - `status` (enum, optional)
      - Allowed values: `passed`, `failed`
  - `run_summaries_by_experiment` (map from string to object, optional)
    - `passed_runs` (integer, optional)
    - `total_runs` (integer, optional)
    - `status` (enum, optional)
      - Allowed values: `passed`, `failed`
  - `dataset_id` (string, optional)
  - `created_at` (datetime, optional)
  - `last_updated_at` (datetime, optional)
  - `created_by` (string, optional)
  - `last_updated_by` (string, optional)
- `model` (string, optional) — Model used for generation
- `total_generated` (integer, optional) — Total number of samples generated
- `generation_time` (datetime, optional) — Generation timestamp

## Examples

**Request**

```json
{
  "model": "gpt-4"
}
```

**Response**

```json
{
  "generated_samples": [
    {
      "source": "manual",
      "data": {},
      "id": "string",
      "dataset_item_id": "string",
      "trace_id": "string",
      "span_id": "string",
      "description": "string",
      "tags": [
        "string"
      ],
      "evaluators": [
        {
          "name": "string",
          "type": "llm_judge",
          "config": {}
        }
      ],
      "execution_policy": {
        "runs_per_item": 1,
        "pass_threshold": 1
      },
      "experiment_items": [
        {
          "experiment_id": "string",
          "dataset_item_id": "string",
          "trace_id": "string",
          "id": "string",
          "project_id": "string",
          "project_name": "string",
          "input": {},
          "output": {},
          "trace_metadata": {},
          "feedback_scores": [
            {
              "name": "string",
              "value": 1.1,
              "source": "ui",
              "category_name": "string",
              "reason": "string",
              "source_queue_id": "string",
              "created_at": "2024-01-15T09:30:00Z",
              "last_updated_at": "2024-01-15T09:30:00Z",
              "created_by": "string",
              "last_updated_by": "string",
              "value_by_author": {}
            }
          ],
          "comments": [
            {
              "text": "string",
              "id": "string",
              "source_queue_id": "string",
              "created_at": "2024-01-15T09:30:00Z",
              "last_updated_at": "2024-01-15T09:30:00Z",
              "created_by": "string",
              "last_updated_by": "string"
            }
          ],
          "total_estimated_cost": 1.1,
          "duration": 1.1,
          "usage": {},
          "created_at": "2024-01-15T09:30:00Z",
          "last_updated_at": "2024-01-15T09:30:00Z",
          "created_by": "string",
          "last_updated_by": "string",
          "trace_visibility_mode": "default",
          "description": "string",
          "execution_policy": {
            "runs_per_item": 1,
            "pass_threshold": 1
          },
          "assertion_results": [
            {
              "value": "string",
              "passed": true,
              "reason": "string"
            }
          ],
          "status": "passed"
        }
      ],
      "run_summaries_by_experiment": {},
      "dataset_id": "string",
      "created_at": "2024-01-15T09:30:00Z",
      "last_updated_at": "2024-01-15T09:30:00Z",
      "created_by": "string",
      "last_updated_by": "string"
    }
  ],
  "model": "gpt-4",
  "total_generated": 10,
  "generation_time": "2024-01-15T09:30:00Z"
}
```

**SDK Code**

```python
import requests

url = "http://localhost:5173/api/v1/private/datasets/id/expansions"

payload = { "model": "gpt-4" }
headers = {"Content-Type": "application/json"}

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

print(response.json())
```

```javascript
const url = 'http://localhost:5173/api/v1/private/datasets/id/expansions';
const options = {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: '{"model":"gpt-4"}'
};

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/datasets/id/expansions"

	payload := strings.NewReader("{\n  \"model\": \"gpt-4\"\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("http://localhost:5173/api/v1/private/datasets/id/expansions")

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

request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n  \"model\": \"gpt-4\"\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("http://localhost:5173/api/v1/private/datasets/id/expansions")
  .header("Content-Type", "application/json")
  .body("{\n  \"model\": \"gpt-4\"\n}")
  .asString();
```

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

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'http://localhost:5173/api/v1/private/datasets/id/expansions', [
  'body' => '{
  "model": "gpt-4"
}',
  'headers' => [
    'Content-Type' => 'application/json',
  ],
]);

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

```csharp
using RestSharp;

var client = new RestClient("http://localhost:5173/api/v1/private/datasets/id/expansions");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"model\": \"gpt-4\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let headers = ["Content-Type": "application/json"]
let parameters = ["model": "gpt-4"] as [String : Any]

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

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