400 Bad request error - infuriating!

I’m addng the contents of a financial statement to gemini-2.0-flash in a prompt and I keep getting the 400 - Bad request error repeatedly - this is utterly frustrating !

I’m using the vertex API, gemini-2.0-flash model
When I try the same promot inside the AI studio it works like a charm !
Why this error from the vertex api then? So much time wasted debugging and replacing calls with other models.

K

Hey @Kumar_Ramanathan , HTTP code 400 error means there is a typo or a missing required field in your request.

Could you share the sample code?

generation_config=types.GenerateContentConfig(temperature=1.0,
safety_settings=[
types.SafetySetting(category=‘HARM_CATEGORY_HATE_SPEECH’, threshold=‘BLOCK_NONE’),
types.SafetySetting(category=‘HARM_CATEGORY_HARASSMENT’, threshold=‘BLOCK_NONE’),
types.SafetySetting(category=‘HARM_CATEGORY_SEXUALLY_EXPLICIT’, threshold=‘BLOCK_NONE’),
types.SafetySetting(category=‘HARM_CATEGORY_DANGEROUS_CONTENT’, threshold=‘BLOCK_NONE’)
],
response_mime_type = ‘application/json’)
response = gemini_client.models.generate_content(
model=model,
contents=prompt,
config=generation_config)

the prompt is a simple prompt asking Gemini to convert data from a financial statement to JSON.

Thank you,
Kumar

You can try below code:

from google.genai import types

response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents=prompt,
    config=types.GenerateContentConfig(
        response_mime_type = "application/json",
      safety_settings=[
        types.SafetySetting(
            category=types.HarmCategory.HARM_CATEGORY_HATE_SPEECH,
            threshold=types.HarmBlockThreshold.BLOCK_NONE
        ),
        types.SafetySetting(
            category=types.HarmCategory.HARM_CATEGORY_HARASSMENT,
            threshold=types.HarmBlockThreshold.BLOCK_NONE
        ),
        types.SafetySetting(
            category=types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
            threshold=types.HarmBlockThreshold.BLOCK_NONE
        ),
        types.SafetySetting(
            category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
            threshold=types.HarmBlockThreshold.BLOCK_NONE
        ),
      ]
    )
)

print(response.text)

Thank you, @GUNAND_MAYANGLAMBAM - but the error still persists - this is making the whole production code break !
K

Any solution, @GUNAND_MAYANGLAMBAM ? This is really weighing down upon us, please advise !
K

Hi @Kumar_Ramanathan

I took the code from Gunand and ran it locally against the Google AI Gemini API first and then against Vertex AI.

Both times it worked.

import os
from google import genai
from google.genai import types

client = genai.Client(
    vertexai=True,
    project='your-projectid-from-gcp-123456',
    location='us-central1'
)

prompt = "Explain me how your safety settings work."
response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents=prompt,
    config=types.GenerateContentConfig(
        temperature=1.0,
        response_mime_type="application/json",
        safety_settings=[
            types.SafetySetting(
                category=types.HarmCategory.HARM_CATEGORY_HATE_SPEECH,
                threshold=types.HarmBlockThreshold.BLOCK_NONE
            ),
            types.SafetySetting(
                category=types.HarmCategory.HARM_CATEGORY_HARASSMENT,
                threshold=types.HarmBlockThreshold.BLOCK_NONE
            ),
            types.SafetySetting(
                category=types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
                threshold=types.HarmBlockThreshold.BLOCK_NONE
            ),
            types.SafetySetting(
                category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
                threshold=types.HarmBlockThreshold.BLOCK_NONE
            ),
        ]
    )
)

print(response.text)

Right now, I’d assume that the problem is not related to the genai package or the code. Given the HTTP 400 Bad Request response you are experiencing I assume that the request gets injected/modified. Is there a proxy in your network?

Could it be that your credentials to access Google Cloud - meaning the Application Default Credentials (ADC) are wrongly configured? Check whether your ProjectId and the so-called “Quota ProjectId” are valid.
That information is added to the HTTP header (‘x-goog-user-project’) of a request created in the genai package. See _api_client.py in the source code.

You’ll find them in your credentials file for ADC. Look for a file “application_default_credentials.json” in either %APPLICATIONDATA%\gcloud or $HOME/.config/gcloud/ depending on your OS.

Can you check your access token in a terminal or command prompt?
$ gcloud auth application-default print-access-token

Can you authenticate and refresh your ADC?
$ gcloud auth application-default login

See documentation for more details on ADC: Authentication methods at Google  |  Google Cloud

Trying to help…

Cheers

Additional note.

Using model gemini-2.0-flash-lite-001 and appropriate API key it also works positively in Vertex AI in express mode.

Cheers.

I’d almost given up hope, @jkirstaetter , and shifted a lot of my code to hit other models.
But I’m going to try these and report in a bit.
Thank you for the inputs, sincerely appreciated.

1 Like

gemini-2.0-flash-lite-001 - this is the only model that works - I still can’t understand why !
Thank you , @jkirstaetter.

K

1 Like

Hi @Kumar_Ramanathan

Feel free to mark an answer as solution. :smile_cat:
Thanks

Cheers