google-gemini/generative-ai-android

[v0.2.1] API Error 400: please use a valid role: user, model

PatilShreyas opened this issue · 0 comments

Issue

In the recently released v0.2.1, while using Chat methods, a server error like following occurs even if we provide a role.

please use a valid role: user, model

Steps to reproduce

  • In the sample app, change the Generative AI SDK version to the 0.2.1
  • Run the app
  • Go to chat
  • Type a prompt
  • Click send

Reason

After I checked a diff between v0.2.0v0.2.1, observed following issues:

Changing a default value in the above internal class is ineffective since it's not a public API that will be instantiated from a public API. Instead, when a public Content is converted to an internal Content, it will be replaced by a value null since the same default value has not been provided to the public class Content. At the time of conversion from public type to internal type, this role's default value will be lost by the following code:
https://github.com/google/generative-ai-android/blob/7abb340e595a042eed2f4f539e57df4007736fde/generativeai/src/main/java/com/google/ai/client/generativeai/internal/util/conversions.kt#L49-L50

Possible fixes

So as per my understanding, the default value for role is needed in the following files:

OR

We can also make changes in a conversion function as follows:

internal fun com.google.ai.client.generativeai.type.Content.toInternal() =
- Content(this.role, this.parts.map { it.toInternal() })
+ Content(this.role ?: "user", this.parts.map { it.toInternal() })