Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/generate-object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export interface GenerateObjectOptions<T extends Schema> extends GenerateTextOpt
schema: T
schemaDescription?: string
schemaName?: string
/** @default true */
strict?: boolean
}

export type GenerateObjectResult<O> = GenerateTextResult & { object: O }
Expand All @@ -21,10 +23,10 @@ export async function generateObject<T extends Schema>(options: GenerateObjectOp
export async function generateObject<T extends Schema>(options: GenerateObjectOptions<T>): Promise<GenerateObjectResult<Infer<T>>>
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
export async function generateObject<T extends Schema>(options: GenerateObjectOptions<T> & { 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)
Expand All @@ -36,13 +38,14 @@ export async function generateObject<T extends Schema>(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!)

Expand All @@ -52,7 +55,7 @@ export async function generateObject<T extends Schema>(options: GenerateObjectOp
messages,
object: await Promise.all((json as { elements: InferIn<T>[] })
.elements
.map(async element => validate(schemaValidator, element))),
.map(async element => validate(options.schema, element))),
steps,
text,
toolCalls,
Expand Down
11 changes: 7 additions & 4 deletions packages/stream-object/src/utils/stream-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export interface StreamObjectOptions<T extends Schema> extends StreamTextOptions
schema: T
schemaDescription?: string
schemaName?: string
/** @default true */
strict?: boolean
}

export interface StreamObjectResult<T extends Schema> extends StreamTextResult {
Expand All @@ -40,10 +42,10 @@ export async function streamObject<T extends Schema>(
options: StreamObjectOptions<T>
& { output?: 'array' | 'object' },
): Promise<StreamObjectResult<T>> {
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)
Expand All @@ -55,13 +57,14 @@ export async function streamObject<T extends Schema>(
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<Infer<T>> | undefined
let partialObjectStream: ReadableStream<PartialDeep<Infer<T>>> | undefined
Expand Down
Loading