> ## Documentation Index
> Fetch the complete documentation index at: https://docs.modaic.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Optimizing Instructions (GEPA)

> Improve question instructions using uploaded labeled datasets.

Instruction optimization automates the experimentation in prompt engineering. Using an adapted version of [GEPA](https://arxiv.org/abs/2507.19457), it proposes alternative question instructions, evaluates them against labeled examples, and uses feedback to guide further revisions. It improves the instructions without changing model weights.

Start from a base model or an existing saved model. For weight training, see [fine-tuning](https://docs.modaic.dev/docs/models/finetuning_models.md).

## 1. Prepare labeled data

Save one object per line in `train.jsonl`, using the same [dataset format as fine-tuning](https://docs.modaic.dev/docs/dataset/guidance.md). These two rows show the format; include enough representative rows for nonempty training, validation, and held-out test partitions.

```jsonl theme={null}
{"state":"Checkout is unavailable.","questions":{"urgent":{"type":"noul","instructions":"Does this require immediate attention?","answer":1}}}
{"state":"Can I change my invoice address?","questions":{"urgent":{"type":"noul","instructions":"Does this require immediate attention?","answer":0}}}
```

Every row must use one shared set of question IDs and definitions, including nonempty text instructions. Use hard labels: Noul `0` or `1`, keyed Choice with a single option label, or ordered Score with a single level index. Optimization does not accept soft-label distributions.

Data is split deterministically. You can supply `validation_file` and `eval_only_dataset` to replace the validation and held-out evaluation partitions. Keep inputs and groups disjoint across partitions. Uploaded files are limited to 64 MiB combined.

## 2. Upload the dataset

Upload `train.jsonl` as a dataset named `triage-labels` in your workspace. Wait for validation to complete.

```python Python theme={null}
from modaic import Modaic

client = Modaic()
upload = client.uploads.upload_file("acme", "train.jsonl", kind="dataset", name="triage-labels")
if upload.status != "completed" or upload.result is None:
    raise RuntimeError(upload.error)
```

```typescript TypeScript theme={null}
import { Modaic } from "modaic";

const client = new Modaic();
const upload = await client.uploads.uploadFile("train.jsonl", {
  workspace: "acme", kind: "dataset", name: "triage-labels",
});
if (upload.status !== "completed" || !upload.result) throw new Error(upload.error?.message);
```

```bash CLI theme={null}
modaic datasets upload acme train.jsonl --kind dataset --name triage-labels
```

## 3. Optimize instructions

Start from a base model and choose the saved destination for your optimized instructions.

```python Python theme={null}
job = client.instruction_optimizations.create(
    model="mo-latest",
    destination="acme/support-priority",
    training_file=upload.result.name,
    idempotency_key="support-optimize-v1",
)
print(job.id)
```

```typescript TypeScript theme={null}
const job = await client.instructionOptimizations.create({
  model: "mo-latest",
  destination: "acme/support-priority",
  trainingFile: upload.result.name,
  idempotencyKey: "support-optimize-v1",
});
console.log(job.id);
```

```bash cURL theme={null}
curl --fail-with-body https://modaic.dev/api/v1/instruction-optimizations \
  -H "Authorization: Bearer $MODAIC_API_KEY" \
  -H 'Idempotency-Key: support-optimize-v1' \
  --json '{"model":"mo-latest","destination":"acme/support-priority","training_file":"triage-labels"}'
```

```bash CLI theme={null}
modaic instruct-opts create --model mo-latest \
  --destination acme/support-priority --training-file triage-labels
```

To optimize an existing saved model, set `model` to `acme/support-priority` and omit `destination`. Its question IDs, types, and criteria must match the dataset; its current instructions provide the starting point. The API resolves the branch head at creation unless you supply a source commit. Avoid editing that branch until the job finishes.

## 4. Review the result

Candidate selection uses validation data. Final accuracy and calibration metrics use held-out data that was not used for candidate selection. An improving run records a new model revision; a model without saved questions also receives its initial question definitions.

```python Python theme={null}
finished = client.instruction_optimizations.wait(job.id, progress=True)
print(finished.status, finished.result_commit_sha)
```

```typescript TypeScript theme={null}
const finished = await client.instructionOptimizations.wait(job.id, { progress: true });
console.log(finished.status, finished.resultCommitSha);
```

```bash cURL theme={null}
curl --fail-with-body "https://modaic.dev/api/v1/instruction-optimizations/$JOB_ID" \
  -H "Authorization: Bearer $MODAIC_API_KEY"
```

```bash CLI theme={null}
modaic instruct-opts get JOB_ID
```

Modaic Platform shows job progress, logs, and results, and lets you cancel a job. Start instruction optimization through the API, SDK, or CLI. Optimization uses uploaded datasets only and does not automatically rerun predictions on stored model examples.

See the [API reference](https://docs.modaic.dev/docs/api-reference/instruction-optimizations/start-instruction-optimization.md) for job options and [Fine-tuning](https://docs.modaic.dev/docs/models/finetuning_models.md) for weight training.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.