Skip to content

Commit e72a9ad

Browse files
authored
fix: don't swallow errors, make datasets processable (#9469)
* fix: don't swallow errors, make datasets processable * make it be a type import * changeset * fix types * fix tests
1 parent 3521599 commit e72a9ad

15 files changed

+41
-9
lines changed

js/.changeset/fancy-items-mate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@arizeai/phoenix-client": patch
3+
---
4+
5+
don't swallow errors, allow for incomplete datasets (e.g. just imputs)

js/packages/phoenix-client/src/client.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import createOpenApiClient, { type ClientOptions } from "openapi-fetch";
1+
import createOpenApiClient, {
2+
type Middleware,
3+
type ClientOptions,
4+
} from "openapi-fetch";
25
import type {
36
paths as oapiPathsV1,
47
components as oapiComponentsV1,
@@ -49,6 +52,20 @@ export const getMergedOptions = ({
4952
};
5053
};
5154

55+
/**
56+
* Middleware to take non-successful API calls throw instead of being swallowed
57+
*/
58+
const middleware: Middleware = {
59+
onResponse({ response }) {
60+
if (!response.ok) {
61+
// Will produce error messages like "https://example.org/api/v1/example: 404 Not Found".
62+
throw new Error(
63+
`${response.url}: ${response.status} ${response.statusText}`
64+
);
65+
}
66+
},
67+
};
68+
5269
/**
5370
* Create a Phoenix client.
5471
*
@@ -79,8 +96,10 @@ export const createClient = (
7996
} = {}
8097
) => {
8198
const mergedOptions = getMergedOptions(config);
99+
const openApiClient = createOpenApiClient<pathsV1>(mergedOptions);
100+
openApiClient.use(middleware);
82101
return {
83-
...createOpenApiClient<pathsV1>(mergedOptions),
102+
...openApiClient,
84103
config: mergedOptions,
85104
};
86105
};

js/packages/phoenix-client/src/datasets/appendDatasetExamples.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export async function appendDatasetExamples({
3333
const client = _client || createClient();
3434
const inputs = examples.map((example) => example.input);
3535
const outputs = examples.map((example) => example.output ?? {}); // Treat null as an empty object
36-
const metadata = examples.map((example) => example.metadata);
36+
const metadata = examples.map((example) => example.metadata ?? {});
3737
let datasetName: string;
3838
if ("datasetName" in dataset) {
3939
datasetName = dataset.datasetName;

js/packages/phoenix-client/src/datasets/createDataset.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ export async function createDataset({
3434
}: CreateDatasetParams): Promise<CreateDatasetResponse> {
3535
const client = _client || createClient();
3636
const inputs = examples.map((example) => example.input);
37-
const outputs = examples.map((example) => example.output ?? {}); // Treat null as an empty object
38-
const metadata = examples.map((example) => example.metadata);
37+
const outputs = examples.map((example) => example?.output ?? {}); // Treat null as an empty object
38+
const metadata = examples.map((example) => example?.metadata ?? {});
3939
const createDatasetResponse = await client.POST("/v1/datasets/upload", {
4040
params: {
4141
query: {

js/packages/phoenix-client/src/experiments/runExperiment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ async function runEvaluator({
699699
input: example.input,
700700
output: run.output ?? null,
701701
expected: example.output,
702-
metadata: example.metadata,
702+
metadata: example?.metadata,
703703
});
704704
thisEval.result = result;
705705
logger.info(

js/packages/phoenix-client/src/types/datasets.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export interface DatasetExamples {
4747
*/
4848
export interface Example {
4949
input: Record<string, unknown>;
50-
output: Record<string, unknown> | null;
51-
metadata: Record<string, unknown>;
50+
output?: Record<string, unknown> | null;
51+
metadata?: Record<string, unknown> | null;
5252
}
5353

5454
/**

js/packages/phoenix-client/src/types/experiments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export type EvaluatorParams = {
6767
/**
6868
* Metadata associated with the Dataset Example
6969
*/
70-
metadata?: Record<string, unknown>;
70+
metadata?: Example["metadata"];
7171
};
7272

7373
export type Evaluator = {

js/packages/phoenix-client/test/prompts/createPrompt.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ vi.mock("openapi-fetch", () => ({
3232
},
3333
error: null,
3434
}),
35+
use: () => {},
3536
}),
3637
}));
3738

js/packages/phoenix-client/test/spans/addDocumentAnnotation.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ vi.mock("openapi-fetch", () => ({
1313
},
1414
error: null,
1515
}),
16+
use: () => {},
1617
}),
1718
}));
1819

js/packages/phoenix-client/test/spans/addSpanAnnotation.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ vi.mock("openapi-fetch", () => ({
1313
},
1414
error: null,
1515
}),
16+
use: () => {},
1617
}),
1718
}));
1819

0 commit comments

Comments
 (0)