Skip to content
Back

Functions returning binary data failing due to JSON encoding

  • 0
  • Self Hosted
cardre
6 Aug, 2025, 11:14

Has anyone successfully deployed a function that returns binary data, such as an image/png or audio/wav? I have with other functions had to base64 encode / decode etc, but want to avoid this due to the size and compute required for my application function. I just want the functions to return the binary data as provided to appwrite by my function. Currently running self hosted, latest 1.7.4 version. Its a simple python 3.12 deployed appwrite function that just returns the raw binary data and content-type of audio/wav, very much like the docs for functions that returns an image/png if you request a type of binary. The documentation shows doing almost exactly the same thing I'm doing where I'm opening the binary file and returning the bytes.

python code, tried each of the following:

return context.res.binary(wav_file_bytes}) return context.res.binary(wav_file_bytes, 200, {"content-type": "audio/wav"}) return context.res.binary(wav_file_bytes, 200, {"content-type": "audio/wav; charset=ISO-8859-1"})

The last one because the python 3.x open-runtimes insist on adding '; charset=utf-8' to any content-type that does not begin with 'multipart/' and I thought this was triggering the error.

The actual executions page successfully shows a 200 status code for the function's returned result, but once Appwrite internally processes the result, it has an error due to trying to then json decode that data.

The following error in 'appwrite' logs:

[Error] Method: POST [Error] URL: /v1/functions/:functionId/executions [Error] Type: JsonException [Error] Message: Malformed UTF-8 characters, possibly incorrectly encoded [Error] File: /usr/src/code/src/Appwrite/Utopia/Response.php [Error] Line: 825

I can see why its complaining because an audio/wav (or png or many other binary file types!) will contain data that is not valid utf-8.

How is an Appwrite (python) function supposed to return binary data to stop Appwrite further trying to process it and encode it wit utf-8?

TL;DR
Developers encountering issues with deploying functions that return binary data, like image/png or audio/wav, in Appwrite. They have tried using various approaches in Python, but the data is being JSON encoded internally by Appwrite, causing errors due to invalid utf-8 characters. To resolve this, developers need to ensure that their function returns the binary data to stop further processing and encoding.
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more