Firebase Function onFlow Throws ZodError: invalid_type When Using Input Schema Validation

  Kiến thức lập trình

I’m working with Firebase Cloud Functions and the @genkit-ai library to create a flow using onFlow. I’ve defined a simple flow called menuSuggestionFlow, where I validate the input using a Zod schema. However, when I try to invoke this flow, I receive a ZodError indicating that the input is undefined, even though I believe I’m sending the correct data structure.

import { z } from '@genkit-ai/core/schema';
import { onFlow, noAuth } from '@genkit-ai/firebase/functions';

// Define the schema
export const ChatSessionInputSchema = z.object({
  question: z.string(),
});

export const menuSuggestionFlow = onFlow(
  {
    name: "menuSuggestionFlow",
    inputSchema: ChatSessionInputSchema, // Apply the schema here
    authPolicy: noAuth(),
  },
  async (subject) => {
    console.log(`Validated subject: ${JSON.stringify(subject)}`);

    if (!subject || !subject.question) {
      throw new Error("The 'question' field is required.");
    }

    const answer = `This is the answer to your question: ${subject.question}`;
    return { answer };
  }
);

menuSuggestionFlow({ data: { question: “What is the menu suggestion for today?” } })
menuSuggestionFlow( { question: “What is the menu suggestion for today?” } )

Your code seems to be correct, so I assume there might be something wrong with how you’re calling the flow.

In my environment, your code is working for both the Dev UI and curl POST.

  • Dev UI (run genkit start -o)
  • curl
$ curl -X POST -H "Content-Type: application/json" -d '{"data": {"question": "What is the menu suggestion for today?"}}' http://[your_firebase_function_url]/menuSuggestionFlow
{"result":{"answer":"This is the answer to your question: What is the menu suggestion for today?"}}

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT