## 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
