I'm using requests in Python and I get a 500 error

If you’re using the requests library in Python to make your API call and you’re getting a 500 error, please make sure that you are not manually setting the HTTP header Content-Type: multipart/form-data.

This header is already being automatically added by the requests library, so adding it a second time in your code will lead to a malformed request.

Copy

import os
import requests

def process_image(input_image_path, output_image_path):
    try:
        url = "https://sdk.photoroom.com/v1/segment"

        with open(input_image_path, 'rb') as image_file:
            files = { "image_file": image_file }

            headers = {
                "Accept": "image/png, application/json",
                "x-api-key": API_KEY,
                'Content-Type': 'multipart/form-data', # 👈 this line need to be removed
            }

            response = requests.post(url, files=files, headers=headers)
            response.raise_for_status()

            with open(output_image_path, 'wb') as f:
                f.write(response.content)
                print(f"Image downloaded and saved to {output_image_path}")