Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Node JS sdk, system_instructions invalid, but are a simple string.

I am making a very simple attempt using the Node JS sdk to make a chat message request with some systemInstructions defined, using 1.5

 

const chat = genModel.startChat({
systemInstruction:
"You are a happy dad and answer questions in a subservient manner.",
});
const x = await chat.sendMessage("Can I have some juice?");

 

 

 

But, I am getting an error that the system_instruction is invalid:

error: GoogleGenerativeAIFetchError: [GoogleGenerativeAI Error]: Error fetching from ... [400 Bad Request] Invalid value at 'system_instruction' (type.googleapis.com/google.ai.generativelanguage.v1beta.Content), "You are a happy dad
and answer questions in a subservient manner." [{"@type":"type.googleapis.com/google.rpc.BadRequest","fieldViolations":[{"field":"system_instruction","description":"Invalid value at 'system_instruction' (type.googleapis.com/google.ai.generativelanguage.v1beta.Content), \"You are a happy dad and answer questions in a subservi
ent manner.\""}]}
Solved Solved
1 2 1,614
1 ACCEPTED SOLUTION

Figured it out. Although the Node js SDK indicates that you can put `systemInstruction` in the startChat api......this is false and wrong.

You need to put the systemInstruction in the place that you make the `getGenerativeModel` call. Like this

const genModel = genAI.getGenerativeModel(
{
model: modelName,
systemInstruction:
"You are a happy dad and answer questions in a subservient manner.",
},
{ apiVersion: API_VERSION },
);

View solution in original post

2 REPLIES 2

Figured it out. Although the Node js SDK indicates that you can put `systemInstruction` in the startChat api......this is false and wrong.

You need to put the systemInstruction in the place that you make the `getGenerativeModel` call. Like this

const genModel = genAI.getGenerativeModel(
{
model: modelName,
systemInstruction:
"You are a happy dad and answer questions in a subservient manner.",
},
{ apiVersion: API_VERSION },
);

const cacheManager = new GoogleAICacheManager(process.env.GEMINI_KEY);
  let cache = null
  try {
    cache = await cacheManager.create({
      name: "ai expert",
      model: "gemini-1.5-flash-001",
      systemInstruction: "how AI works?"
    });
    const genAI = new GoogleGenerativeAI(process.env.GEMINI_KEY);
    const cacheModel = genAI.getGenerativeModelFromCachedContent(cache)
    const content = await cacheModel.generateContent(["why we need ai"])

    return {cache, cacheManager, content: content }
  } catch (error) {
    return error
  }

I am trying to create context. I tried this solution, but it didn’t work with this code. Is there any other way to do same?