Skip to content

[JS] VertexAI.ClientError when providing context from retriever and Gemini calling a provided tool #452

@jeff-treuting

Description

@jeff-treuting

Describe the bug
When calling generate with an array of available tools and providing the context parameter the results of a Firestore Retriever call, the call errors out with the following error:

{"severity":"ERROR","message":"Unhandled error Error: Vertex response generation failed: ClientError: [VertexAI.ClientError]: Within a single message, FunctionResponse cannot be mixed with other type of part in the request for sending chat message.\n    at /xxxx/node_modules/@genkit-ai/vertexai/lib/gemini.js:514:17\n    at Generator.throw (<anonymous>)\n    at rejected (/xxxx/node_modules/@genkit-ai/vertexai/lib/gemini.js:50:29)\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"}

To Reproduce
Here is some simplified code that shows the setup that produces the error for me every time. When I comment out passing in the context, it works as expected.

const myTool = defineTool(
  {
    name: "myTool",
    description:
      "When a question about the calendar bookings or reservations is asked, this tool will load the calendar booking data.",
    inputSchema: z.object({
      beginDate: z
        .string()
        .optional()
        .describe("The beginning date to load the calendar data for."),
      endDate: z
        .string()
        .optional()
        .describe("The end date to load the calendar data for."),
      member: z
        .string()
        .optional()
        .describe("The member ID or name to filter the calendar bookings by."),
    }),
    outputSchema: z.string(),
  },
  async (input) => "haha Just kidding no joke about for you! got you"
);

    const myRetrieverRef = defineFirestoreRetriever({
      name: "retriever-id",
      firestore: getFirestore(),
      collection: "calendar/vectorIndex",
      contentField: "content",
      vectorField: "embedding",
      embedder: textEmbeddingGecko,
      distanceMeasure: "COSINE", // 'EUCLIDEAN', 'DOT_PRODUCT', or 'COSINE' (default)
    });

    const docs = await retrieve({
      retriever: myRetrieverRef,
      query: userMessage,
      options: {
        limit: 5,
        k: 3,
      },
    });

    const myPrompt = await prompt("myBot");
    const result = await myPrompt.generate({
      input: {
        message: userMessage,
        today: format(new Date(), "yyyy-MM-dd"), // "2022-01-01
        user: {
          id: request.auth?.uid || "anonymous",
          name: request.auth?.token.name || "anonymous",
        },
      },
      context: docs, // when I comment out this line the error goes away regardless of what tool I include, I've tried a few different versions of it
      tools: [myTool],
    });
    ```

**Expected behavior**
I would expect to be able to get results that utilize a cusotm tool for retrieving information form a data source and provide context from a retriever as well.


**Runtime (please complete the following information):**
 - OS: MacOS
 - Version: Sonoma 14.4.1
** Node version
 -  v20.11.1

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingjs

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions