@arizeai/phoenix-client 6.11.2 → 6.12.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arizeai/phoenix-client",
3
- "version": "6.11.2",
3
+ "version": "6.12.0",
4
4
  "description": "A client for the Phoenix API",
5
5
  "keywords": [
6
6
  "arize",
@@ -101,8 +101,8 @@
101
101
  "openapi-fetch": "^0.17.0",
102
102
  "tiny-invariant": "^1.3.3",
103
103
  "zod": "^4.4.3",
104
- "@arizeai/phoenix-config": "0.1.4",
105
- "@arizeai/phoenix-otel": "1.0.2"
104
+ "@arizeai/phoenix-otel": "1.0.2",
105
+ "@arizeai/phoenix-config": "0.1.4"
106
106
  },
107
107
  "devDependencies": {
108
108
  "@ai-sdk/openai": "^3.0.75",
@@ -7,3 +7,4 @@ export * from "./getSpans";
7
7
  export type { SpanKindFilter, SpanStatusCode } from "../types/spans";
8
8
  export * from "./getSpanAnnotations";
9
9
  export * from "./deleteSpan";
10
+ export * from "./logSpans";
@@ -0,0 +1,328 @@
1
+ import type { paths } from "../__generated__/api/v1";
2
+ import { createClient } from "../client";
3
+ import type { ClientFn } from "../types/core";
4
+ import type { ProjectIdentifier } from "../types/projects";
5
+ import { resolveProjectIdentifier } from "../types/projects";
6
+ import { formatApiError } from "../utils/apiErrorUtils";
7
+ import { isObject } from "../utils/isObject";
8
+ import { safelyParseJSON } from "../utils/safelyParseJSON";
9
+
10
+ type CreateSpansRequestData =
11
+ paths["/v1/projects/{project_identifier}/spans"]["post"]["requestBody"]["content"]["application/json"]["data"];
12
+
13
+ /**
14
+ * A span in Phoenix's simplified span structure, as accepted by {@link logSpans}.
15
+ * This is the same shape returned by `getSpans`, which makes it possible to read
16
+ * spans from one project and log them into another.
17
+ */
18
+ export type Span = CreateSpansRequestData[number];
19
+
20
+ /** Information about a span that failed validation and was not queued. */
21
+ export interface InvalidSpanInfo {
22
+ spanId: string;
23
+ traceId: string;
24
+ error: string;
25
+ }
26
+
27
+ /** Information about a span that was rejected because it already exists. */
28
+ export interface DuplicateSpanInfo {
29
+ spanId: string;
30
+ traceId: string;
31
+ }
32
+
33
+ /**
34
+ * Raised by {@link logSpans} when one or more spans in the request are invalid
35
+ * or duplicates. If any span in a request fails, none of the spans in that
36
+ * request are queued.
37
+ */
38
+ export class SpanCreationError extends Error {
39
+ readonly invalidSpans: InvalidSpanInfo[];
40
+ readonly duplicateSpans: DuplicateSpanInfo[];
41
+ readonly totalReceived: number;
42
+ readonly totalQueued: number;
43
+
44
+ constructor(params: {
45
+ message: string;
46
+ invalidSpans?: InvalidSpanInfo[];
47
+ duplicateSpans?: DuplicateSpanInfo[];
48
+ totalReceived?: number;
49
+ totalQueued?: number;
50
+ }) {
51
+ super(params.message);
52
+ this.name = "SpanCreationError";
53
+ this.invalidSpans = params.invalidSpans ?? [];
54
+ this.duplicateSpans = params.duplicateSpans ?? [];
55
+ this.totalReceived = params.totalReceived ?? 0;
56
+ this.totalQueued = params.totalQueued ?? 0;
57
+ }
58
+
59
+ /** Number of spans rejected as invalid. */
60
+ get totalInvalid(): number {
61
+ return this.invalidSpans.length;
62
+ }
63
+
64
+ /** Number of spans rejected as duplicates. */
65
+ get totalDuplicates(): number {
66
+ return this.duplicateSpans.length;
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Parameters to log spans to a project
72
+ */
73
+ export interface LogSpansParams extends ClientFn {
74
+ /** The project to log spans into */
75
+ project: ProjectIdentifier;
76
+ /** The spans to log */
77
+ spans: Span[];
78
+ }
79
+
80
+ /**
81
+ * Statistics about a {@link logSpans} call. When successful, `totalQueued`
82
+ * equals `totalReceived`.
83
+ */
84
+ export interface LogSpansResult {
85
+ totalReceived: number;
86
+ totalQueued: number;
87
+ }
88
+
89
+ const MAX_ERRORS_TO_SHOW = 5;
90
+
91
+ function formatLogSpansErrorMessage({
92
+ invalidSpans,
93
+ duplicateSpans,
94
+ }: {
95
+ invalidSpans: InvalidSpanInfo[];
96
+ duplicateSpans: DuplicateSpanInfo[];
97
+ }): string {
98
+ const parts: string[] = [];
99
+
100
+ if (invalidSpans.length > 0) {
101
+ parts.push(`Failed to queue ${invalidSpans.length} invalid spans:`);
102
+ for (const span of invalidSpans.slice(0, MAX_ERRORS_TO_SHOW)) {
103
+ parts.push(` - Span ${span.spanId}: ${span.error}`);
104
+ }
105
+ if (invalidSpans.length > MAX_ERRORS_TO_SHOW) {
106
+ parts.push(
107
+ ` ... and ${invalidSpans.length - MAX_ERRORS_TO_SHOW} more invalid spans`
108
+ );
109
+ }
110
+ }
111
+
112
+ if (duplicateSpans.length > 0) {
113
+ if (parts.length > 0) parts.push("");
114
+ parts.push(`Found ${duplicateSpans.length} duplicate spans:`);
115
+ for (const span of duplicateSpans.slice(0, MAX_ERRORS_TO_SHOW)) {
116
+ parts.push(` - Span ${span.spanId}`);
117
+ }
118
+ if (duplicateSpans.length > MAX_ERRORS_TO_SHOW) {
119
+ parts.push(
120
+ ` ... and ${duplicateSpans.length - MAX_ERRORS_TO_SHOW} more duplicates`
121
+ );
122
+ }
123
+ }
124
+
125
+ return parts.join("\n");
126
+ }
127
+
128
+ function toSpanIdAndTraceId(item: unknown): {
129
+ spanId: string;
130
+ traceId: string;
131
+ } {
132
+ const record = isObject(item) ? (item as Record<string, unknown>) : {};
133
+ return {
134
+ spanId: typeof record.span_id === "string" ? record.span_id : "unknown",
135
+ traceId: typeof record.trace_id === "string" ? record.trace_id : "unknown",
136
+ };
137
+ }
138
+
139
+ function toInvalidSpanInfoList(value: unknown): InvalidSpanInfo[] {
140
+ if (!Array.isArray(value)) return [];
141
+ return value.map((item) => {
142
+ const record = isObject(item) ? (item as Record<string, unknown>) : {};
143
+ return {
144
+ ...toSpanIdAndTraceId(item),
145
+ error:
146
+ typeof record.error === "string" ? record.error : "Validation error",
147
+ };
148
+ });
149
+ }
150
+
151
+ function toDuplicateSpanInfoList(value: unknown): DuplicateSpanInfo[] {
152
+ if (!Array.isArray(value)) return [];
153
+ return value.map(toSpanIdAndTraceId);
154
+ }
155
+
156
+ /**
157
+ * Builds a {@link SpanCreationError} from already-normalized invalid/duplicate
158
+ * span lists, computing the shared message once.
159
+ */
160
+ function makeSpanCreationError(params: {
161
+ invalidSpans: InvalidSpanInfo[];
162
+ duplicateSpans: DuplicateSpanInfo[];
163
+ totalReceived: number;
164
+ totalQueued: number;
165
+ }): SpanCreationError {
166
+ return new SpanCreationError({
167
+ message: formatLogSpansErrorMessage(params),
168
+ ...params,
169
+ });
170
+ }
171
+
172
+ /**
173
+ * Builds a {@link SpanCreationError} from a parsed error payload matching the
174
+ * shape returned by the server for invalid/duplicate spans:
175
+ * `{ total_received, total_queued, total_invalid, total_duplicates, invalid_spans, duplicate_spans }`.
176
+ */
177
+ function buildSpanCreationError(
178
+ payload: Record<string, unknown>
179
+ ): SpanCreationError {
180
+ return makeSpanCreationError({
181
+ invalidSpans: toInvalidSpanInfoList(payload.invalid_spans),
182
+ duplicateSpans: toDuplicateSpanInfoList(payload.duplicate_spans),
183
+ totalReceived:
184
+ typeof payload.total_received === "number" ? payload.total_received : 0,
185
+ totalQueued:
186
+ typeof payload.total_queued === "number" ? payload.total_queued : 0,
187
+ });
188
+ }
189
+
190
+ function isSpanCreationErrorPayload(
191
+ value: unknown
192
+ ): value is Record<string, unknown> {
193
+ return (
194
+ isObject(value) && ("invalid_spans" in value || "duplicate_spans" in value)
195
+ );
196
+ }
197
+
198
+ /**
199
+ * Extracts invalid span info from a FastAPI request-validation error array
200
+ * (returned with a 422 status when a span in the payload is malformed).
201
+ */
202
+ function extractInvalidSpansFromValidationErrors(
203
+ errors: unknown[],
204
+ spans: Span[]
205
+ ): InvalidSpanInfo[] {
206
+ const invalidSpans: InvalidSpanInfo[] = [];
207
+
208
+ for (const rawError of errors) {
209
+ if (!isObject(rawError)) continue;
210
+ const loc = (rawError as { loc?: unknown }).loc;
211
+ if (
212
+ !Array.isArray(loc) ||
213
+ loc.length < 3 ||
214
+ loc[0] !== "body" ||
215
+ loc[1] !== "data" ||
216
+ typeof loc[2] !== "number"
217
+ ) {
218
+ continue;
219
+ }
220
+
221
+ const span = spans[loc[2]];
222
+ if (!span) continue;
223
+
224
+ const msg = (rawError as { msg?: unknown }).msg;
225
+ invalidSpans.push({
226
+ spanId: span.context?.span_id ?? "unknown",
227
+ traceId: span.context?.trace_id ?? "unknown",
228
+ error: typeof msg === "string" ? msg : "Validation error",
229
+ });
230
+ }
231
+
232
+ return invalidSpans;
233
+ }
234
+
235
+ function parseLogSpansError(error: unknown, spans: Span[]): Error {
236
+ if (isObject(error)) {
237
+ const detail = (error as { detail?: unknown }).detail;
238
+
239
+ if (typeof detail === "string") {
240
+ const { json: parsed } = safelyParseJSON(detail);
241
+ if (isSpanCreationErrorPayload(parsed)) {
242
+ return buildSpanCreationError(parsed);
243
+ }
244
+ } else if (Array.isArray(detail)) {
245
+ const invalidSpans = extractInvalidSpansFromValidationErrors(
246
+ detail,
247
+ spans
248
+ );
249
+ if (invalidSpans.length > 0) {
250
+ return makeSpanCreationError({
251
+ invalidSpans,
252
+ duplicateSpans: [],
253
+ totalReceived: spans.length,
254
+ // A FastAPI 422 rejects the entire request body, so no spans are
255
+ // queued — matching the all-or-nothing contract and the 400 path.
256
+ totalQueued: 0,
257
+ });
258
+ }
259
+ } else if (isSpanCreationErrorPayload(error)) {
260
+ return buildSpanCreationError(error);
261
+ }
262
+ }
263
+
264
+ return new Error(`Failed to log spans: ${formatApiError(error)}`);
265
+ }
266
+
267
+ /**
268
+ * Log spans to a project using Phoenix's simplified span structure.
269
+ *
270
+ * If any span in the request is invalid or a duplicate of a span that already
271
+ * exists, none of the spans in the request are queued and a
272
+ * {@link SpanCreationError} is thrown with details about the failures.
273
+ *
274
+ * @experimental this function is experimental and may change in the future
275
+ *
276
+ * @param params - The parameters to log spans
277
+ * @returns Statistics about the operation. When successful, `totalQueued`
278
+ * equals `totalReceived`.
279
+ *
280
+ * @example
281
+ * ```ts
282
+ * const result = await logSpans({
283
+ * project: { projectName: "my-project" },
284
+ * spans: [
285
+ * {
286
+ * name: "test",
287
+ * context: { trace_id: "123", span_id: "456" },
288
+ * span_kind: "CHAIN",
289
+ * start_time: "2024-01-01T00:00:00Z",
290
+ * end_time: "2024-01-01T00:00:01Z",
291
+ * status_code: "OK",
292
+ * },
293
+ * ],
294
+ * });
295
+ * console.log(`Queued ${result.totalQueued} spans`);
296
+ * ```
297
+ */
298
+ export async function logSpans({
299
+ client: _client,
300
+ project,
301
+ spans,
302
+ }: LogSpansParams): Promise<LogSpansResult> {
303
+ const client = _client ?? createClient();
304
+ const projectIdentifier = resolveProjectIdentifier(project);
305
+
306
+ const { data, error } = await client.POST(
307
+ "/v1/projects/{project_identifier}/spans",
308
+ {
309
+ params: {
310
+ path: {
311
+ project_identifier: projectIdentifier,
312
+ },
313
+ },
314
+ body: {
315
+ data: spans,
316
+ },
317
+ }
318
+ );
319
+
320
+ if (error) {
321
+ throw parseLogSpansError(error, spans);
322
+ }
323
+
324
+ return {
325
+ totalReceived: data?.total_received ?? spans.length,
326
+ totalQueued: data?.total_queued ?? 0,
327
+ };
328
+ }