Handling JSONDecodeError in Python

I input plain text to translate it to another language. Sometimes, I get a good chunk in response, but I can also get a bad chunk that Python can’t parse. In that case, I get a

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

error. How can I process that error? Is there any way around it?
I am using gemini-2.5-pro-exp-03-25 model.

Hi @prottozz,

Welcome to forum, are you trying to parse the text which is received as a json output as text output or using structured output scenario: Generate structured output with the Gemini API  |  Google AI for Developers

if you didn’t tried structured you can give it shot, because it will have response.parsed field that is already parsed for you.

Thank you!

Hello.

No, I just use

chunk.text

The error was because I used safety settings. I removed them, and now it works again.