Troubleshooting guide
Use this guide to help you diagnose and resolve common issues that arise when you call the Gemini API. You may encounter issues from either the Gemini API backend service or the client SDKs. Our client SDKs are open sourced in the following repositories:
If you encounter API key issues, verify that you have set up your API key correctly per the API key setup guide.
Gemini API backend service error codes
The following table lists common backend error codes you may encounter, along with explanations for their causes and troubleshooting steps:
Check your API calls for model parameter errors
Verify that your model parameters are within the following values:
get_model (Python)
to determine the maximum number of tokens for the model you are using.
In addition to checking parameter values, make sure you're using the correct
API version (e.g., /v1 or /v1beta) and
model that supports the features you need. For example, if a feature is in Beta
release, it will only be available in the /v1beta API version.
Check if you have the right model
Verify that you are using a supported model listed on our models page.
Higher latency or token usage with 2.5 models
If you're observing higher latency or token usage with the 2.5 Flash and Pro models, this can be because they come with thinking is enabled by default in order to enhance quality. If you are prioritizing speed or need to minimize costs, you can adjust or disable thinking.
Refer to thinking page for guidance and sample code.
Safety issues
If you see a prompt was blocked because of a safety setting in your API call, review the prompt with respect to the filters you set in the API call.
If you see BlockedReason.OTHER, the query or response may violate the terms
of service or be otherwise unsupported.
Recitation issue
If you see the model stops generating output due to the RECITATION reason, this means the model output may resemble certain data. To fix this, try to make prompt / context as unique as possible and use a higher temperature.
Repetitive tokens issue
If you see repeated output tokens, try the following suggestions to help reduce or eliminate them.
| Description | Cause | Suggested workaround |
|---|---|---|
| Repeated hyphens in Markdown tables | This can occur when the contents of the table are long as the model tries to create a visually aligned Markdown table. However, the alignment in Markdown is not necessary for correct rendering. |
Add instructions in your prompt to give the model specific guidelines for generating Markdown tables. Provide examples that follow those guidelines. You can also try adjusting the temperature. For generating code or very structured output like Markdown tables, high temperature have shown to work better (>= 0.8). The following is an example set of guidelines you can add to your prompt to prevent this issue: # Markdown Table Format * Separator line: Markdown tables must include a separator line below the header row. The separator line must use only 3 hyphens per column, for example: |---|---|---|. Using more hypens like ----, -----, ------ can result in errors. Always use |:---|, |---:|, or |---| in these separator strings. For example: | Date | Description | Attendees | |---|---|---| | 2024年10月26日 | Annual Conference | 500 | | 2025年01月15日 | Q1 Planning Session | 25 | * Alignment: Do not align columns. Always use |---|. For three columns, use |---|---|---| as the separator line. For four columns use |---|---|---|---| and so on. * Conciseness: Keep cell content brief and to the point. * Never pad column headers or other cells with lots of spaces to match with width of other content. Only a single space on each side is needed. For example, always do "| column name |" instead of "| column name |". Extra spaces are wasteful. A markdown renderer will automatically take care displaying the content in a visually appealing form. |
| Repeated tokens in Markdown tables | Similar to the repeated hyphens, this occurs when the model tries to visually align the contents of the table. The alignment in Markdown is not required for correct rendering. |
|
Repeated newlines (\n) in structured output
|
When the model input contains unicode or escape sequences like
\u or \t, it can lead to repeated newlines.
|
|
| Repeated text in using structured output | When the model output has a different order for the fields than the defined structured schema, this can lead to repeating text. |
|
| Repetitive tool calling | This can occur if the model loses the context of previous thoughts and/or call an unavailable endpoint that it's forced to. |
Instruct the model to maintain state within its thought process.
Add this to the end of your system instructions:
When thinking silently: ALWAYS start the thought with a brief (one sentence) recap of the current progress on the task. In particular, consider whether the task is already done. |
| Repetitive text that's not part of structured output | This can occur if the model gets stuck on a request that it can't resolve. |
|
Blocked or non-working API keys
This section describes how to check whether your Gemini API key is blocked and what to do about it.
Understand why keys are blocked
We have identified a vulnerability where some API keys may have been publicly exposed. To protect your data and prevent unauthorized access, we have proactively blocked these known leaked keys from accessing the Gemini API.
Confirm if your keys are affected
If your key is known to be leaked, you can no longer use that key with the Gemini API. You can use Google AI Studio to see if any of your API keys are blocked from calling the Gemini API and generate new keys. You may also see the following error returned when attempting to use these keys:
Your API key was reported as leaked. Please use another API key.
Action for blocked API keys
You should generate new API keys for your Gemini API integrations using Google AI Studio. We strongly recommend reviewing your API key management practices to ensure that your new keys are kept secure and are not publicly exposed.
Unexpected charges due to vulnerability
Submit a billing support case. Our billing team is working on this, and we will communicate updates as soon as possible.
Google's security measures for leaked keys
How is Google going to help secure my account from cost overrun and abuse if my API keys are leaked?
- We are moving towards issuing API keys when you request a new key using Google AI Studio that will by default be limited to only Google AI Studio and not accept keys from other services. This will help prevent any unintended cross-key usage.
- We are defaulting to blocking API keys that are leaked and used with the Gemini API, helping prevent abuse of cost and your application data.
- You will be able to find the status of your API keys within Google AI Studio and we will work on communicating proactively when we identify your API keys are leaked for immediate action.
Improve model output
For higher quality model outputs, explore writing more structured prompts. The prompt engineering guide page introduces some basic concepts, strategies, and best practices to get you started.
Understand token limits
Read through our Token guide to better understand how to count tokens and their limits.
Known issues
- The API supports only a number of select languages. Submitting prompts in unsupported languages can produce unexpected or even blocked responses. See available languages for updates.
File a bug
Join the discussion on the Google AI developer forum if you have questions.