Gemini api requests not working
thomasvmc
FREEOP

6 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

6 months ago

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


6 months ago

If the repo is public I can take a look


thomasvmc
FREEOP

6 months ago


6 months ago

Does it throw any type of error?


thomasvmc
FREEOP

6 months ago

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


thomasvmc
FREEOP

6 months ago

the first error was getting refresh token


thomasvmc
FREEOP

6 months ago

oh wait its still on


thomasvmc
FREEOP

6 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%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%2Fauth%2Fcloud-platform failed, reason: connect ETIMEDOUT 169.254.169.254:80

at Gaxios._request (/app/node_modules/gaxios/build/cjs/src/gaxios.js:165:23)

at process.processTicksAndRejections (node:internal/process/task_queues:105:5)

at async metadataAccessor (/app/node_modules/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

6 months ago

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


thomasvmc
FREEOP

6 months ago

it doesnt crash just sends the error


6 months ago

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


6 months ago

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


6 months ago

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


thomasvmc
FREEOP

6 months ago

Ive already submitted this can't change it


6 months ago

What do you mean you can't change it?


thomasvmc
FREEOP

6 months ago

unless i can make changes on railway outside of github


thomasvmc
FREEOP

6 months ago

its an assignment


thomasvmc
FREEOP

6 months ago

for school


6 months ago

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



thomasvmc
FREEOP

6 months ago

alr


6 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

6 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

6 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/node_modules/google-auth-library/build/src/auth/googleauth.js:284:15)

at async #determineClient (/app/node_modules/google-auth-library/build/src/auth/googleauth.js:764:36)

at async GoogleAuth.getClient (/app/node_modules/google-auth-library/build/src/auth/googleauth.js:741:20)

at async GoogleAuth.getRequestHeaders (/app/node_modules/google-auth-library/build/src/auth/googleauth.js:793:24)

at async NodeAuth.addGoogleAuthHeaders (file:///app/node_modules/@google/genai/dist/node/index.mjs:15480:29)

at async ApiClient.getHeadersInternal (file:///app/node_modules/@google/genai/dist/node/index.mjs:11239:9)

at async ApiClient.includeExtraHttpOptionsToRequestInit (file:///app/node_modules/@google/genai/dist/node/index.mjs:11114:31)

at async ApiClient.requestStream (file:///app/node_modules/@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

6 months ago

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


thomasvmc
FREEOP

6 months ago

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


6 months ago

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


6 months ago

are you hardcoding it or using env vars?


6 months ago

are they properly set on your service variables tab?


thomasvmc
FREEOP

6 months ago

wait


thomasvmc
FREEOP

6 months ago

i think i see it lol


thomasvmc
FREEOP

6 months ago

1438664778817077431


thomasvmc
FREEOP

6 months ago

it should be key


6 months ago

yep


thomasvmc
FREEOP

6 months ago

i cant believe i didnt see that


thomasvmc
FREEOP

6 months ago

and it works now thats so stupid


6 months ago

that's more common than you think lol


6 months ago

is everything working fine now?


thomasvmc
FREEOP

6 months ago

yep


6 months ago

nice, can I mark this thread as solved?


thomasvmc
FREEOP

6 months ago

yep


Status changed to Solved medim 6 months ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...