@ai-sdk/perplexity 5.0.16 → 5.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,8 @@
1
+ import {
2
+ lazySchema,
3
+ zodSchema,
4
+ type InferSchema,
5
+ } from '@ai-sdk/provider-utils';
1
6
  import type { JSONSchema7 } from '@ai-sdk/provider';
2
7
  import { z } from 'zod/v4';
3
8
 
@@ -11,35 +16,38 @@ export type PerplexityAgentTool =
11
16
  }
12
17
  | ({ type: string } & Record<string, unknown>);
13
18
 
14
- export const perplexitySearchResultSchema = z.looseObject({
15
- id: z.number().optional(),
16
- title: z.string(),
17
- url: z.string(),
18
- snippet: z.string().optional(),
19
- date: z.string().nullish(),
20
- last_updated: z.string().nullish(),
21
- source: z.string().optional(),
22
- });
19
+ export const perplexitySearchResultSchema = () =>
20
+ z.looseObject({
21
+ id: z.number().optional(),
22
+ title: z.string(),
23
+ url: z.string(),
24
+ snippet: z.string().optional(),
25
+ date: z.string().nullish(),
26
+ last_updated: z.string().nullish(),
27
+ source: z.string().optional(),
28
+ });
23
29
 
24
- const perplexityFetchedContentSchema = z.looseObject({
25
- title: z.string(),
26
- url: z.string(),
27
- snippet: z.string().optional(),
28
- });
30
+ const perplexityFetchedContentSchema = () =>
31
+ z.looseObject({
32
+ title: z.string(),
33
+ url: z.string(),
34
+ snippet: z.string().optional(),
35
+ });
29
36
 
30
- const perplexityContentPartSchema = z.looseObject({
31
- type: z.string(),
32
- text: z.string().optional(),
33
- annotations: z
34
- .array(
35
- z.looseObject({
36
- type: z.string().optional(),
37
- url: z.string().optional(),
38
- title: z.string().optional(),
39
- }),
40
- )
41
- .nullish(),
42
- });
37
+ const perplexityContentPartSchema = () =>
38
+ z.looseObject({
39
+ type: z.string(),
40
+ text: z.string().optional(),
41
+ annotations: z
42
+ .array(
43
+ z.looseObject({
44
+ type: z.string().optional(),
45
+ url: z.string().optional(),
46
+ title: z.string().optional(),
47
+ }),
48
+ )
49
+ .nullish(),
50
+ });
43
51
 
