Skip to content

Commit

Permalink
fix(generate-object): validate
Browse files Browse the repository at this point in the history
  • Loading branch information
kwaa committed Feb 25, 2025
1 parent 52ca1c2 commit fa3d92d
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions packages/generate-object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,33 @@ export async function generateObject<T extends Schema>(options: GenerateObjectOp
schemaDescription: undefined, // Remove schemaDescription from options
schemaName: undefined, // Remove schemaName from options
}).then(async ({ finishReason, messages, steps, text, toolCalls, toolResults, usage }) => {
const object = JSON.parse(text!) as Infer<T>
const json: unknown = JSON.parse(text!)

if (options.output === 'array') {
const elements = (object as { elements: Array<Infer<T>> }).elements
for (const element of elements) {
await validate(schemaValidator, element as InferIn<T>)
}

return {
finishReason,
messages,
object: elements,
object: await Promise.all((json as { elements: InferIn<T>[] })
.elements
.map(async element => validate(schemaValidator, element))),
steps,
text,
toolCalls,
toolResults,
usage,
}
}

return {
finishReason,
messages,
object,
steps,
text,
toolCalls,
toolResults,
usage,
else {
return {
finishReason,
messages,
object: await validate(options.schema, json as InferIn<T>),
steps,
text,
toolCalls,
toolResults,
usage,
}
}
})
}

0 comments on commit fa3d92d

Please sign in to comment.