Gemini api requests not working
thomasvmc
FREEOP

4 months ago

I made a discord bot for a school project that uses the gemini-2.5 flash model. It works locally but on railway the requests sent to gemini dont seem to work.
project id: 064561f6-ee63-4793-9aff-36ef213e04f5

Solved

41 Replies

4 months ago

Does it throw an error? I doubt this is related to Railway itself but I can help you investigate this.


4 months ago

If the repo is public I can take a look


thomasvmc
FREEOP

4 months ago


4 months ago

Does it throw any type of error?


thomasvmc
FREEOP

4 months ago

i would have to start it again but i think it was every request to gemini not working


thomasvmc
FREEOP

4 months ago

the first error was getting refresh token


thomasvmc
FREEOP

4 months ago

oh wait its still on


thomasvmc
FREEOP

4 months ago

hostname: '169.254.169.254',
config: {
port: '',
url: URL {
pathname: '/computeMetadata/v1/instance/service-accounts/default/token',
href: 'http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token?scopes=https%3A%2F%[2Fwww.googleapis.com](2Fwww.googleapis.com)%2Fauth%2Fcloud-platform',
search: '?scopes=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform',
origin: 'http://169.254.169.254',
searchParams: URLSearchParams { 'scopes' => 'https://www.googleapis.com/auth/cloud-platform' },
protocol: 'http:',
username: '',
password: '',
host: '169.254.169.254',
Unexpected error creating chat: GaxiosError: Could not refresh access token: request to http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token?scopes=https%3A%2F%[2Fwww.googleapis.com](2Fwww.googleapis.com)%2Fauth%2Fcloud-platform failed, reason: connect ETIMEDOUT 169.254.169.254:80
at Gaxios.request (/app/nodemodules/gaxios/build/cjs/src/gaxios.js:165:23)
at process.processTicksAndRejections (node:internal/process/taskqueues:105:5) at async metadataAccessor (/app/nodemodules/gcp-metadata/build/src/index.js:160:17)
at async Compute.refreshTokenNoCache (/app/node_modules/google-auth-library/build/src/auth/computeclient.js:57:20) {
response: undefined,
params: { scopes: 'https://www.googleapis.com/auth/cloud-platform' },
retry: 3,
code: 'ETIMEDOUT',
responseType: 'text',
httpMethodsToRetry: [Array],
status: undefined,
timeout: 0,


thomasvmc
FREEOP

4 months ago

I just don't get that its identical to my local setup and it doesnt work on railway


thomasvmc
FREEOP

4 months ago

it doesnt crash just sends the error


4 months ago

Hmmmm code seems fine, try replacing the chat.sendMessageStream to just chat.sendMessage


4 months ago

And make sure you've properly set the environment vars on Railway


4 months ago

Add some type of logging on startup to check if the env vars are really there


thomasvmc
FREEOP

4 months ago

Ive already submitted this can't change it


4 months ago

What do you mean you can't change it?


thomasvmc
FREEOP

4 months ago

unless i can make changes on railway outside of github


thomasvmc
FREEOP

4 months ago

its an assignment


thomasvmc
FREEOP

4 months ago

for school


4 months ago

Through the CLI, you can clone the repo and do a railway up



thomasvmc
FREEOP

4 months ago

alr


4 months ago

Another weird thing I spotted is that you send an empty object to GoogleGenAi
e.g: const ai = new GoogleGenAI({});
Maybe try explicity sending the API key there

const ai = new GoogleGenAI({
  apiKey: process.env.GEMINI_API_KEY,
});

thomasvmc
FREEOP

4 months ago

its like that in the documentation they say it gets the token from the environment but ill try adding it to see if it helps


thomasvmc
FREEOP

4 months ago

Unexpected error creating chat: Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
at GoogleAuth.getApplicationDefaultAsync (/app/nodemodules/google-auth-library/build/src/auth/googleauth.js:284:15) at async #determineClient (/app/nodemodules/google-auth-library/build/src/auth/googleauth.js:764:36)
at async GoogleAuth.getClient (/app/nodemodules/google-auth-library/build/src/auth/googleauth.js:741:20) at async GoogleAuth.getRequestHeaders (/app/nodemodules/google-auth-library/build/src/auth/googleauth.js:793:24)
at async NodeAuth.addGoogleAuthHeaders (file:///app/nodemodules/@google/genai/dist/node/index.mjs:15480:29) at async ApiClient.getHeadersInternal (file:///app/nodemodules/@google/genai/dist/node/index.mjs:11239:9)
at async ApiClient.includeExtraHttpOptionsToRequestInit (file:///app/nodemodules/@google/genai/dist/node/index.mjs:11114:31) at async ApiClient.requestStream (file:///app/nodemodules/@google/genai/dist/node/index.mjs:11087:23)
at async Models.generateContentStream (file:///app/node_modules/@google/genai/dist/node/index.mjs:12524:24)
(node:20) MetadataLookupWarning: received unexpected error = All promises were rejected code = UNKNOWN


thomasvmc
FREEOP

4 months ago

does it have something to do with that its on railroad because this literally works locally


thomasvmc
FREEOP

4 months ago

i already copied and pasted the gemini token a few times so i dont think thats it


4 months ago

Now I'm sure it's related to the env vars


4 months ago

are you hardcoding it or using env vars?


4 months ago

are they properly set on your service variables tab?


thomasvmc
FREEOP

4 months ago

wait


thomasvmc
FREEOP

4 months ago

i think i see it lol


thomasvmc
FREEOP

4 months ago

1438664778817077500


thomasvmc
FREEOP

4 months ago

it should be key


4 months ago

yep


thomasvmc
FREEOP

4 months ago

i cant believe i didnt see that


thomasvmc
FREEOP

4 months ago

and it works now thats so stupid


4 months ago

that's more common than you think lol


4 months ago

is everything working fine now?


thomasvmc
FREEOP

4 months ago

yep


4 months ago

nice, can I mark this thread as solved?


thomasvmc
FREEOP

4 months ago

yep


Status changed to Solved medim 4 months ago


Loading...