POST
/
studio
/
v1
/
library
/
files
rom ai21 import AI21Client

client = AI21Client()

# Option 1: Upload a local file
file_from_disk = client.library.files.create(
    file_path="path/to/local/file.pdf",
    path="docs/file-from-disk.pdf",
    labels=["label1", "label2"],
)

# Option 2: Upload a file from a public URL
file_from_url = client.library.files.create(
    public_url="https://www.example.com/file.pdf",
    path="docs/file-from-url.pdf",
    labels=["label1", "label2"],
)

# Retrieve uploaded file metadata (example)
uploaded_file = client.library.files.get(file_from_disk.id)
{
  "id": "da13301a-14e4-4487-aa2f-cc6048e73cdc",
}

You can assign metadata to your files to limit searches to specific files by file metadata. There is no bulk upload method; files must be loaded one at a time.

  • Max number of files: No limit. The playground limits bulk uploads to 50 files per request.
  • Max total library size: 1 GB.
  • Max file size: 5 MB.
  • Supported file types: PDF, DocX, HTML, TXT, Markdown.

Request body fields

file
string
required

Raw file bytes. Every uploaded file must have a unique file name, not including the local file path. Specifying file + path or file + labels will find only files with both the specified name and path/label. SDK NOTE In the SDK this parameter is replaced with file_path, which is the local string path and name of the file, not the file bytes.

path
string

An arbitrary file-path-like string that indicates the content of the file. Can be used to limit the scope of files in a query or search request. See above for more details about paths. Paths are case-sensitive. Specifying path + file or path + labels will return only files with both the specified path and name/label.

labels
string[]

Arbitrary string labels that describe the contents of this file. Labels are case-sensitive. Specifying labels + path or labels + name will return only files with both any of the specified labels and the specified name/path.

publicUrl
string

A public URL associated with the file, if any. Only used as metadata, to indicate the location of the source file. For example, if implementing a search engine against a website, specifying a URL for each uploaded file is a simple way to present the link to the file in the search results presented to the user. (Tip: You can also provide a file path or any arbitrary string: URL validity is not checked.)

Responses

Response 200

A unique identifier for the uploaded file. Use this later to request, modify, or delete the file. You don’t need to store the value though, as it is returned along with all file information in a GET /files request. Type: UUID. Example: da13301a-14e4-4487-aa2f-cc6048e73cdc // file-uuid

Error: Unsupported document type 422

Error message:

{
    "detail": "Invalid file type: image/png. Supported file types are: text/plain, text/html, application/docx, application/pdf"
}

Error: Same file name, same path 422

Error message:

{
  "detail": "File: {fileName} already exists",
  "suggestion": "To override the file content, delete it first using the DELETE endpoint"
}
{
  "id": "da13301a-14e4-4487-aa2f-cc6048e73cdc",
}
rom ai21 import AI21Client

client = AI21Client()

# Option 1: Upload a local file
file_from_disk = client.library.files.create(
    file_path="path/to/local/file.pdf",
    path="docs/file-from-disk.pdf",
    labels=["label1", "label2"],
)

# Option 2: Upload a file from a public URL
file_from_url = client.library.files.create(
    public_url="https://www.example.com/file.pdf",
    path="docs/file-from-url.pdf",
    labels=["label1", "label2"],
)

# Retrieve uploaded file metadata (example)
uploaded_file = client.library.files.get(file_from_disk.id)