feat(backends/python): use tempfile.gettempdir() instead of hardcoded /tmp - #9629
Merged
Merged
Conversation
… /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
approved these changes
May 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #9601
Problem
The Python backends
vllm,vllm-omni,tinygrad, andpocket-ttshardcode/tmpas 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 withLOCALAI_GENERATED_CONTENT_PATHandLOCALAI_UPLOAD_PATHalready configurable for the Go side, these Python backends still go to/tmp.Reporter on #9601:
Fix
Replace the five hardcoded
"/tmp/..."literals withos.path.join(tempfile.gettempdir(), ...).tempfile.gettempdir()honors the standardTMPDIRenv var (andTMP/TEMPon Windows), so users can simply set e.g.TMPDIR=/data/tmpand these scratch files relocate accordingly. Behavior is unchanged whenTMPDIRis unset (Linux default is still/tmp).This is a deliberately minimal change — it lets the existing
tempfilestandard-library mechanism solve the issue without inventing a new LocalAI-specific config knob. It composes cleanly with the existingLOCALAI_GENERATED_CONTENT_PATH/LOCALAI_UPLOAD_PATHoptions (which target different code paths on the Go side).Sites changed
backend/python/vllm/backend.pyf"/tmp/vl-{timestamp}.data"os.path.join(tempfile.gettempdir(), f"vl-{timestamp}.data")backend/python/tinygrad/backend.pyrequest.dst or "/tmp/tinygrad_image.png"request.dst or os.path.join(tempfile.gettempdir(), "tinygrad_image.png")backend/python/pocket-tts/backend.pyoutput_path = "/tmp/pocket-tts-output.wav"output_path = os.path.join(tempfile.gettempdir(), "pocket-tts-output.wav")backend/python/vllm-omni/backend.pyf"/tmp/vl-{timestamp}.data"os.path.join(tempfile.gettempdir(), f"vl-{timestamp}.data")backend/python/vllm-omni/backend.pyf"/tmp/audio-{timestamp}.wav"os.path.join(tempfile.gettempdir(), f"audio-{timestamp}.wav")Plus an
import tempfileadded to each file (next to the existing stdlib imports). All 4 modified files parse cleanly withpython -m py_compile.Why not switch to
NamedTemporaryFile?The existing code uses
time.time() * 1000-based filenames and explicitos.remove(p)cleanup; switching toNamedTemporaryFilewould 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_compileon all 4 modified files passesgrep -n '"/tmp\|f"/tmp' backend/python/{vllm,tinygrad,pocket-tts,vllm-omni}/backend.pyreturns no hits after the changeTMPDIR=/data/tmpset, vllm video base64 fallback writes/data/tmp/vl-<ts>.datainstead of/tmp/vl-<ts>.data. WithTMPDIRunset, behavior is identical to before on Linux (/tmp/...).🤖 Generated with Claude Code