500 INTERNAL SERVER ERRROR when processing more that one file

I’m using Gemini 2.5 Pro on Level 1 Paid tier through the API. I have a project where I’m extracting data from scientific articles and then validating the output. It looks like this:

Stage 1: Entity Extraction - input: PDF, output: entities.json
Stage 2: Observation Extraction - input: PDF + entities.json, output: observations.json
Stage 3: Validation & Correction - input: PDF + entities.json + observations.json, output: corrections.json

Now, step 1 produces the expected output, but as soon as I send the resulting entities JSON file and try to make Gemini extract observations, I get a

google.genai.errors.ServerError: 500 INTERNAL:internal error has occurred. Please retry or report in https://developers.generativeai.google/guide/troubleshooting

This is all of the code I use, I tried for it to be as simple as possible:

import logging
import os
from dotenv import load_dotenv
from google import genai
from pathlib import Path

logging.basicConfig(
    level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)


load_dotenv()
api_key = os.getenv("GEMINI_API_KEY")


def strip_response(raw_response):
    response_stripped = (
        raw_response.text.replace(f"```json\n", "").replace("```", "").strip()
    )
    return response_stripped


if not api_key:
    raise ValueError("Selected API key not found.")
client = genai.Client(api_key=api_key)
logging.info("Gemini API configured successfully.")
model = "gemini-2.5-pro"

# Extracting entities
prompt = Path(f"prompts/extract_entities.md").read_text(encoding="utf-8")
article = client.files.upload(file="data/article_files/10.1007_bf02245606.pdf")
total_tokens = client.models.count_tokens(model=model, contents=[prompt, article])
logging.info(f"Total tokens: {total_tokens}")
response = client.models.generate_content(model=model, contents=[prompt, article])
response_text = strip_response(response)
with open("data/full/json/10.1007_bf02245606_ent.json", "w", encoding="utf-8") as f:
    f.write(response_text)

# Extracting observations
prompt = Path(f"prompts/extract_observations.md").read_text(encoding="utf-8")
entities = client.files.upload(file="data/full/json/10.1007_bf02245606_ent.json")
total_tokens = client.models.count_tokens(
    model=model, contents=[prompt, article, entities]
)
logging.info(f"Total tokens: {total_tokens}")
response = client.models.generate_content(
    model=model, contents=[prompt, article, entities]
)
response_text = strip_response(response)
with open("data/full/json/10.1007_bf02245606_obs.json", "w", encoding="utf-8") as f:
    f.write(response_text)

# Validating
observations = client.files.upload(file=f"data/full/json/10.1007_bf02245606_obs.json")
total_tokens = client.models.count_tokens(
    model=model, contents=[prompt, article, entities, observations]
)
logging.info(f"Total tokens: {total_tokens}")
prompt = Path(f"prompts/validate.md").read_text(encoding="utf-8")
response = client.models.generate_content(
    model=model, contents=[prompt, article, entities, observations]
)
response_text = strip_response(response)
with open("data/full/json/10.1007_bf02245606_val.json", "w", encoding="utf-8") as f:
    f.write(response_text)

And here is the console output:

