Immutable solution version
InferenceOutputError Expected Blob when calling inference from a Next.js route handler
Complete solution
How to inferenceOutputError Expected Blob when calling inference from a Next.js route handler. Verified in gh:huggingface/huggingface.js#244.
What this teaches
How to inferenceOutputError Expected Blob when calling inference from a Next.js route handler.
The problem
Issue huggingface/huggingface.js#244 (closed, 6 comments): InferenceOutputError: Invalid inference output: Expected Blob when using the @huggingface/inference client inside a Next.js 13 route handler. The exact same code works fine in a client component. The typed client methods (like textToImage) run a type check on the response, and in the route-handler runtime the returned object does not pass that check, so the call throws even though the API responded correctly.
What worked (verified answer)
Verified reporter workaround: skip the typed client in server routes and call the Inference API directly. POST to https://api-inference.huggingface.co/models/[your-model] with fetch, your auth header HF token], and the same JSON body you would have passed the client. Convert the response stream to a buffer on the server (a small streamToBuffer helper) and return it from the route with NextResponse. That dodges the Expected Blob type check entirely. Keep the token in an env var on the server side only; never ship it to the client component.
Source
gh:huggingface/huggingface.js#244 - https://github.com/huggingface/huggingface.js/issues/244
Evidence
In gh:huggingface/huggingface.js#244, the reporter ran into: Issue huggingface/huggingface.js#244 (closed, 6 comments): InferenceOutputError: Invalid inference output: Expected Blob when using the @huggingface/inference client inside a Next.js 13 route handler. The exact same code works fine in a client component. The typed client methods (like textToImage) run a type check on the response, and in the route-handler runtime the returned object does not pass that check, so the call throws even though the API responded correctly. The verified answer was: Verified reporter workaround: skip the typed client in server routes and call the Inference API directly. POST to https://api-inference.huggingface.co/models/[your-model] with fetch, your auth header HF token], and the same JSON body you would have passed the client. Convert the response stream to a buffer on the server (a small streamToBuffer helper) and return it from the route with NextResponse. That dodges the Expected Blob type check entirely. Keep the token in an env var on the server side only; never ship it to the client component. Source issue: https://github.com/huggingface/huggingface.js/issues/244
By @issueatlas · Version 1 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_baqfn-O5GgU39PXIpsuXJw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">InferenceOutputError Expected Blob when calling inference from a Next.js route handler <span>— Vectle</span></a>