Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Bubble errors up from nested invocation parameter schema transforms #5202

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion app/src/pages/playground/__tests__/playgroundUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ describe("transformSpanAttributesToPlaygroundInstance", () => {
playgroundInstance: {
...expectedPlaygroundInstanceWithIO,
},
parsingErrors: [],
parsingErrors: [MODEL_CONFIG_WITH_INVOCATION_PARAMETERS_PARSING_ERROR],
});
});

Expand All @@ -398,6 +398,23 @@ describe("transformSpanAttributesToPlaygroundInstance", () => {
parsingErrors: [MODEL_CONFIG_WITH_INVOCATION_PARAMETERS_PARSING_ERROR],
});
});

it("should return invocation parameters parsing errors if they are malformed", () => {
const parsedAttributes = {
llm: {
model_name: "gpt-3.5-turbo",
invocation_parameters: '"invalid"',
},
};
expect(getModelConfigFromAttributes(parsedAttributes)).toEqual({
modelConfig: {
modelName: "gpt-3.5-turbo",
provider: "OPENAI",
invocationParameters: {},
},
parsingErrors: [MODEL_CONFIG_WITH_INVOCATION_PARAMETERS_PARSING_ERROR],
});
});
});

describe("getChatRole", () => {
Expand Down
21 changes: 17 additions & 4 deletions app/src/pages/playground/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "@arizeai/openinference-semantic-conventions";

import { ChatMessage } from "@phoenix/store";
import { Mutable, schemaForType } from "@phoenix/typeUtils";
import { isObject, Mutable, schemaForType } from "@phoenix/typeUtils";
import { safelyParseJSON } from "@phoenix/utils/jsonUtils";

import { InvocationParameters } from "./__generated__/PlaygroundOutputSubscription.graphql";
Expand Down Expand Up @@ -136,8 +136,8 @@ const stringToInvocationParametersSchema = z
.string()
.transform((s) => {
const { json } = safelyParseJSON(s);
if (json == null) {
return {};
if (!isObject(json)) {
return null;
}
// using the invocationParameterSchema as a base,
// apply all matching keys from the input string,
Expand All @@ -156,10 +156,23 @@ const stringToInvocationParametersSchema = z
),
}))
// reparse the object to ensure the mapped keys are also validated
.transform(invocationParameterSchema.parse)
.parse(json)
);
})
.transform((v, ctx) => {
const result = invocationParameterSchema.safeParse(v);
if (!result.success) {
// bubble errors up to the original schema
result.error.issues.forEach((issue) => {
ctx.addIssue(issue);
});
// https://zod.dev/?id=validating-during-transform
cephalization marked this conversation as resolved.
Show resolved Hide resolved
// ensures that this schema still infers the "success" type
// errors will throw instead
return z.NEVER;
}
return result.data;
})
.default("{}");
/**
* The zod schema for llm model config
Expand Down
Loading