/Users/krzysztofbogusz/miniconda3/envs/ml312/bin/python /Users/krzysztofbogusz/Documents/Projects/OLTID/main.py 
2025-10-02 11:34:14,650 - INFO - Gemini API configured successfully.
2025-10-02 11:34:15,670 - INFO - HTTP Request: POST https://generativelanguage.googleapis.com/upload/v1beta/files "HTTP/1.1 200 OK"
2025-10-02 11:34:17,783 - INFO - HTTP Request: POST https://generativelanguage.googleapis.com/upload/v1beta/files?upload_id=AAwnv3Il-0Lz4gnm00YYtcYRdMPFl5MvJ95HuaSdmK3O0Jc8pbU43AuglaHgaqXIhSrch2azGE8Kol16PaZ6Frwzj-HPj0hyIhPtUaZFffnwgQ&upload_protocol=resumable "HTTP/1.1 200 OK"
2025-10-02 11:34:18,738 - INFO - HTTP Request: POST https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:countTokens "HTTP/1.1 200 OK"
2025-10-02 11:34:18,740 - INFO - Total tokens: total_tokens=8776 cached_content_token_count=None
2025-10-02 11:34:18,740 - INFO - AFC is enabled with max remote calls: 10.
2025-10-02 11:37:09,313 - INFO - HTTP Request: POST https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:generateContent "HTTP/1.1 200 OK"
2025-10-02 11:37:09,316 - INFO - AFC remote call 1 is done.
2025-10-02 11:37:09,905 - INFO - HTTP Request: POST https://generativelanguage.googleapis.com/upload/v1beta/files "HTTP/1.1 200 OK"
2025-10-02 11:37:11,079 - INFO - HTTP Request: POST https://generativelanguage.googleapis.com/upload/v1beta/files?upload_id=AAwnv3IDzVi24rDWPUPpomIPly2WP1FsOtN5Y9droLA0e9CGQCBy1RGpERISoUP8e3ayJZwcg8YX_7Kfavuo_Qz1yas87e4Ear4Bt4G3u3Upscs&upload_protocol=resumable "HTTP/1.1 200 OK"
2025-10-02 11:37:11,694 - INFO - HTTP Request: POST https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:countTokens "HTTP/1.1 200 OK"
2025-10-02 11:37:11,700 - INFO - Total tokens: total_tokens=21582 cached_content_token_count=None
2025-10-02 11:37:11,700 - INFO - AFC is enabled with max remote calls: 10.
2025-10-02 11:37:13,613 - INFO - HTTP Request: POST https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:generateContent "HTTP/1.1 500 Internal Server Error"
Traceback (most recent call last):
  File "/Users/krzysztofbogusz/Documents/Projects/OLTID/main.py", line 45, in <module>
    response = client.models.generate_content(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/krzysztofbogusz/miniconda3/envs/ml312/lib/python3.12/site-packages/google/genai/models.py", line 5202, in generate_content
    response = self._generate_content(
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/krzysztofbogusz/miniconda3/envs/ml312/lib/python3.12/site-packages/google/genai/models.py", line 4178, in _generate_content
    response_dict = self._api_client.request(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/krzysztofbogusz/miniconda3/envs/ml312/lib/python3.12/site-packages/google/genai/_api_client.py", line 755, in request
    response = self._request(http_request, stream=False)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/krzysztofbogusz/miniconda3/envs/ml312/lib/python3.12/site-packages/google/genai/_api_client.py", line 684, in _request
    errors.APIError.raise_for_response(response)
  File "/Users/krzysztofbogusz/miniconda3/envs/ml312/lib/python3.12/site-packages/google/genai/errors.py", line 103, in raise_for_response
    raise ServerError(status_code, response_json, response)
google.genai.errors.ServerError: 500 INTERNAL. {'error': {'code': 500, 'message': 'An internal error has occurred. Please retry or report in https://developers.generativeai.google/guide/troubleshooting', 'status': 'INTERNAL'}}

Process finished with exit code 1

I don’t know what I’m doing wrong. I’m not exceeding token limits, I’m following file API rules, the Python code works, the files get uploaded.

I tried using the other way of sending files, but it gives the same error:

file_paths_proper = [Path(string) for string in file_paths]
file_parts = []
for path in file_paths_proper:
    if path.suffix == ".pdf":
        specific_type = "application/pdf"
    elif path.suffix == ".json":
        specific_type = "application/json"
    else:
        specific_type = "text/plain"
    file_parts.append(
        types.Part.from_bytes(data=path.read_bytes(), mime_type=specific_type)
    )

if file_parts:
    contents = [prompt] + file_parts
else:
    contents = prompt

total_tokens = client.models.count_tokens(model=model, contents=contents)
logging.info(f"Total tokens: {total_tokens}")
response = client.models.generate_content(
    model=model,
    contents=contents,
)

When using the AI Studio, however, everything works as expected.

I would be grateful for any help.

1 Like

Hello,

Welcome to the Forum!

This looks like a temporary error and should ideally resolve after some time. Could you please retry and check if it works now?

If the issue persists, it would be helpful if you could share some additional details, such as:

  • The specific model you are struggling with (only 2.5 Pro or other models also)
  • The type of task where you are facing this issue
  • Whether it occurs consistently or only at certain times

These details will help us analyze the issue more effectively and provide you with a more accurate solution.