Skip to content

feat(backends/python): use tempfile.gettempdir() instead of hardcoded /tmp - #9629

Merged
mudler merged 1 commit into
mudler:masterfrom
Anai-Guo:feat/configurable-temp-paths
May 1, 2026
Merged

feat(backends/python): use tempfile.gettempdir() instead of hardcoded /tmp#9629
mudler merged 1 commit into
mudler:masterfrom
Anai-Guo:feat/configurable-temp-paths

Conversation

@Anai-Guo

@Anai-Guo Anai-Guo commented May 1, 2026

Copy link
Copy Markdown
Contributor

Closes #9601

Problem

The Python backends vllm, vllm-omni, tinygrad, and pocket-tts hardcode /tmp as the directory for scratch files (base64-decoded video/audio buffers, generated image/TTS outputs when no destination is provided). On systems where / is a small or read-only partition (containers with size-limited overlays, locked-down hosts, automation environments where the user can manage data volume but not root), there is no escape hatch: even with LOCALAI_GENERATED_CONTENT_PATH and LOCALAI_UPLOAD_PATH already configurable for the Go side, these Python backends still go to /tmp.

Reporter on #9601:

default /tmp is on the root partition which has limit size; the size limit is because of automation and manageability ... tried out a few parameters to no avail.

Fix

Replace the five hardcoded "/tmp/..." literals with os.path.join(tempfile.gettempdir(), ...). tempfile.gettempdir() honors the standard TMPDIR env var (and TMP/TEMP on Windows), so users can simply set e.g. TMPDIR=/data/tmp and these scratch files relocate accordingly. Behavior is unchanged when TMPDIR is unset (Linux default is still /tmp).

This is a deliberately minimal change — it lets the existing tempfile standard-library mechanism solve the issue without inventing a new LocalAI-specific config knob. It composes cleanly with the existing LOCALAI_GENERATED_CONTENT_PATH / LOCALAI_UPLOAD_PATH options (which target different code paths on the Go side).

Sites changed

File Line (orig) Before After
backend/python/vllm/backend.py 605 f"/tmp/vl-{timestamp}.data" os.path.join(tempfile.gettempdir(), f"vl-{timestamp}.data")
backend/python/tinygrad/backend.py 671 request.dst or "/tmp/tinygrad_image.png" request.dst or os.path.join(tempfile.gettempdir(), "tinygrad_image.png")
backend/python/pocket-tts/backend.py 207 output_path = "/tmp/pocket-tts-output.wav" output_path = os.path.join(tempfile.gettempdir(), "pocket-tts-output.wav")
backend/python/vllm-omni/backend.py 121 f"/tmp/vl-{timestamp}.data" os.path.join(tempfile.gettempdir(), f"vl-{timestamp}.data")
backend/python/vllm-omni/backend.py 141 f"/tmp/audio-{timestamp}.wav" os.path.join(tempfile.gettempdir(), f"audio-{timestamp}.wav")

Plus an import tempfile added to each file (next to the existing stdlib imports). All 4 modified files parse cleanly with python -m py_compile.

Why not switch to NamedTemporaryFile?

The existing code uses time.time() * 1000-based filenames and explicit os.remove(p) cleanup; switching to NamedTemporaryFile would be a larger behavior change (collision-safe naming, RAII cleanup) that's worth doing on its own merit but is out of scope for this /tmp-relocation fix.

Test plan

  • python -m py_compile on all 4 modified files passes
  • grep -n '"/tmp\|f"/tmp' backend/python/{vllm,tinygrad,pocket-tts,vllm-omni}/backend.py returns no hits after the change
  • Behavior (manual): with TMPDIR=/data/tmp set, vllm video base64 fallback writes /data/tmp/vl-<ts>.data instead of /tmp/vl-<ts>.data. With TMPDIR unset, behavior is identical to before on Linux (/tmp/...).

🤖 Generated with Claude Code

… /tmp

Closes mudler#9601

Makes the temporary scratch paths in vllm, vllm-omni, tinygrad, and pocket-tts
backends configurable via the standard TMPDIR env var, instead of always writing
to /tmp. This is a one-line change per call site that calls tempfile.gettempdir()
for the directory and keeps the same filename suffix.

Users who run on systems with a small root partition (or want to relocate scratch
files to a larger volume) can now redirect these by setting TMPDIR
(e.g. TMPDIR=/data/tmp), without affecting the existing LOCALAI_GENERATED_CONTENT_PATH
or LOCALAI_UPLOAD_PATH options that already cover other temp paths.

Files touched:
- backend/python/vllm/backend.py        (1 site: video base64 scratch)
- backend/python/tinygrad/backend.py    (1 site: image fallback dst)
- backend/python/pocket-tts/backend.py  (1 site: tts wav fallback dst)
- backend/python/vllm-omni/backend.py   (2 sites: video + audio scratch)
@mudler
mudler merged commit 80961d2 into mudler:master May 1, 2026
37 of 62 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

parameter to change /tmp default download temp to /..../tmp

2 participants