agents icon indicating copy to clipboard operation
agents copied to clipboard

Google gemini-2.5-flash responses can throw several errors that are fatal to the worker

Open DavidHuie opened this issue 8 months ago • 7 comments

I've experienced both of these errors in the middle of a conversation. Both shut down the conversation and prevent further progress.

livekit.agents._exceptions.APIStatusError: gemini llm: client error (status_code=400, request_id=d18a6998476a, body=Please ensure that single turn requests end with a user role or the role field is empty.INVALID_ARGUMENT)

livekit.agents._exceptions.APIConnectionError: gemini llm: error generating content No candidates in the response (status_code=-1, request_id=bd1f1400344a, body=None)

DavidHuie avatar Apr 25 '25 16:04 DavidHuie

I have seen the same

NikoLandgraf avatar Apr 30 '25 16:04 NikoLandgraf

Same here even with flash-2.0. I cannot use session.generate_reply with any of the google models

ramon-prieto avatar May 01 '25 00:05 ramon-prieto

This bug needs to be fixed as it causes applications to stop working completely and cannot be recovered. Sometimes, Google returns a None value in response.parts. According to some forums, this is due to content moderation, although subsequent requests work correctly.

anlagbr avatar May 15 '25 08:05 anlagbr

could anyone share a repro? what sequence of actions would cause this to occur?

davidzhao avatar May 18 '25 19:05 davidzhao

@davidzhao

You can try with a simple setup — GEMINI API keys have a free tier. You can also use Groq Whisper free tier and any TTS model.

It's difficult to reproduce the issue because Google sometimes returns None, but after that, the conversation seems to work fine. Trying with sensitive prompts might help to reproduce the problem.

anlagbr avatar May 19 '25 06:05 anlagbr

The first Please ensure that single turn requests end with a user role should be already fixed in https://github.com/livekit/agents/pull/2183, for the second No candidates in the response, probably we should just ignore the empty response instead of raising the error.

longcw avatar May 19 '25 07:05 longcw

@longcw

Correct. Ignoring the No candidates in the response is the correct approach. I modified some lines of the original gemini plugin in my codebase to ignore the empty response

anlagbr avatar May 19 '25 07:05 anlagbr

also experiencing this issue. it's especially bad for longer calls since this causes the entire call to fail, even with fallback adapters in place

bryanhoulton avatar May 21 '25 22:05 bryanhoulton

{"message": "no candidates in the response: sdk_http_response=HttpResponse(\n headers=\n) candidates=[Candidate(\n finish_reason=<FinishReason.OTHER: 'OTHER'>,\n index=0\n)] create_time=None response_id=None model_version='gemini-2.5-flash' prompt_feedback=None usage_metadata=GenerateContentResponseUsageMetadata(\n cache_tokens_details=[\n ModalityTokenCount(\n modality=<MediaModality.TEXT: 'TEXT'>,\n token_count=8525\n ),\n ],\n cached_content_token_count=8525,\n candidates_token_count=89,\n prompt_token_count=8631,\n prompt_tokens_details=[\n ModalityTokenCount(\n modality=<MediaModality.TEXT: 'TEXT'>,\n token_count=8631\n ),\n ],\n thoughts_token_count=126,\n total_token_count=8846\n) automatic_function_calling_history=None parsed=None", "level": "WARNING", "name": "livekit.plugins.google", "pid": 395397, "job_id": "AJ_Zsee5VXWuZze", "timestamp": "2025-08-06T16:07:24.328512+00:00"}

The agent has stopped responding in the middle of the conversation, I am using the latest scripts only with the above fixes. Can anyone help me to resolve this issue?

satheeshkola-532 avatar Aug 06 '25 16:08 satheeshkola-532

"level": "WARNING"

                if (
                    not response.candidates
                    or not response.candidates[0].content
                    or not response.candidates[0].content.parts
                ):
                    logger.warning(f"no candidates in the response: {response}")
                    continue

this is a warning and the conversation is allowed to continue. unfortunately if Gemini doesn't send back any responses, there's nothing we could do.

davidzhao avatar Aug 06 '25 16:08 davidzhao

"level": "WARNING"

                if (
                    not response.candidates
                    or not response.candidates[0].content
                    or not response.candidates[0].content.parts
                ):
                    logger.warning(f"no candidates in the response: {response}")
                    continue

this is a warning and the conversation is allowed to continue. unfortunately if Gemini doesn't send back any responses, there's nothing we could do.

Thank you for your response. It seems to be working with Gemini 2.0 Flash instead of 2.5 Flash, based on the tests I’ve run so far—but I’m still not completely sure.

satheeshkola-532 avatar Aug 06 '25 16:08 satheeshkola-532

2025-09-18 04:17:28,022 - WARNING livekit.plugins.google - no candidates in the response: sdk_http_response=HttpResponse( headers= ) candidates=[Candidate( content=Content( role='model' ), finish_reason=<FinishReason.STOP: 'STOP'>, index=0 )] create_time=None model_version='gemini-2.5-flash' prompt_feedback=None response_id='2IfLaNMEmbCq2w_6i_WpDw' usage_metadata=GenerateContentResponseUsageMetadata( prompt_token_count=4589, prompt_tokens_details=[ ModalityTokenCount( modality=<MediaModality.TEXT: 'TEXT'>, token_count=4589 ), ], total_token_count=4589 ) automatic_function_calling_history=None parsed=None {"pid": 71, "job_id": "AJ_apM9tfCyuNjG"}

i am consistenly seeing this problem with gemini 2.5 flash but not in 2.0 flash

Vri6500 avatar Sep 18 '25 04:09 Vri6500