Matt Luo
Matt Luo10mo ago

passing json to convex mutation

Do you know of any examples of passing a JSON to a mutation? I am able to accomplish this on the client side of Next.js but not on the server side. My use case is that after I have finished streaming a structured response in the actions.ts of this Vercel AI SDK tutorial (https://sdk.vercel.ai/examples/next-app/basics/streaming-object-generation), then I want to insert the object (essentially, it's a JSON) in the Convex DB by creating a Convex DB document per JSON element. I am able to accomplish a successful save on the client side, the app/page.tsx of this tutorial, but only with the end user clicking a save button
4 Replies
Michal Srb
Michal Srb10mo ago
You can: 1. Define the args shape and pass the deserialized json to the mutation 2. Use v.any() and pass the deserialized json to the mutation 3. Pass the json string into the mutation and store it as a string
Matt Luo
Matt LuoOP10mo ago
Ah interesting- I missed seeing that any type was available. It is not in the chart in https://docs.convex.dev/functions/args-validation but I see it now in the docs as this sentence: Additionally you can also express type unions, literals, any types, and optional fields.
Argument Validation | Convex Developer Hub
Argument validators ensure that queries,
Matt Luo
Matt LuoOP9mo ago
By the way, @Michal Srb , I could never figure out how to save the partialObjectStream variable from this Vercel AI SDK documentation to Convex DB. If you come across a successful insertion, let me know! https://sdk.vercel.ai/docs/reference/ai-sdk-core/stream-object
import { openai } from '@ai-sdk/openai';
import { streamObject } from 'ai';
import { z } from 'zod';

const { partialObjectStream } = await streamObject({
model: openai('gpt-4-turbo'),
schema: z.object({
recipe: z.object({
name: z.string(),
ingredients: z.array(z.string()),
steps: z.array(z.string()),
}),
}),
prompt: 'Generate a lasagna recipe.',
});

for await (const partialObject of partialObjectStream) {
console.clear();
console.log(partialObject);
}
import { openai } from '@ai-sdk/openai';
import { streamObject } from 'ai';
import { z } from 'zod';

const { partialObjectStream } = await streamObject({
model: openai('gpt-4-turbo'),
schema: z.object({
recipe: z.object({
name: z.string(),
ingredients: z.array(z.string()),
steps: z.array(z.string()),
}),
}),
prompt: 'Generate a lasagna recipe.',
});

for await (const partialObject of partialObjectStream) {
console.clear();
console.log(partialObject);
}
AI SDK Core: streamObject
API Reference for streamObject
Michal Srb
Michal Srb9mo ago
@sshader probably figured that out

Did you find this page helpful?