Fix: retry on invalid JSON from Gemini #829
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Sometimes, the response from Gemini (and I assume the other LLM services) is not in a valid JSON format. This makes the
json.loads-call throw an exception, and the service-call returns an empty dict.This is frustrating, as the resulting markdown will have missing image descriptions, tables that have not been refined, etc. Since the error is ignored and the program moves on, one has to carefully check the logs during conversion, and then rerun the conversion if it happens.
IMHO, the most logical thing to do if the LLM service returns malformed JSON is to try again, in the same way we do if we are rate limited.
Note: this fix only handles the problem for the Gemini service. LMK if you want me to fix this for the other services as well.
Logs where this happens: