genai.file.file_service module#

class genai.file.file_service.FileService[source]#

Bases: BaseService[BaseServiceConfig, BaseServiceServices]

create(file_path, purpose, *, origin_id=None)[source]#
Parameters:
  • file_path (str | Path) – The path to the local file that will be uploaded.

  • purpose (str | FilePurpose) – The purpose of the file to be created.

  • origin_id (str | None) – Optional ID of the parent file.

Raises:
  • ValueError – If the file does not exist or if the file format is not supported.

  • ApiResponseException – In case of a known API error.

  • ApiNetworkException – In case of unhandled network error.

Return type:

FileCreateResponse

delete(id)[source]#

Deletes a file with the given ID.

Parameters:

id (str) – The ID of the file to be deleted.

Raises:
Return type:

None

list(*, limit=None, offset=None, sort_by=None, direction=None, search=None, purpose=None, format_id=None)[source]#
Parameters:
  • limit (int | None) – The maximum number of files to retrieve. Defaults to None.

  • offset (int | None) – The number of files to skip before starting retrieval. Defaults to None.

  • sort_by (str | FileListSortBy | None) – The field to sort the files by.

  • direction (str | SortDirection | None) – The sort direction. Can be either “asc” or “desc”. Defaults to None.

  • search (str | None) – The search string to filter files by. Defaults to None.

  • purpose (str | FilePurpose | None) – The purpose of the files. Can be one of the values from the FilePurpose enum. Defaults to None.

  • format_id (int | None) – The ID of the file format. Defaults to None.

Raises:
Return type:

FileRetrieveResponse

read(id)[source]#
Parameters:

id (str) – The ID of the file to be read.

Returns:

The content of the file as a UTF-8 decoded string.

Return type:

str

Raises:
retrieve(id)[source]#
Parameters:

id (str) – The ID of the file to retrieve.

Returns:

The response object containing the retrieved file information.

Return type:

FileIdRetrieveResponse

Raises:
update(id, *, file_path)[source]#

Replace the file content.

Parameters:
  • id (str) – The ID of the file to be updated.

  • file_path (str | Path) – The path to the local file that will be uploaded.

Raises:
  • ValueError – If the file does not exist or if the file format is not supported.

  • ApiResponseException – In case of a known API error.

  • ApiNetworkException – In case of unhandled network error.

Return type:

FileIdPatchResponse