400 Invalid Argument - Content.parts must not be empty


ClientError Traceback (most recent call last)
Cell In[37], line 5
1 client = genai.Client()
3 text_input = ‘A cute panda, Poko, carefully stacking three smooth, green river stones. Focus on the serenity of the scene, soft lighting’
----> 5 response = client.models.generate_content(
6 model=“gemini-2.0-flash-preview-image-generation”,
7 contents=[text_input, image],
8 config=types.GenerateContentConfig(
9 response_modalities=[‘TEXT’, ‘IMAGE’]
10 )
11 )
13 for part in response.candidates[0].content.parts:
14 if part.text is not None:

File ~/adk-made-simple/.venv/lib/python3.10/site-packages/google/genai/models.py:4799, in Models.generate_content(self, model, contents, config)
4797 while remaining_remote_calls_afc > 0:
4798 i += 1
→ 4799 response = self._generate_content(
4800 model=model, contents=contents, config=config
4801 )
4802 logger.info(f’AFC remote call {i} is done.')
4803 remaining_remote_calls_afc -= 1

File ~/adk-made-simple/.venv/lib/python3.10/site-packages/google/genai/models.py:3775, in Models._generate_content(self, model, contents, config)
3772 request_dict = _common.convert_to_dict(request_dict)
3773 request_dict = _common.encode_unserializable_types(request_dict)
→ 3775 response_dict = self._api_client.request(
3776 ‘post’, path, request_dict, http_options
3777 )
3779 if self._api_client.vertexai:
3780 response_dict = _GenerateContentResponse_from_vertex(
3781 self._api_client, response_dict
3782 )

File ~/adk-made-simple/.venv/lib/python3.10/site-packages/google/genai/_api_client.py:726, in BaseApiClient.request(self, http_method, path, request_dict, http_options)
716 def request(
717 self,
718 http_method: str,
(…)
721 http_options: Optional[HttpOptionsOrDict] = None,
722 ) → Union[BaseResponse, Any]:
723 http_request = self._build_request(
724 http_method, path, request_dict, http_options
725 )
→ 726 response = self._request(http_request, stream=False)
727 json_response = response.json
728 if not json_response:

File ~/adk-made-simple/.venv/lib/python3.10/site-packages/google/genai/_api_client.py:655, in BaseApiClient._request(self, http_request, stream)
647 else:
648 response = self._httpx_client.request(
649 method=http_request.method,
650 url=http_request.url,
(…)
653 timeout=http_request.timeout,
654 )
→ 655 errors.APIError.raise_for_response(response)
656 return HttpResponse(
657 response.headers, response if stream else [response.text]
658 )

File ~/adk-made-simple/.venv/lib/python3.10/site-packages/google/genai/errors.py:101, in APIError.raise_for_response(cls, response)
99 status_code = response.status_code
100 if 400 <= status_code < 500:
→ 101 raise ClientError(status_code, response_json, response)
102 elif 500 <= status_code < 600:
103 raise ServerError(status_code, response_json, response)

ClientError: 400 INVALID_ARGUMENT. {‘error’: {‘code’: 400, ‘message’: ‘* GenerateContentRequest.contents[1].parts: contents.parts must not be empty.\n’, ‘status’: ‘INVALID_ARGUMENT’}}

I think the issue lies here. You are passing “text_input” and “image” in contents but “image” is not defined anywhere and that’s what error is saying: GenerateContentRequest.contents[1].parts: contents.parts must not be empty. The contents at index 1 which is variable “image” in your case is empty . If you want to generate only image from text only then correct snippet would like:

response = client.models.generate_content(
 model=“gemini-2.0-flash-preview-image-generation”,  contents=[text_input], config=types.GenerateContentConfig(
response_modalities=[‘TEXT’, ‘IMAGE’]

Hi @Shubham_Dhabu , Welcome to the forum.

Looks like the image was not generated. While initializing the client, which location did you set? For your information, gemini-2.0-flash-preview-image-generation model is not supported in some regions.

If possible, could you provide a code snippet showing how the image file is being loaded? It might be part of the problem.