Specific images causing 400 error for the GPT Vision API endpoint

I use the GPT Vision endpoint (using “model”: “gpt-4-turbo”) to feed it an image and then get an analysis of the image.

It is super weird: consistently for the same images, a 400 error will be returned: requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.openai.com/v1/chat/completion

For other images, they will work every time. I’m using my phone’s camera everytime. So if I’m taking a photo of a document, even if the image is slightly different (photo of the document taken at different times), the error will always show for the given document.

Any ideas on why this is happening? I’ve tried changing "detail" to low, and changing max_tokens to 2200, 1250, still having the same issue.

My simple function is below:

def gpt4_analysis_of_image(base64_image):
    payload = {
    "model": "gpt-4-turbo",
    "messages": [
        {
        "role": "user",
        "content": [
            {
            "type": "text",
            "text": "Attached is an image of what SHOULD be a medical document. Analyse the document." 
            },
            {
            "type": "image_url",
            "image_url": {
                "url": f"{base64_image}",
                "detail": "high"
            }
            }
        ]
        }
    ],
    "max_tokens": 4000
    }

    response = requests.post("https://api.openai.com/v1/chat/completions", 
            headers=request_headers, json=payload)
    response.raise_for_status() # Triggers on 400 error.

so how the vision thing works for text, is outputted to the LLM for context when regarding the image. so it must have read the context from your document, and then rejected the contents of the image. or it could simply be too long a document, and the text in the image was too long for the LLM’s context memory, so when your request is too long, or the context, it will send an error back to you.