zod.d.ts 3.0 KB

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