Skip to main content
Applies to:


Summary

Goal: Configure dataset schemas programmatically via API using the metadata.__schemas field. Features: Dataset API endpoints, metadata.__schemas field, JSON Schema validation with enforce flag.

Configuration Steps

Step 1: Create dataset with schema

POST to /v1/dataset with __schemas in metadata field.
const options = {
  method: "POST",
  headers: {
    Authorization: "Bearer <your-api-key>",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    project_id: "<project-id>",
    name: "<dataset-name>",
    metadata: {
      __schemas: {
        input: {
          type: "object",
          properties: {
            postal: { type: "number" },
          },
          additionalProperties: false,
          enforce: true
        },
      },
    },
  }),
};

fetch("https://api.braintrust.dev/v1/dataset", options)
  .then((res) => res.json())
  .then((res) => console.log(res));

Step 2: Update existing dataset schema

PATCH to /v1/dataset/ with __schemas in metadata field.
const options = {
  method: "PATCH",
  headers: {
    Authorization: "Bearer <your-api-key>",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    metadata: {
      __schemas: {
        input: {
          type: "object",
          properties: {
            postal: { type: "number" },
          },
          additionalProperties: false,
          enforce: true
        },
      },
    },
  }),
};

fetch("https://api.braintrust.dev/v1/dataset/<dataset-id>", options)
  .then((res) => res.json())
  .then((res) => console.log(res));

Step 3: Retrieve existing schema

GET from /v1/dataset/ to view current schema configuration.
const options = {
  method: "GET",
  headers: {
    Authorization: "Bearer <your-api-key>",
  },
};

fetch("https://api.braintrust.dev/v1/dataset/<dataset-id>", options)
  .then((res) => res.json())
  .then((res) => console.log(res.metadata.__schemas));

Step 4: Copy schema structure from UI

Create schema in UI first, then GET the dataset to copy the __schemas object structure for programmatic use.