Upload and download files programmatically.
Automatic Upload
Local file paths and data URIs are uploaded automatically:
1result = client.tasks.run({2 "app": "image-processor",3 "input": {4 "image": "/path/to/image.png" # Uploaded automatically5 }6})Manual Upload
1from inferencesh import UploadFileOptions23# Upload from path4file_obj = client.files.upload("/path/to/image.png")5print(f"URI: {file_obj['uri']}")67# Upload from bytes8file_obj = client.files.upload(9 b"raw bytes data",10 UploadFileOptions(11 filename="data.bin",12 content_type="application/octet-stream"13 )14)1516# Upload with options17file_obj = client.files.upload(18 "/path/to/image.png",19 UploadFileOptions(20 filename="custom_name.png",21 content_type="image/png",22 public=True # Make publicly accessible23 )24)Data URI formats (JavaScript)
client.files.upload() and automatic upload in client.run() accept standard data URIs:
| Format | Example | Notes |
|---|---|---|
| Base64 | data:image/png;base64,iVBORw0KGgo= | Default for binary payloads |
| URL-encoded | data:text/plain,Hello%20World | Decoded with decodeURIComponent before upload |
| URL-safe base64 | data:text/plain;base64,SGVsbG8 | - and _ are normalized to + and / |
| Omitted media type | data:;base64,SGVsbG8= | Defaults to text/plain |
Invalid data URIs throw Invalid data URI format. If the API does not return upload_url for a created file record, or the PUT to that URL fails, upload rejects with a clear error.
From @inferencesh/sdk v0.36.2, upload sends size on POST /files from the decoded bytes (Blob, data URI or bare base64). Earlier versions could leave it out for string uploads and get 400 size is required. Calling REST yourself, set files[].size.
When processInput walks task input, it uploads data: strings anywhere in nested objects and replaces them with inf://files/... URIs. Short plain strings (for example "hello" or API keys under 64 characters) are left unchanged — only data: URIs or long base64-looking strings are treated as file content.
Upload Options
1from inferencesh import UploadFileOptions23UploadFileOptions(4 filename="custom.png", # File name5 content_type="image/png", # MIME type6 path="/custom/path", # Custom storage path7 public=True # Make publicly accessible8)Use Uploaded Files
1file = client.files.upload("/path/to/image.png")23result = client.tasks.run({4 "app": "image-processor",5 "input": {"image": file["uri"]}6})Agent file attachments
When chatting with agents, attach files on send_message / sendMessage:
1response = agent.send_message(2 "Describe this image",3 files=[open("image.png", "rb").read()],4)56# upload_file on the agent returns a FileRef (uri, filename, content_type)7ref = agent.upload_file(open("doc.pdf", "rb").read(), filename="doc.pdf")8response = agent.send_message("Summarize", files=[open("doc.pdf", "rb").read()])Files are uploaded via the Files API and referenced in the message input.attachments field (Python) or input.images / input.files (JavaScript client.agent().sendMessage()). The React AgentChatProvider posts them as attachments on POST /chats/{id}/messages (@inferencesh/sdk ≥ v0.22.1).
→ Template agents · Python SDK · JavaScript SDK
Download Results
1import requests23result = client.tasks.run(params)4output_url = result["output"]["image"]["uri"]56# Download7response = requests.get(output_url)8with open("output.png", "wb") as f:9 f.write(response.content)File Class (App Development)
When building apps in Python, use the File class:
1from inferencesh import File23# From path4file = File(path="/path/to/file.png")56# With metadata7file = File(8 path="/path/to/file.png",9 content_type="image/png",10 filename="custom.png"11)1213# Check existence14if file.exists():15 print(f"Size: {file.size} bytes")1617# From a URL: downloaded on first use and cached18file = File(uri="https://example.com/input.png")URL downloads are cached under FILE_CACHE_DIR (default ~/.cache/inferencesh/files). From inferencesh v0.37.4 each download writes its own temp file and replaces the cache entry atomically, so tasks on one worker fetching the same URL at once no longer collide.
Each cached file sits in a directory named by the first 12 hex characters of a SHA-256 of the URL's host, path and query. The host includes any user:password@, so URLs that differ only by credentials do not share an entry. From inferencesh v0.39.3 a URL that is not https also hashes its scheme, so http:// and https:// never share an entry; https keys are unchanged, so older cache entries still match. inferencesh.utils.download(url, directory) uses the same key under the directory you pass.
Task input files
On the platform, a File in task input or setup must be an http(s) URL or a data: URI. A local path, or a dict with a path key, fails validation with:
1a file in task input must be an http(s) URL or a data URI; local paths are not acceptedThis stops a caller from pointing your app at files on the worker. It does not apply to belt app test and other local runs, where you wrote the input yourself, or to File(path=...) your own code creates. client.run() uploads local paths before it sends the task, so SDK callers can still pass paths; raw POST /run bodies must use URLs.
The check is done by the engine (v1.21.62+). With inferencesh v0.39.4 or later it runs inside the SDK's untrusted_input() (exported from inferencesh.models) and refuses the path before touching it. With older SDKs the engine checks the built model instead and refuses any input file that is not remote or not in the download cache.