zod.d.mts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { ResponseFormatJSONSchema } from "../resources/index.mjs";
  2. import * as z3 from 'zod/v3';
  3. import * as z4 from 'zod/v4';
  4. import { AutoParseableResponseFormat, AutoParseableTextFormat, AutoParseableTool } from "../lib/parser.mjs";
  5. import { AutoParseableResponseTool } from "../lib/ResponsesParser.mjs";
  6. import { type ResponseFormatTextJSONSchemaConfig } from "../resources/responses/responses.mjs";
  7. type InferZodType<T> = T extends z4.ZodType ? z4.infer<T> : T extends z3.ZodType ? z3.infer<T> : never;
  8. /**
  9. * Creates a chat completion `JSONSchema` response format object from
  10. * the given Zod schema.
  11. *
  12. * If this is passed to the `.parse()`, `.stream()` or `.runTools()`
  13. * chat completion methods then the response message will contain a
  14. * `.parsed` property that is the result of parsing the content with
  15. * the given Zod object.
  16. *
  17. * ```ts
  18. * const completion = await client.chat.completions.parse({
  19. * model: 'gpt-4o-2024-08-06',
  20. * messages: [
  21. * { role: 'system', content: 'You are a helpful math tutor.' },
  22. * { role: 'user', content: 'solve 8x + 31 = 2' },
  23. * ],
  24. * response_format: zodResponseFormat(
  25. * z.object({
  26. * steps: z.array(z.object({
  27. * explanation: z.string(),
  28. * answer: z.string(),
  29. * })),
  30. * final_answer: z.string(),
  31. * }),
  32. * 'math_answer',
  33. * ),
  34. * });
  35. * const message = completion.choices[0]?.message;
  36. * if (message?.parsed) {
  37. * console.log(message.parsed);
  38. * console.log(message.parsed.final_answer);
  39. * }
  40. * ```
  41. *
  42. * This can be passed directly to the `.create()` method but will not
  43. * result in any automatic parsing, you'll have to parse the response yourself.
  44. */
  45. export declare function zodResponseFormat<ZodInput extends z3.ZodType | z4.ZodType>(zodObject: ZodInput, name: string, props?: Omit<ResponseFormatJSONSchema.JSONSchema, 'schema' | 'strict' | 'name'>): AutoParseableResponseFormat<InferZodType<ZodInput>>;
  46. export declare function zodTextFormat<ZodInput extends z3.ZodType | z4.ZodType>(zodObject: ZodInput, name: string, props?: Omit<ResponseFormatTextJSONSchemaConfig, 'schema' | 'type' | 'strict' | 'name'>): AutoParseableTextFormat<InferZodType<ZodInput>>;
  47. /**
  48. * Creates a chat completion `function` tool that can be invoked
  49. * automatically by the chat completion `.runTools()` method or automatically
  50. * parsed by `.parse()` / `.stream()`.
  51. */
  52. export declare function zodFunction<Parameters extends z3.ZodType | z4.ZodType>(options: {
  53. name: string;
  54. parameters: Parameters;
  55. function?: ((args: InferZodType<Parameters>) => unknown | Promise<unknown>) | undefined;
  56. description?: string | undefined;
  57. }): AutoParseableTool<{
  58. arguments: Parameters;
  59. name: string;
  60. function: (args: InferZodType<Parameters>) => unknown;
  61. }>;
  62. export declare function zodResponsesFunction<Parameters extends z3.ZodType | z4.ZodType>(options: {
  63. name: string;
  64. parameters: Parameters;
  65. function?: ((args: InferZodType<Parameters>) => unknown | Promise<unknown>) | undefined;
  66. description?: string | undefined;
  67. }): AutoParseableResponseTool<{
  68. arguments: Parameters;
  69. name: string;
  70. function: (args: InferZodType<Parameters>) => unknown;
  71. }>;
  72. export {};
  73. //# sourceMappingURL=zod.d.mts.map