Next.js Discord

Discord Forum

Client Functions cannot be passed directly to Server Functions.

Unanswered
Golden paper wasp posted this in #help-forum
Open in Discord
Golden paper waspOP
I am trying to use the AI SDK but i keep getting this error
Error: Client Functions cannot be passed directly to Server Functions. Only Functions passed from the Server can be passed back again.

and i am following the docs example:
server action example: https://github.com/vercel/ai/blob/main/examples/next-ai-rsc/app/stream-object/generate-itinerary.tsx

My Server Action:
export async function getTriageLevel(prompt: string) {
  "use server";

  const history = getMutableAIState();

  const stream = createStreamableValue();

  history.update([...history.get(), { role: "user", content: prompt }]);

  (async () => {
    const { partialObjectStream } = await streamObject({
      model: google("models/gemini-1.5-pro-latest"),
      system: SYSTEM_PROMPT,
      messages: history.get(),
      schema: triageResultSchema,
    });

    for await (const partialObject of partialObjectStream) {
      stream.update(partialObject);
    }

    stream.done();
  })();

  history.done([
    ...history.get(),
    {
      role: "assistant",
      content: { object: stream.value },
    },
  ]);

  return { object: stream.value };
}


My client component:
setMessages((prevMessages: UIState) => [
        ...prevMessages,
        { id: Date.now().toString(), role: "user", content: prompt },
      ]);

      const { object } = await getTriageLevel(prompt);

      for await (const partialObject of readStreamableValue(object)) {
        if (partialObject) {
          console.log(partialObject);

          setGeneration(partialObject);
        }
      }

      setMessages((prevMessages: UIState) => [
        ...prevMessages,
        {
          id: Date.now().toString(),
          role: "assistant",
          content: generation as PartialTriageResult,
        },
      ]);

1 Reply

Airedale Terrier