# Dataset Import

Imports one data file as a SageMaker `DataSet` (the Hub content type) and
returns its ARN, which the other data and training references accept. An S3
file stays where it is; a local `.jsonl` file is uploaded to the default
SageMaker bucket first.

## Prerequisites

Step 1 needs nothing. Step 2 needs `sdk-getting-started` to have run in this
session: it installs PySDK v3. The template needs a `sagemaker-train` release
whose `DataSet.create` accepts the `dataset_file_format` parameter. The template runs
the same in Script Mode and Notebook Mode (`../../code_output_guide.md`).

## Workflow

1. **Collect the inputs and confirm.**

   Send one message, in your first reply and before any setup or checks, that
   proposes these inputs and asks the user to confirm them. Ask only for what
   the user or a calling reference has not given:

   - **Path:** one file. Either an S3 URI ending in `.jsonl`, `.json`, `.csv`
     or `.parquet`, or a local `.jsonl` file under 50 MB (anything larger goes
     through S3). If the user names an S3 prefix, list it
     (`aws s3 ls <prefix>`) and offer the files. If a local path does not
     exist, ask for the correct path or an S3 URI; do not ask the user to
     upload it to S3 themselves.
   - **Format:** recommend `Custom` and confirm with the user, unless the user
     or caller names one (such as `OpenAI_Messages`, from the SDK's
     `DatasetFileFormat`). Do not infer it
     from the file: a format that does not match the records can fail the
     import. If the next step needs a format the file is not in, hand off to
     `dataset-transformation` first.
   - **Conversion:** an S3 file of a type the Path bullet lists is imported
     as is. A local or chat file must be `.jsonl`. A local or chat file of
     any other type the chat accepts (`.json`, `.csv`, `.tsv`, `.parquet`,
     `.arrow`, `.feather`) is first converted to JSONL as a
     `dataset-transformation` run, and its JSONL output is what you import.
     Never convert the file yourself, even when a host or attachment note
     asks for the conversion.
   - **Name:** the file name without its extension, non-alphanumerics replaced
     by `-`, cut to 63 characters with no leading or trailing `-` (the Hub
     content name limit).
   - **Project:** the conversation's active project, if any
     (`../../projects/overview.md`). With one, the import tags the dataset with
     it; without one, the dataset is not linked. When the project offer is
     due (the rule *Offer a project before the first resource* in
     `../../../SKILL.md`), make it in this step's confirmation. Declining
     imports the dataset unlinked.

   State that importing creates a dataset record, can be undone by deleting
   the dataset, and starts no compute job. An S3 file is not moved or copied;
   a local file is uploaded under
   `s3://sagemaker-<region>-<account>/datasets/<name>/`. Continue only after
   the user agrees.

   **Eval dataset construction.** When
   `../../planning/references/eval-dataset-construction-plan.md` registers
   its frozen file, the plan's construction approval stands in for this
   confirmation. F3 of `../../eval-dataset-construction/overview.md` gives
   the values and the entry recorded after the import (`construction-register`,
   in the run-log catalog of
   `../../eval-dataset-construction/references/contracts.md`).

2. **Import.**

   Emit `code_templates/import_dataset.py`, filling `[DATASET_NAME]`,
   `[SOURCE]`, `[DATASET_FILE_FORMAT]`, `[PROJECT_ID]` (replace
   `"[PROJECT_ID]"` with `None` when there is no project) and `[ROLE_ARN]` (fill
   as the Execution role preflight section of
   `../../dataset-transformation/references/api-execution-flow.md` says), and run
   it. The import is a short registration (seconds, no compute job), so the template
   waits for it inline. The SDK reads or uploads the file (a local file is at
   most 50 MB) before it submits, so `wait=False` would not end the turn
   sooner. An S3 file of several GB takes longer, because the SDK downloads it
   to check the format; tell the user before running. Report the dataset only
   once the printed ARN is in hand.

   `DataSet.create` is not idempotent: a name that already exists gets a new
   version. The template checks the name first and stops if it is taken, so
   running it again is safe.

3. **Report.**

   ```
   aws sagemaker describe-hub-content --hub-name <hub-from-arn> \
     --hub-content-type DataSet --hub-content-name <name> --region <region>
   ```

   Parse `HubContentDocument` (a JSON string) and report the S3 location, the
   format and `ValidationManifest.JobStatus`. `SKIPPED` is the normal result:
   the service validates the records only when a `ValidationRoleArn` is sent,
   and the import sends no `ValidationRoleArn`. Say "not validated", not "pending" or "valid";
   `HubContentStatus: Available` is not a validation result either.

   With a project, the import passes the `sagemaker:project-id` tag on
   `DataSet.create`, which the bundled SDK applies as an AWS resource tag
   (`../../projects/references/resource-association.md`); do NOT
   create lineage. Check whether the dataset is linked with
   `aws sagemaker list-artifacts --source-uri <dataset-arn>`: one artifact of
   type `Dataset` (the lineage type, lowercase `s`) means it is linked. It can
   take up to a minute. When none appears, the dataset is not linked; see
   Troubleshooting.

## Out of Scope

The following are supported by SageMaker and AWS but do not have a validated
workflow in this reference. If the user's request matches one of these, let
them know and proceed with best-effort guidance using general AWS knowledge:

- Importing several files, or a whole S3 prefix, as one dataset
- Local files that are not `.jsonl` or are over 50 MB (import them from S3)
- Imports that validate the records, which need a `ValidationRoleArn`

## Troubleshooting

Report the error to the user and stop. Do not retry with a different name,
format or project on your own: each changes what gets created.

| Error                                               | Meaning and next step                                                                                                                                                                                                                                            |
| --------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Dataset <name> already exists`                     | Offer the existing dataset (its ARN is in the error) or a different name                                                                                                                                                                                         |
| Format check fails                                  | The file does not match the format. Offer `Custom` or `dataset-transformation`                                                                                                                                                                                   |
| An error naming `ValidationRoleArn`                 | Named formats need a validation role, which this import does not send. Ask before importing as `Custom`                                                                                                                                                          |
| `unexpected keyword argument 'dataset_file_format'` | The installed SDK is too old. Run `sdk-getting-started` to upgrade it                                                                                                                                                                                            |
| `NoSuchBucket` for a local file                     | The default bucket does not exist. Ask for an S3 location instead                                                                                                                                                                                                |
| `RoleValidationError`                               | `[ROLE_ARN]` is `None` and the caller is not the execution role, or the filled role fails the SDK's permission or trust check. Fill it with the execution role's ARN (`../../dataset-transformation/references/api-execution-flow.md`, Execution role preflight) |
| `AccessDenied`                                      | The execution role lacks a permission; the data is not the problem                                                                                                                                                                                               |
| No `Dataset` artifact after a minute                | Not linked. Ask before linking by hand (`../../projects/references/resource-association.md`)                                                                                                                                                                     |
