CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-groq

The official Python library for the groq API

Pending
Overview
Eval results
Files

files.mddocs/

File Operations

Upload, manage, and organize files for use with various Groq services and batch processing. The files API provides comprehensive file management capabilities for storing and organizing data used with other API endpoints.

Capabilities

Upload File

Upload files to be used with various Groq services such as batch processing.

def create(
    file: FileTypes,
    purpose: Literal["batch"],
    extra_headers: Headers | None = None,
    extra_query: Query | None = None,
    extra_body: Body | None = None,
    timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN
) -> FileCreateResponse:
    """
    Upload a file for use with other API endpoints.
    
    Parameters:
    - file: File to upload (binary data or file path)
    - purpose: Purpose of the file (currently only "batch" is supported)
    
    Returns:
    FileCreateResponse with file information and ID
    """

List Files

Retrieve a list of all uploaded files with their metadata.

def list(
    extra_headers: Headers | None = None,
    extra_query: Query | None = None,
    extra_body: Body | None = None,
    timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN
) -> FileListResponse:
    """
    List all uploaded files.
    
    Returns:
    FileListResponse containing list of files with metadata
    """

Retrieve File Information

Get detailed information about a specific file by its ID.

def retrieve(
    file_id: str,
    extra_headers: Headers | None = None,
    extra_query: Query | None = None,
    extra_body: Body | None = None,
    timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN
) -> FileInfoResponse:
    """
    Retrieve information about a specific file.
    
    Parameters:
    - file_id: ID of the file to retrieve information for
    
    Returns:
    FileInfoResponse with detailed file information
    """

Delete File

Remove a file from your account permanently.

def delete(
    file_id: str,
    extra_headers: Headers | None = None,
    extra_query: Query | None = None,
    extra_body: Body | None = None,
    timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN
) -> FileDeleteResponse:
    """
    Delete a file.
    
    Parameters:
    - file_id: ID of the file to delete
    
    Returns:
    FileDeleteResponse confirmation object
    """

Async File Operations

All file operations have asynchronous counterparts with identical parameters.

async def create(file: FileTypes, purpose: str, **kwargs) -> FileCreateResponse: ...
async def list(**kwargs) -> FileListResponse: ...
async def retrieve(file_id: str, **kwargs) -> FileInfoResponse: ...
async def delete(file_id: str, **kwargs) -> FileDeleteResponse: ...

Usage Examples

Upload a File

from groq import Groq

client = Groq()

# Upload a file from disk
with open("batch_requests.jsonl", "rb") as file:
    response = client.files.create(
        file=file,
        purpose="batch"
    )

print(f"File uploaded successfully!")
print(f"File ID: {response.id}")
print(f"Filename: {response.filename}")
print(f"Size: {response.bytes} bytes")
print(f"Status: {response.status}")

Upload Using file_from_path

from groq import Groq, file_from_path

client = Groq()

# Use the utility function for file handling
file_obj = file_from_path("data/training_data.jsonl")
response = client.files.create(
    file=file_obj,
    purpose="batch"
)

print(f"Uploaded file: {response.filename}")
print(f"File ID: {response.id}")

List All Files

from groq import Groq

client = Groq()

# Get all uploaded files
files = client.files.list()

print(f"Total files: {len(files.data)}")
for file in files.data:
    print(f"- {file.filename} (ID: {file.id})")
    print(f"  Size: {file.bytes} bytes")
    print(f"  Purpose: {file.purpose}")
    print(f"  Status: {file.status}")
    print(f"  Created: {file.created_at}")

Get File Information

from groq import Groq

client = Groq()

# Get information about a specific file
file_info = client.files.retrieve("file-abc123")

print(f"File ID: {file_info.id}")
print(f"Filename: {file_info.filename}")
print(f"Size: {file_info.bytes} bytes")
print(f"Purpose: {file_info.purpose}")
print(f"Status: {file_info.status}")
print(f"Created: {file_info.created_at}")

Delete a File

from groq import Groq

client = Groq()

# Delete a file
try:
    result = client.files.delete("file-abc123")
    print(f"File deleted: {result.deleted}")
    print(f"File ID: {result.id}")
except Exception as e:
    print(f"Failed to delete file: {e}")

File Management Workflow

from groq import Groq
import json

client = Groq()

# 1. Create a batch request file
batch_requests = [
    {
        "custom_id": "request-1",
        "method": "POST",
        "url": "/v1/chat/completions",
        "body": {
            "model": "llama3-8b-8192",
            "messages": [{"role": "user", "content": "What is AI?"}]
        }
    },
    {
        "custom_id": "request-2", 
        "method": "POST",
        "url": "/v1/chat/completions",
        "body": {
            "model": "llama3-8b-8192",
            "messages": [{"role": "user", "content": "Explain machine learning"}]
        }
    }
]

# Write to JSONL file
with open("batch_requests.jsonl", "w") as f:
    for request in batch_requests:
        f.write(json.dumps(request) + "\n")

# 2. Upload the file
with open("batch_requests.jsonl", "rb") as file:
    upload_response = client.files.create(
        file=file,
        purpose="batch"
    )

print(f"File uploaded: {upload_response.id}")

# 3. Use the file for batch processing (would be done with batches API)
# batch = client.batches.create(
#     input_file_id=upload_response.id,
#     endpoint="/v1/chat/completions",
#     completion_window="24h"
# )

Async Usage

import asyncio
from groq import AsyncGroq

async def main():
    client = AsyncGroq()
    
    # Async file upload
    with open("data.jsonl", "rb") as file:
        upload_response = await client.files.create(
            file=file,
            purpose="batch"
        )
    
    print(f"Uploaded file: {upload_response.id}")
    
    # Async file listing
    files = await client.files.list()
    print(f"Total files: {len(files.data)}")

asyncio.run(main())

Types

File Types

FileTypes = Union[IO[bytes], bytes, PathLike, str]

Request Types

class FileCreateParams:
    file: FileTypes
    purpose: Literal["batch"]

Response Types

class FileCreateResponse:
    id: str
    bytes: int
    created_at: int
    filename: str
    object: Literal["file"]
    purpose: Literal["batch"]
    status: Literal["uploaded", "processed", "error"]

class FileInfoResponse:
    id: str
    bytes: int
    created_at: int
    filename: str
    object: Literal["file"]
    purpose: Literal["batch"]
    status: Literal["uploaded", "processed", "error"]

class FileListResponse:
    object: Literal["list"]
    data: List[FileInfoResponse]

class FileDeleteResponse:
    id: str
    object: Literal["file"]
    deleted: bool

File Status Values

  • uploaded - File has been successfully uploaded
  • processed - File has been processed and is ready for use
  • error - File processing encountered an error

Install with Tessl CLI

npx tessl i tessl/pypi-groq

docs

audio.md

batches.md

chat-completions.md

embeddings.md

files.md

index.md

models.md

tile.json