zod.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.zodResponseFormat = zodResponseFormat;
  4. exports.zodTextFormat = zodTextFormat;
  5. exports.zodFunction = zodFunction;
  6. exports.zodResponsesFunction = zodResponsesFunction;
  7. const tslib_1 = require("../internal/tslib.js");
  8. const z4 = tslib_1.__importStar(require("zod/v4"));
  9. const parser_1 = require("../lib/parser.js");
  10. const zod_to_json_schema_1 = require("../_vendor/zod-to-json-schema/index.js");
  11. const ResponsesParser_1 = require("../lib/ResponsesParser.js");
  12. const transform_1 = require("../lib/transform.js");
  13. function zodV3ToJsonSchema(schema, options) {
  14. return (0, zod_to_json_schema_1.zodToJsonSchema)(schema, {
  15. openaiStrictMode: true,
  16. name: options.name,
  17. nameStrategy: 'duplicate-ref',
  18. $refStrategy: 'extract-to-root',
  19. nullableStrategy: 'property',
  20. });
  21. }
  22. function zodV4ToJsonSchema(schema) {
  23. return (0, transform_1.toStrictJsonSchema)(z4.toJSONSchema(schema, {
  24. target: 'draft-7',
  25. }));
  26. }
  27. function isZodV4(zodObject) {
  28. return '_zod' in zodObject;
  29. }
  30. /**
  31. * Creates a chat completion `JSONSchema` response format object from
  32. * the given Zod schema.
  33. *
  34. * If this is passed to the `.parse()`, `.stream()` or `.runTools()`
  35. * chat completion methods then the response message will contain a
  36. * `.parsed` property that is the result of parsing the content with
  37. * the given Zod object.
  38. *
  39. * ```ts
  40. * const completion = await client.chat.completions.parse({
  41. * model: 'gpt-4o-2024-08-06',
  42. * messages: [
  43. * { role: 'system', content: 'You are a helpful math tutor.' },
  44. * { role: 'user', content: 'solve 8x + 31 = 2' },
  45. * ],
  46. * response_format: zodResponseFormat(
  47. * z.object({
  48. * steps: z.array(z.object({
  49. * explanation: z.string(),
  50. * answer: z.string(),
  51. * })),
  52. * final_answer: z.string(),
  53. * }),
  54. * 'math_answer',
  55. * ),
  56. * });
  57. * const message = completion.choices[0]?.message;
  58. * if (message?.parsed) {
  59. * console.log(message.parsed);
  60. * console.log(message.parsed.final_answer);
  61. * }
  62. * ```
  63. *
  64. * This can be passed directly to the `.create()` method but will not
  65. * result in any automatic parsing, you'll have to parse the response yourself.
  66. */
  67. function zodResponseFormat(zodObject, name, props) {
  68. return (0, parser_1.makeParseableResponseFormat)({
  69. type: 'json_schema',
  70. json_schema: {
  71. ...props,
  72. name,
  73. strict: true,
  74. schema: isZodV4(zodObject) ? zodV4ToJsonSchema(zodObject) : zodV3ToJsonSchema(zodObject, { name }),
  75. },
  76. }, (content) => zodObject.parse(JSON.parse(content)));
  77. }
  78. function zodTextFormat(zodObject, name, props) {
  79. return (0, parser_1.makeParseableTextFormat)({
  80. type: 'json_schema',
  81. ...props,
  82. name,
  83. strict: true,
  84. schema: isZodV4(zodObject) ? zodV4ToJsonSchema(zodObject) : zodV3ToJsonSchema(zodObject, { name }),
  85. }, (content) => zodObject.parse(JSON.parse(content)));
  86. }
  87. /**
  88. * Creates a chat completion `function` tool that can be invoked
  89. * automatically by the chat completion `.runTools()` method or automatically
  90. * parsed by `.parse()` / `.stream()`.
  91. */
  92. function zodFunction(options) {
  93. // @ts-expect-error TODO
  94. return (0, parser_1.makeParseableTool)({
  95. type: 'function',
  96. function: {
  97. name: options.name,
  98. parameters: isZodV4(options.parameters) ?
  99. zodV4ToJsonSchema(options.parameters)
  100. : zodV3ToJsonSchema(options.parameters, { name: options.name }),
  101. strict: true,
  102. ...(options.description ? { description: options.description } : undefined),
  103. },
  104. }, {
  105. callback: options.function,
  106. parser: (args) => options.parameters.parse(JSON.parse(args)),
  107. });
  108. }
  109. function zodResponsesFunction(options) {
  110. return (0, ResponsesParser_1.makeParseableResponseTool)({
  111. type: 'function',
  112. name: options.name,
  113. parameters: isZodV4(options.parameters) ?
  114. zodV4ToJsonSchema(options.parameters)
  115. : zodV3ToJsonSchema(options.parameters, { name: options.name }),
  116. strict: true,
  117. ...(options.description ? { description: options.description } : undefined),
  118. }, {
  119. callback: options.function,
  120. parser: (args) => options.parameters.parse(JSON.parse(args)),
  121. });
  122. }
  123. //# sourceMappingURL=zod.js.map