L5-text-generation error

I am getting an error when trying to predict using the TextGenerationModel text-bison as demonstrated in the lab.

I tried switching the model to text-bison-002 and other model versions listed in the PaLM Model Garden here but nothing seemed to work.

import vertexai
vertexai.init(project=PROJECT_ID, 
              location=REGION, 
              credentials = credentials)
from vertexai.language_models import TextGenerationModel
generation_model = TextGenerationModel.from_pretrained("text-bison-001")

print(generation_model.predict(prompt=prompt).text)
---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
File /usr/local/lib/python3.10/site-packages/requests/models.py:971, in Response.json(self, **kwargs)
    970 try:
--> 971     return complexjson.loads(self.text, **kwargs)
    972 except JSONDecodeError as e:
    973     # Catch JSON-related errors and raise as requests.JSONDecodeError
    974     # This aliases json.JSONDecodeError and simplejson.JSONDecodeError

File /usr/local/lib/python3.10/json/__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    343 if (cls is None and object_hook is None and
    344         parse_int is None and parse_float is None and
    345         parse_constant is None and object_pairs_hook is None and not kw):
--> 346     return _default_decoder.decode(s)
    347 if cls is None:

File /usr/local/lib/python3.10/json/decoder.py:340, in JSONDecoder.decode(self, s, _w)
    339 if end != len(s):
--> 340     raise JSONDecodeError("Extra data", s, end)
    341 return obj

JSONDecodeError: Extra data: line 1 column 5 (char 4)

During handling of the above exception, another exception occurred:

JSONDecodeError                           Traceback (most recent call last)
Cell In[14], line 1
----> 1 print(generation_model.predict(prompt=prompt).text)

File /usr/local/lib/python3.10/site-packages/vertexai/language_models/_language_models.py:263, in TextGenerationModel.predict(self, prompt, max_output_tokens, temperature, top_k, top_p)
    241 def predict(
    242     self,
    243     prompt: str,
   (...)
    248     top_p: float = _DEFAULT_TOP_P,
    249 ) -> "TextGenerationResponse":
    250     """Gets model response for a single prompt.
    251 
    252     Args:
   (...)
    260         A `TextGenerationResponse` object that contains the text produced by the model.
    261     """
--> 263     return self._batch_predict(
    264         prompts=[prompt],
    265         max_output_tokens=max_output_tokens,
    266         temperature=temperature,
    267         top_k=top_k,
    268         top_p=top_p,
    269     )[0]

File /usr/local/lib/python3.10/site-packages/vertexai/language_models/_language_models.py:342, in TextGenerationModel._batch_predict(self, prompts, max_output_tokens, temperature, top_k, top_p)
    309 """Gets model response for a single prompt.
    310 
    311 Args:
   (...)
    319     A list of `TextGenerationResponse` objects that contain the texts produced by the model.
    320 """
    321 # instances = [{"content": str(prompt)} for prompt in prompts]
    322 # prediction_parameters = {
    323 #     "temperature": temperature,
   (...)
    339 #     for prediction in prediction_response.predictions
    340 # ]
--> 342 prediction_response = self._dlai_custom_api(prompts[0], temperature, top_p, top_k, max_output_tokens)
    344 return [
    345     TextGenerationResponse(
    346         text=prediction_response["text"],
    347         _prediction_response=None
    348     )
    349 ]

File /usr/local/lib/python3.10/site-packages/vertexai/language_models/_language_models.py:299, in TextGenerationModel._dlai_custom_api(self, prompt, temperature, top_p, top_k, max_output_tokens)
    294 headers = {
    295     'Content-Type': 'application/json',
    296     'Authorization': f'Bearer {API_KEY}'
    297 }
    298 response = requests.request("POST", url, headers=headers, data=json.dumps(payload))
--> 299 return response.json()

File /usr/local/lib/python3.10/site-packages/requests/models.py:975, in Response.json(self, **kwargs)
    971     return complexjson.loads(self.text, **kwargs)
    972 except JSONDecodeError as e:
    973     # Catch JSON-related errors and raise as requests.JSONDecodeError
    974     # This aliases json.JSONDecodeError and simplejson.JSONDecodeError
--> 975     raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)

JSONDecodeError: Extra data: line 1 column 5 (char 4)
1 Like

Dear @matt-s,

It seems that Vertex AI API is returning a response that is not valid JSON — so response.json() fails. There are many reason for it.

Try after some time, If problem still persists, Let me know.


Keep Learning AI with DeepLearning.AI - Girijesh

Thank you for reporting this @matt-s ! Indeed the “text-bison-002” has been deprecated this 2025, check the documentation here.

It is recommended to use now gemini-2.0-flash-lite. We’re currently updating our notebooks in the platform! Soon, this update will be online! In the meanwhile, I’ve tested L5 examples in this google colab if you want to give a check!