Skip to content

Files

The files SDK allows you to manage files uploaded to the PromptIDE. Its main purposes is to grant file access when executing prompts created in the IDE locally.

API reference

xai_sdk.files.AsyncFiles

Contains API for interacting with files.

Source code in xai_sdk/files.py
class AsyncFiles:
    """Contains API for interacting with files."""

    def __init__(self, stub: files_pb2_grpc.FileStub) -> None:
        """Initializes a new instance of the `AsyncFiles` class.

        Args:
            stub: Stub used to communicate with the gRPC API.
        """
        self._stub = stub

    async def download(self, file_name: str) -> bytes:
        """Reads a file store in the IDE.

        Args:
            file_name: Name of the file. This is case-sensitive.

        Returns:
            The file contents in bytes.
        """
        response: files_pb2.FileObject = await self._stub.DownloadFile(
            files_pb2.DownloadFileRequest(file_name=file_name)
        )
        return response.content

    async def upload_file(self, local_file_name: str, remote_file_name: str) -> None:
        """Uploads a locally stored file to the IDE's storage.

        Args:
            local_file_name: Local filename.
            remote_file_name: Name of the file in the IDE.
        """
        # Read the file content.
        with open(local_file_name, "rb") as f:
            await self.upload(remote_file_name, f.read())

    async def upload(self, file_name: str, content: bytes, mime_type: str = "", overwrite: bool = True) -> None:
        """Creates a new file in the IDE.

        Args:
            file_name: Name of the file in the IDE.
            content: File contents.
            mime_type: Mime type of the file.
            overwrite: True if the file shall be overwritten when it's re-uploaded.
        """
        await self._stub.UploadFile(
            files_pb2.UploadFileRequest(file_name=file_name, content=content, mime_type=mime_type, overwrite=overwrite)
        )

    async def list(self) -> list[files_pb2.FileMetadata]:
        """Returns the metadata of all files stored in the IDE.

        Returns:
            Metadata of all uploaded files.
        """
        response: files_pb2.ListFilesResponse = await self._stub.ListFiles(empty_pb2.Empty())
        return list(response.files)

    async def delete(self, file_name: str) -> None:
        """Deletes a file.

        Args:
            file_name: Name of the file to delete.
        """
        await self._stub.DeleteFile(files_pb2.DeleteFileRequest(file_name=file_name))

xai_sdk.files.AsyncFiles.__init__(stub)

Initializes a new instance of the AsyncFiles class.

Parameters:

Name Type Description Default
stub FileStub

Stub used to communicate with the gRPC API.

required
Source code in xai_sdk/files.py
def __init__(self, stub: files_pb2_grpc.FileStub) -> None:
    """Initializes a new instance of the `AsyncFiles` class.

    Args:
        stub: Stub used to communicate with the gRPC API.
    """
    self._stub = stub

xai_sdk.files.AsyncFiles.delete(file_name) async

Deletes a file.

Parameters:

Name Type Description Default
file_name str

Name of the file to delete.

required
Source code in xai_sdk/files.py
async def delete(self, file_name: str) -> None:
    """Deletes a file.

    Args:
        file_name: Name of the file to delete.
    """
    await self._stub.DeleteFile(files_pb2.DeleteFileRequest(file_name=file_name))

xai_sdk.files.AsyncFiles.download(file_name) async

Reads a file store in the IDE.

Parameters:

Name Type Description Default
file_name str

Name of the file. This is case-sensitive.

required

Returns:

Type Description
bytes

The file contents in bytes.

Source code in xai_sdk/files.py
async def download(self, file_name: str) -> bytes:
    """Reads a file store in the IDE.

    Args:
        file_name: Name of the file. This is case-sensitive.

    Returns:
        The file contents in bytes.
    """
    response: files_pb2.FileObject = await self._stub.DownloadFile(
        files_pb2.DownloadFileRequest(file_name=file_name)
    )
    return response.content

xai_sdk.files.AsyncFiles.list() async

Returns the metadata of all files stored in the IDE.

Returns:

Type Description
list[FileMetadata]

Metadata of all uploaded files.

Source code in xai_sdk/files.py
async def list(self) -> list[files_pb2.FileMetadata]:
    """Returns the metadata of all files stored in the IDE.

    Returns:
        Metadata of all uploaded files.
    """
    response: files_pb2.ListFilesResponse = await self._stub.ListFiles(empty_pb2.Empty())
    return list(response.files)

xai_sdk.files.AsyncFiles.upload(file_name, content, mime_type='', overwrite=True) async

Creates a new file in the IDE.

Parameters:

Name Type Description Default
file_name str

Name of the file in the IDE.

required
content bytes

File contents.

required
mime_type str

Mime type of the file.

''
overwrite bool

True if the file shall be overwritten when it's re-uploaded.

True
Source code in xai_sdk/files.py
async def upload(self, file_name: str, content: bytes, mime_type: str = "", overwrite: bool = True) -> None:
    """Creates a new file in the IDE.

    Args:
        file_name: Name of the file in the IDE.
        content: File contents.
        mime_type: Mime type of the file.
        overwrite: True if the file shall be overwritten when it's re-uploaded.
    """
    await self._stub.UploadFile(
        files_pb2.UploadFileRequest(file_name=file_name, content=content, mime_type=mime_type, overwrite=overwrite)
    )

xai_sdk.files.AsyncFiles.upload_file(local_file_name, remote_file_name) async

Uploads a locally stored file to the IDE's storage.

Parameters:

Name Type Description Default
local_file_name str

Local filename.

required
remote_file_name str

Name of the file in the IDE.

required
Source code in xai_sdk/files.py
async def upload_file(self, local_file_name: str, remote_file_name: str) -> None:
    """Uploads a locally stored file to the IDE's storage.

    Args:
        local_file_name: Local filename.
        remote_file_name: Name of the file in the IDE.
    """
    # Read the file content.
    with open(local_file_name, "rb") as f:
        await self.upload(remote_file_name, f.read())