Add a pluggable media uploader so large video downloads don't OOM serverless runtimes #539
Spinning out a follow-up flagged in PR #409 review (https://github.com/TanStack/ai/pull/409#discussion_r2218600385 / r2218663501).
OpenAICompatibleVideoAdapter.getVideoUrl falls through to videos.downloadContent/content/getContent/download and a raw fetch for backends that don't return a CDN URL on retrieve(). In every fallback path we read the entire video into an ArrayBuffer, base64-encode it, and emit a data: URL. PR #409 added a >10 MiB warning (warnIfLargeMediaBuffer) but the underlying behaviour is still unsafe on Workers / Lambda / small isolates.
This will almost certainly blow up workers. It's pretty common for videos to be bigger than 100mb. — @tombeckenham
Downloading the video and base64'ing it will cause most backends to run out of memory. I'm wondering if we support these older methods at all. At the very least we should add a warning with logger and look at whether buffers are over e.g. 10mb. It's quite hard to figure out where memory issues come from as they don't necessarily occur at the point this blob would be created. I'm wondering if we might want to add a media uploader function to the the constructor in a future PR — @tombeckenham
Scope#
- Design a
mediaUploader(orMediaUploadStrategy) constructor option onOpenAICompatibleVideoAdapter(and likely image/audio adapters too) that takes a stream-or-blob and returns a public URL. - Default behaviour: if no uploader is configured AND the SDK doesn't expose a
retrieve()-time CDN URL, return aVideoUrlResultthat surfaces "upstream URL not available; provide amediaUploaderto download/host the bytes" rather than silently base64ing. - Promote the current
warnIfLargeMediaBufferto flow through theInternalLoggerinstead ofconsole.warnonce a logger is plumbed through these methods. - Apply the same strategy to TTS / image adapters where a base64 data URL is emitted today (see also the response-input-file base64 follow-up @tombeckenham mentioned).
Related#
- PR #409 review thread on
video.ts:148andvideo.ts:165. - The existing
responses-text.tsbase64 data-URI thread (https://github.com/TanStack/ai/pull/409#discussion_r2218493238) — Tom mentioned an in-flight PR removing that for input attachments; this issue covers the symmetrical concern on the output side.
cc @tombeckenham
github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: author
github-actions[bot](GitHub profile, opens in a new tab) assignedtombeckenham
github-actions[bot](GitHub profile, opens in a new tab) added labelshas-pr
Scope update: this issue now covers video only. #1541 closes it.
What #1541 does
- Video adapters with no public URL for the finished video (OpenRouter, Lovable, Sora jobs without
url) return the download as a stream. They do not buffer it into a base64data:URL. withGenerationPersistencestores that stream in the blob store and setsurlfromartifactUrl.- If nothing stores the stream, the run fails with an error that says what to add. This replaces the silent base64 fallback.
The design changed from the one in the description. There is no
mediaUploaderoption on the adapters. Generation persistence already fetched provider URLs and stored them, so it now handles provider bytes too.What is left out, on purpose
- TTS and audio. These adapters still return base64. Speech output is usually small, and it works today with no storage. A stream result would need a new
urlfield onTTSResult, client changes, and a decision on what happens with no persistence. - Image. Providers return images as base64 inside the JSON response. There is no stream to pass along. Persistence already stores those bytes.
- The >10 MiB warning through
InternalLogger. That warning existed only in the three video adapters. They no longer buffer, so the warning is removed and there is nothing to move to the logger.
If large speech or audio output causes a memory problem for someone, open a new issue and we will look at those activities then.
- Video adapters with no public URL for the finished video (OpenRouter, Lovable, Sora jobs without
AlemTuzlak closed this
Sign in to join this conversation.
Refreshed cross-adapter inventory (2026-07)#
Audited where output-side
arrayBuffer() → arrayBufferToBase64() → data:URL buffering still lives, so themediaUploaderdesign covers everything. Three categories:A. Already URL-passthrough — no change needed
ai-gemini/src/adapters/video.tsgetVideoUrl) returnsoperation.response…video.uridirectly. ✅videoInfo.urlwhenretrieve()exposes one. ✅item.urlwhen the API provides one (DALL·E). ✅B. Fetch/download → base64 (the OOM hazard this issue targets)
downloadContent/content/getContent/download+ rawfetch, three copies ofarrayBuffer → base64 → data:atai-openai/src/adapters/video.ts:238,:283,:308.warnIfLargeMediaBufferguards all three. This is the exact path from the PR #409 review.C. Response body is the media — no URL exists to pass through (needs the streaming/uploader variant too)
ai-openai/tts.ts:83,ai-grok/tts.ts:87,ai-groq/tts.ts:75,ai-elevenlabs/speech.ts:152ai-elevenlabs/audio.ts:206,ai-fal/speech.ts:123generateContentinlineData(ai-gemini/src/adapters/image.ts:291), OpenAI gpt-imageb64_jsonCategory B is a download we can redirect into
mediaUploader. Category C has no URL by construction — the provider streams raw bytes in the HTTP response body — so covering it means a streaming result variant (pipe theReadableStreaminto the uploader) alongside today's base64, sinceGeneratedAudio/GeneratedImageare currently strictlyurlxorb64Json.Note the overlap with #909: the
mediaUploaderprimitive here (stream bytes → storage → get a URL) is the same primitive as the native storage adapters proposed there (falFiles()wrapsfal.storage.upload, plus Gemini/OpenAI/Anthropic Files APIs), just pointed at output instead of input. Worth sharing one interface.