Skip to content

fix(provider/gemini): Prevent empty parts in assistant messages #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

monotykamary
Copy link
Contributor

@monotykamary monotykamary commented Jun 6, 2025

This PR addresses an issue where cancelling a chat request and promptly starting a new one could lead to an Error 400, Message: * GenerateContentRequest.contents[38].parts: contents.parts must not be empty. when using the Gemini provider.

The root cause was that assistant messages (model responses) were being included in the conversation history sent to the Gemini API even if they were empty (i.e., contained no text content or tool calls). This typically occurred during rapid interactions like cancelling and restarting a chat.

The fix modifies the convertMessages function in internal/llm/provider/gemini.go to ensure that an assistant message is only added to the history if msg.Content().String() != "" or len(msg.ToolCalls()) > 0. This prevents empty parts from being sent to the Gemini API.

Changes:

  • Modified internal/llm/provider/gemini.go:
    • Added a conditional check before appending assistant messages to the history. An assistant message is now only appended if it has actual content (text or tool calls).

To Test:

  1. Checkout this branch.
  2. Rebuild the opencode application (go build -o opencode).
  3. Configure Gemini as the AI provider.
  4. Attempt to reproduce the error by starting a chat, cancelling it (Ctrl+X), and immediately starting a new chat or sending a new message.
  5. The "contents.parts must not be empty" error should no longer occur.
┃ Hello. Who are you?

┃ I am OpenCode, an interactive CLI tool that helps users with software engineering tasks. Gemini 2.5 Pro (4ms)

┃ Write an essay on the Great Gatsby.

<cancel>

┃ ?

┃ I am a software engineering assistant and cannot write an essay. My purpose is to help with software development tasks. Gemini 2.5 Pro (9ms)

┃ What have I said so far to you?

┃ Here is a summary of our conversation so far:
┃
┃ 1. You said: "Hello. Who are you?"
┃ 2. I said: "I am OpenCode, an interactive CLI tool that helps users with software engineering tasks."
┃ 3. You said: "Write an essay on the Great Gatsby.?"
┃ 4. I said: "I am a software engineering assistant and cannot write an essay. My purpose is to help with software development tasks."
┃ 5. You said: "What have I said so far to you?" Gemini 2.5 Pro (5ms)

Copy link
Collaborator

@kujtimiihoxha kujtimiihoxha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR

@kujtimiihoxha kujtimiihoxha merged commit 18f020c into opencode-ai:main Jun 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants