diff --git a/packages/generate-object/src/index.ts b/packages/generate-object/src/index.ts index 4250a2ce..e8fc4733 100644 --- a/packages/generate-object/src/index.ts +++ b/packages/generate-object/src/index.ts @@ -10,6 +10,8 @@ export interface GenerateObjectOptions extends GenerateTextOpt schema: T schemaDescription?: string schemaName?: string + /** @default true */ + strict?: boolean } export type GenerateObjectResult = GenerateTextResult & { object: O } @@ -21,10 +23,10 @@ export async function generateObject(options: GenerateObjectOp export async function generateObject(options: GenerateObjectOptions): Promise>> // eslint-disable-next-line prefer-arrow/prefer-arrow-functions export async function generateObject(options: GenerateObjectOptions & { output?: GenerateObjectOutputOption }) { - const { schema: schemaValidator } = options + let schema = await toJsonSchema(options.schema) - let schema = await toJsonSchema(schemaValidator) - .then(schema => strictJsonSchema(schema)) + if (options.strict !== false) + schema = strictJsonSchema(schema) if (options.output === 'array') schema = wrap(schema) @@ -36,13 +38,14 @@ export async function generateObject(options: GenerateObjectOp description: options.schemaDescription, name: options.schemaName ?? 'json_schema', schema, - strict: true, + strict: options.strict ?? true, }, type: 'json_schema', }, schema: undefined, // Remove schema from options schemaDescription: undefined, // Remove schemaDescription from options schemaName: undefined, // Remove schemaName from options + strict: undefined, // Remove strict from options }).then(async ({ finishReason, messages, steps, text, toolCalls, toolResults, usage }) => { const json: unknown = JSON.parse(text!) @@ -52,7 +55,7 @@ export async function generateObject(options: GenerateObjectOp messages, object: await Promise.all((json as { elements: InferIn[] }) .elements - .map(async element => validate(schemaValidator, element))), + .map(async element => validate(options.schema, element))), steps, text, toolCalls, diff --git a/packages/stream-object/src/utils/stream-object.ts b/packages/stream-object/src/utils/stream-object.ts index df225eed..0fda68a1 100644 --- a/packages/stream-object/src/utils/stream-object.ts +++ b/packages/stream-object/src/utils/stream-object.ts @@ -17,6 +17,8 @@ export interface StreamObjectOptions extends StreamTextOptions schema: T schemaDescription?: string schemaName?: string + /** @default true */ + strict?: boolean } export interface StreamObjectResult extends StreamTextResult { @@ -40,10 +42,10 @@ export async function streamObject( options: StreamObjectOptions & { output?: 'array' | 'object' }, ): Promise> { - const { schema: schemaValidator } = options + let schema = await toJsonSchema(options.schema) - let schema = await toJsonSchema(schemaValidator) - .then(schema => strictJsonSchema(schema)) + if (options.strict !== false) + schema = strictJsonSchema(schema) if (options.output === 'array') schema = wrap(schema) @@ -55,13 +57,14 @@ export async function streamObject( description: options.schemaDescription, name: options.schemaName ?? 'json_schema', schema, - strict: true, + strict: options.strict ?? true, }, type: 'json_schema', }, schema: undefined, schemaDescription: undefined, schemaName: undefined, + strict: undefined, }).then(({ textStream, ...rest }) => { let elementStream: ReadableStream> | undefined let partialObjectStream: ReadableStream>> | undefined