INVALID_ARGUMENT when using Node.js Fetch

This works in Chrome (open inspector and paste):

async function main() {
  const response = await fetch(
    'https://generativelanguage.googleapis.com/v1beta/openai/chat/completions',
    {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Authorization: `Bearer ABC123`,
      },
      body: JSON.stringify({
        model: 'gemini-1.5-flash',
        messages: [{ role: 'user', content: 'Explain to me how AI works' }],
        stream: true,
      }),
    }
  )

  console.log('>>>', response.statusText, await response.text())
}

main()

The exact same code does not work in Node. The exact error is:

Bad Request [{
  "error": {
    "code": 400,
    "message": "Request contains an invalid argument.",
    "status": "INVALID_ARGUMENT"
  }
}

Does anyone know what is going on?

For some reason it always fails for Node version of fetch up-to the latest (23).

Same problem from rust. I’m also using the openai compatibility endpoint. Replicating the request closely with curl works.

Did you find a solution?

Nope. I just couldn’t be bothered. I am sure it is a problem with Google’s service and the likelyhood of them fixing it is zero. I was hoping to bring Gemini to our customers this week though.

1 Like

I guess this is not a priority for Google.