Overview
Defines flexible file type definitions and utilities for multipart file upload handling compatible with httpx.
Description
The file module provides type aliases and helper functions for working with file uploads in the Cohere SDK. It defines FileContent (accepting IO[bytes], bytes, or str) and File (a union of FileContent with optional tuple forms carrying filename, content type, and headers). The module also provides convert_file_dict_to_httpx_tuples to flatten a dictionary of files into the list-of-tuples format that httpx requires, and with_content_type to ensure every file has an explicit content type set.
Usage
Use the File type when annotating parameters that accept file uploads. Use convert_file_dict_to_httpx_tuples internally when preparing multipart payloads for httpx requests. Use with_content_type when you need to ensure a default MIME type is applied to a file that may not have one specified.
Code Reference
Source Location
Signature
FileContent = Union[IO[bytes], bytes, str]
File = Union[
FileContent,
Tuple[Optional[str], FileContent],
Tuple[Optional[str], FileContent, Optional[str]],
Tuple[Optional[str], FileContent, Optional[str], Mapping[str, str]],
]
def convert_file_dict_to_httpx_tuples(
d: Dict[str, Union[File, List[File]]],
) -> List[Tuple[str, File]]: ...
def with_content_type(*, file: File, default_content_type: str) -> File: ...
Import
from cohere.core.file import File, FileContent, convert_file_dict_to_httpx_tuples, with_content_type
I/O Contract
Inputs
convert_file_dict_to_httpx_tuples
| Name |
Type |
Required |
Description
|
| d |
Dict[str, Union[File, List[File]]] |
Yes |
A dictionary mapping field names to single files or lists of files to be uploaded.
|
with_content_type
| Name |
Type |
Required |
Description
|
| file |
File |
Yes |
The file value (raw content or tuple) to which the content type should be applied.
|
| default_content_type |
str |
Yes |
The MIME type to use if the file does not already have a content type specified.
|
Outputs
convert_file_dict_to_httpx_tuples
| Name |
Type |
Description
|
| return |
List[Tuple[str, File]] |
A flat list of (field_name, file) tuples suitable for passing to httpx as multipart data.
|
with_content_type
| Name |
Type |
Description
|
| return |
File |
A tuple of (filename, content, content_type[, headers]) with the content type guaranteed to be set.
|
Usage Examples
from cohere.core.file import File, convert_file_dict_to_httpx_tuples, with_content_type
# Prepare a simple file upload dict
files: dict[str, File] = {
"document": ("report.pdf", open("report.pdf", "rb"), "application/pdf"),
"image": ("photo.png", open("photo.png", "rb"), "image/png"),
}
# Convert to httpx-compatible tuples
httpx_tuples = convert_file_dict_to_httpx_tuples(files)
# Result: [("document", ("report.pdf", <file>, "application/pdf")), ("image", ("photo.png", <file>, "image/png"))]
# Ensure a default content type on a bare file
file_with_type = with_content_type(
file=open("data.csv", "rb"),
default_content_type="text/csv",
)
# Result: (None, <file>, "text/csv")
Related Pages