44
52
  const handledOutputTypes = [
45
53
  'message',
@@ -48,146 +56,161 @@ const handledOutputTypes = [
48
56
  'function_call',
49
57
  ];
50
58
 
51
- export const perplexityOutputItemSchema = z.union([
52
- z.looseObject({
53
- type: z.literal('message'),
54
- id: z.string().nullish(),
55
- content: z.array(perplexityContentPartSchema).nullish(),
56
- }),
57
- z.looseObject({
58
- type: z.literal('search_results'),
59
- results: z.array(perplexitySearchResultSchema).nullish(),
60
- }),
61
- z.looseObject({
62
- type: z.literal('fetch_url_results'),
63
- contents: z.array(perplexityFetchedContentSchema).nullish(),
64
- }),
65
- z.looseObject({
66
- type: z.literal('function_call'),
67
- id: z.string().nullish(),
68
- call_id: z.string().nullish(),
69
- name: z.string().nullish(),
70
- arguments: z.string().nullish(),
71
- thought_signature: z.string().nullish(),
72
- }),
73
- // Native tool traces remain available in raw responses and raw stream chunks.
74
- // Their fields (e.g. finance_results.results) have different shapes. Do not
75
- // validate them as search results, or let malformed handled items fall back.
76
- z
77
- .object({
78
- type: z.string().refine(type => !handledOutputTypes.includes(type)),
79
- })
80
- .transform(() => ({ type: 'unhandled' as const })),
81
- ]);
59
+ export const perplexityOutputItemSchema = () =>
60
+ z.union([
61
+ z.looseObject({
62
+ type: z.literal('message'),
63
+ id: z.string().nullish(),
64
+ content: z.array(perplexityContentPartSchema()).nullish(),
65
+ }),
66
+ z.looseObject({
67
+ type: z.literal('search_results'),
68
+ results: z.array(perplexitySearchResultSchema()).nullish(),
69
+ }),
70
+ z.looseObject({
71
+ type: z.literal('fetch_url_results'),
72
+ contents: z.array(perplexityFetchedContentSchema()).nullish(),
73
+ }),
74
+ z.looseObject({
75
+ type: z.literal('function_call'),
76
+ id: z.string().nullish(),
77
+ call_id: z.string().nullish(),
78
+ name: z.string().nullish(),
79
+ arguments: z.string().nullish(),
80
+ thought_signature: z.string().nullish(),
81
+ }),
82
+ // Native tool traces remain available in raw responses and raw stream chunks.
83
+ // Their fields (e.g. finance_results.results) have different shapes. Do not
84
+ // validate them as search results, or let malformed handled items fall back.
85
+ z
86
+ .object({
87
+ type: z.string().refine(type => !handledOutputTypes.includes(type)),
88
+ })
89
+ .transform(() => ({ type: 'unhandled' as const })),
90
+ ]);
82
91
 
83
- const perplexityCostSchema = z.looseObject({
84
- currency: z.string().optional(),
85
- input_cost: z.number().optional(),
86
- output_cost: z.number().optional(),
87
- total_cost: z.number().optional(),
88
- cache_creation_cost: z.number().nullish(),
89
- cache_read_cost: z.number().nullish(),
90
- tool_calls_cost: z.number().nullish(),
91
- });
92
+ const perplexityCostSchema = () =>
93
+ z.looseObject({
94
+ currency: z.string().optional(),
95
+ input_cost: z.number().optional(),
96
+ output_cost: z.number().optional(),
97
+ total_cost: z.number().optional(),
98
+ cache_creation_cost: z.number().nullish(),
99
+ cache_read_cost: z.number().nullish(),
100
+ tool_calls_cost: z.number().nullish(),
101
+ });
92
102
 
93
- export const perplexityUsageSchema = z.looseObject({
94
- input_tokens: z.number(),
95
- output_tokens: z.number(),
96
- total_tokens: z.number(),
97
- input_tokens_details: z
98
- .looseObject({
99
- cached_tokens: z.number().optional(),
100
- cache_creation_input_tokens: z.number().optional(),
101
- cache_read_input_tokens: z.number().optional(),
102
- })
103
- .nullish(),
104
- output_tokens_details: z
105
- .looseObject({
106
- reasoning_tokens: z.number().optional(),
107
- })
108
- .nullish(),
109
- tool_calls_details: z
110
- .record(
111
- z.string(),
112
- z.looseObject({
113
- invocation: z.number().optional(),
114
- }),
115
- )
116
- .nullish(),
117
- cost: perplexityCostSchema.nullish(),
118
- });
103
+ export const perplexityUsageSchema = () =>
104
+ z.looseObject({
105
+ input_tokens: z.number(),
106
+ output_tokens: z.number(),
107
+ total_tokens: z.number(),
108
+ input_tokens_details: z
109
+ .looseObject({
110
+ cached_tokens: z.number().optional(),
111
+ cache_creation_input_tokens: z.number().optional(),
112
+ cache_read_input_tokens: z.number().optional(),
113
+ })
114
+ .nullish(),
115
+ output_tokens_details: z
116
+ .looseObject({
117
+ reasoning_tokens: z.number().optional(),
118
+ })
119
+ .nullish(),
120
+ tool_calls_details: z
121
+ .record(
122
+ z.string(),
123
+ z.looseObject({
124
+ invocation: z.number().optional(),
125
+ }),
126
+ )
127
+ .nullish(),
128
+ cost: perplexityCostSchema().nullish(),
129
+ });
119
130
 
120
- export const perplexityAgentResponseSchema = z.looseObject({
121
- id: z.string(),
122
- created_at: z.number(),
123
- model: z.string(),
124
- object: z.literal('response'),
125
- output: z.array(perplexityOutputItemSchema),
126
- status: z.string(),
127
- incomplete_details: z
128
- .looseObject({
129
- reason: z.string(),
130
- })
131
- .nullish(),
132
- error: z
133
- .looseObject({
134
- code: z.string().optional(),
135
- message: z.string(),
136
- type: z.string().optional(),
137
- })
138
- .nullish(),
139
- usage: perplexityUsageSchema.nullish(),
140
- });
131
+ export const perplexityAgentResponseSchema = () =>
132
+ z.looseObject({
133
+ id: z.string(),
134
+ created_at: z.number(),
135
+ model: z.string(),
136
+ object: z.literal('response'),
137
+ output: z.array(perplexityOutputItemSchema()),
138
+ status: z.string(),
139
+ incomplete_details: z
140
+ .looseObject({
141
+ reason: z.string(),
142
+ })
143
+ .nullish(),
144
+ error: z
145
+ .looseObject({
146
+ code: z.string().optional(),
147
+ message: z.string(),
148
+ type: z.string().optional(),
149
+ })
150
+ .nullish(),
151
+ usage: perplexityUsageSchema().nullish(),
152
+ });
153
+ export const perplexityAgentResponseSchemaValidator = lazySchema(() =>
154
+ zodSchema(perplexityAgentResponseSchema()),
155
+ );
141
156
 
142
- export const perplexityAgentChunkSchema = z.looseObject({
143
- type: z.string(),
144
- sequence_number: z.number().nullish(),
145
- response: perplexityAgentResponseSchema.nullish(),
146
- item: perplexityOutputItemSchema.nullish(),
147
- output_index: z.number().nullish(),
148
- item_id: z.string().nullish(),
149
- content_index: z.number().nullish(),
150
- delta: z.string().nullish(),
151
- text: z.string().nullish(),
152
- thought: z.string().nullish(),
153
- queries: z.array(z.string()).nullish(),
154
- urls: z.array(z.string()).nullish(),
155
- results: z.array(perplexitySearchResultSchema).nullish(),
156
- contents: z.array(perplexityFetchedContentSchema).nullish(),
157
- error: z
158
- .looseObject({
159
- code: z.string().nullish(),
160
- message: z.string(),
161
- type: z.string().nullish(),
162
- })
163
- .nullish(),
164
- });
157
+ export const perplexityAgentChunkSchema = lazySchema(() =>
158
+ zodSchema(
159
+ z.looseObject({
160
+ type: z.string(),
161
+ sequence_number: z.number().nullish(),
162
+ response: perplexityAgentResponseSchema().nullish(),
163
+ item: perplexityOutputItemSchema().nullish(),
164
+ output_index: z.number().nullish(),
165
+ item_id: z.string().nullish(),
166
+ content_index: z.number().nullish(),
167
+ delta: z.string().nullish(),
168
+ text: z.string().nullish(),
169
+ thought: z.string().nullish(),
170
+ queries: z.array(z.string()).nullish(),
171
+ urls: z.array(z.string()).nullish(),
172
+ results: z.array(perplexitySearchResultSchema()).nullish(),
173
+ contents: z.array(perplexityFetchedContentSchema()).nullish(),
174
+ error: z
175
+ .looseObject({
176
+ code: z.string().nullish(),
177
+ message: z.string(),
178
+ type: z.string().nullish(),
179
+ })
180
+ .nullish(),
181
+ }),
182
+ ),
183
+ );
165
184
 
166
- export const perplexityErrorSchema = z.looseObject({
167
- error: z
168
- .union([
169
- z.string(),
170
- z.looseObject({
171
- code: z.union([z.string(), z.number()]).nullish(),
172
- message: z.string().nullish(),
173
- type: z.string().nullish(),
174
- }),
175
- ])
176
- .optional(),
177
- detail: z
178
- .union([
179
- z.string(),
180
- z.array(
181
- z.looseObject({
182
- msg: z.string(),
183
- }),
184
- ),
185
- ])
186
- .optional(),
187
- message: z.string().optional(),
188
- });
185
+ export const perplexityErrorSchema = lazySchema(() =>
186
+ zodSchema(
187
+ z.looseObject({
188
+ error: z
189
+ .union([
190
+ z.string(),
191
+ z.looseObject({
192
+ code: z.union([z.string(), z.number()]).nullish(),
193
+ message: z.string().nullish(),
194
+ type: z.string().nullish(),
195
+ }),
196
+ ])
197
+ .optional(),
198
+ detail: z
199
+ .union([
200
+ z.string(),
201
+ z.array(
202
+ z.looseObject({
203
+ msg: z.string(),
204
+ }),
205
+ ),
206
+ ])
207
+ .optional(),
208
+ message: z.string().optional(),
209
+ }),
210
+ ),
211
+ );
189
212
 
190
- export type PerplexityErrorData = z.infer<typeof perplexityErrorSchema>;
213
+ export type PerplexityErrorData = InferSchema<typeof perplexityErrorSchema>;
191
214
 
192
215
  export function perplexityErrorToMessage(data: PerplexityErrorData): string {
193
216
  if (typeof data.error === 'string') {
@@ -6,6 +6,8 @@ import {
6
6
  type Experimental_DecisionModelV4Answer as DecisionModelV4Answer,
7
7
  } from '@ai-sdk/provider';
8
8
  import {
9
+ lazySchema,
10
+ zodSchema,
9
11
  combineHeaders,
10
12
  createJsonErrorResponseHandler,
11
13
  createJsonResponseHandler,
@@ -251,32 +253,36 @@ export class DecisionPerplexityModel implements DecisionModelV4 {
251
253
  }
252
254
  }
253
255
 
254
- const probability = z.number().min(0).max(1);
255
- const perplexityDecisionResponseSchema = z.object({
256
- model: z.string().nullish(),
257
- answers: z.record(
258
- z.string(),
259
- z.discriminatedUnion('type', [
260
- z.object({ type: z.literal('noul'), noul: probability }),
261
- z.object({
262
- type: z.literal('choice'),
263
- choice: z.string(),
264
- probabilities: z.record(z.string(), probability),
265
- confidence: probability.nullish(),
266
- }),
267
- z.object({
268
- type: z.literal('score'),
269
- score: z.number(),
270
- probabilities: z.record(z.string(), probability),
271
- confidence: probability.nullish(),
272
- legend: z.record(z.string(), z.json()).nullish(),
273
- }),
274
- ]),
256
+ const probability = () => z.number().min(0).max(1);
257
+ const perplexityDecisionResponseSchema = lazySchema(() =>
258
+ zodSchema(
259
+ z.object({
260
+ model: z.string().nullish(),
261
+ answers: z.record(
262
+ z.string(),
263
+ z.discriminatedUnion('type', [
264
+ z.object({ type: z.literal('noul'), noul: probability() }),
265
+ z.object({
266
+ type: z.literal('choice'),
267
+ choice: z.string(),
268
+ probabilities: z.record(z.string(), probability()),
269
+ confidence: probability().nullish(),
270
+ }),
271
+ z.object({
272
+ type: z.literal('score'),
273
+ score: z.number(),
274
+ probabilities: z.record(z.string(), probability()),
275
+ confidence: probability().nullish(),
276
+ legend: z.record(z.string(), z.json()).nullish(),
277
+ }),
278
+ ]),
279
+ ),
280
+ usage: z
281
+ .object({
282
+ input_tokens: z.number().nullish(),
283
+ output_tokens: z.number().nullish(),
284
+ })
285
+ .nullish(),
286
+ }),
275
287
  ),
276
- usage: z
277
- .object({
278
- input_tokens: z.number().nullish(),
279
- output_tokens: z.number().nullish(),
280
- })
281
- .nullish(),
282
- });
288
+ );
@@ -1,3 +1,8 @@
1
+ import {
2
+ lazySchema,
3
+ zodSchema,
4
+ type InferSchema,
5
+ } from '@ai-sdk/provider-utils';
1
6
  import { z } from 'zod/v4';
2
7
 
3
8
  // https://docs.perplexity.ai/docs/embeddings/quickstart
@@ -6,23 +11,27 @@ export type PerplexityEmbeddingModelId =
6
11
  | 'pplx-embed-v1-4b'
7
12
  | (string & {});
8
13
 
9
- export const perplexityEmbeddingModelOptions = z.object({
10
- /**
11
- * The number of dimensions the resulting output embeddings should have
12
- * (Matryoshka truncation). Ranges from 128 up to the model's full size
13
- * (1024 for 0.6b models, 2560 for 4b models).
14
- */
15
- dimensions: z.number().int().positive().optional(),
14
+ export const perplexityEmbeddingModelOptions = lazySchema(() =>
15
+ zodSchema(
16
+ z.object({
17
+ /**
18
+ * The number of dimensions the resulting output embeddings should have
19
+ * (Matryoshka truncation). Ranges from 128 up to the model's full size
20
+ * (1024 for 0.6b models, 2560 for 4b models).
21
+ */
22
+ dimensions: z.number().int().positive().optional(),
16
23
 
17
- /**
18
- * The quantized encoding format returned by the API. Perplexity does not
19
- * return floating point embeddings; values are decoded to numbers:
20
- * - `base64_int8` (default): signed int8 values, compare via cosine similarity.
21
- * - `base64_binary`: packed bits per byte (0-255), compare via Hamming distance.
22
- */
23
- encodingFormat: z.enum(['base64_int8', 'base64_binary']).optional(),
24
- });
24
+ /**
25
+ * The quantized encoding format returned by the API. Perplexity does not
26
+ * return floating point embeddings; values are decoded to numbers:
27
+ * - `base64_int8` (default): signed int8 values, compare via cosine similarity.
28
+ * - `base64_binary`: packed bits per byte (0-255), compare via Hamming distance.
29
+ */
30
+ encodingFormat: z.enum(['base64_int8', 'base64_binary']).optional(),
31
+ }),
32
+ ),
33
+ );
25
34
 
26
- export type PerplexityEmbeddingModelOptions = z.infer<
35
+ export type PerplexityEmbeddingModelOptions = InferSchema<
27
36
  typeof perplexityEmbeddingModelOptions
28
37
  >;
@@ -3,6 +3,8 @@ import {
3
3
  type EmbeddingModelV4,
4
4
  } from '@ai-sdk/provider';
5
5
  import {
6
+ lazySchema,
7
+ zodSchema,
6
8
  combineHeaders,
7
9
  convertBase64ToUint8Array,
8
10
  createJsonErrorResponseHandler,
@@ -169,18 +171,22 @@ function decodeEmbedding(
169
171
 
170
172
  // minimal version of the schema, focussed on what is needed for the implementation
171
173
  // this approach limits breakages when the API changes and increases efficiency
172
- const perplexityEmbeddingResponseSchema = z.object({
173
- data: z.array(z.object({ embedding: z.string() })),
174
- usage: z
175
- .object({
176
- prompt_tokens: z.number(),
177
- cost: z
174
+ const perplexityEmbeddingResponseSchema = lazySchema(() =>
175
+ zodSchema(
176
+ z.object({
177
+ data: z.array(z.object({ embedding: z.string() })),
178
+ usage: z
178
179
  .object({
179
- input_cost: z.number().nullish(),
180
- total_cost: z.number().nullish(),
181
- currency: z.string().nullish(),
180
+ prompt_tokens: z.number(),
181
+ cost: z
182
+ .object({
183
+ input_cost: z.number().nullish(),
184
+ total_cost: z.number().nullish(),
185
+ currency: z.string().nullish(),
186
+ })
187
+ .nullish(),
182
188
  })
183
189
  .nullish(),
184
- })
185
- .nullish(),
186
- });
190
+ }),
191
+ ),
192
+ );