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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 16 additions & 3 deletions app/src/pages/playground/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ const stringToInvocationParametersSchema = z
.string()
.transform((s) => {
const { json } = safelyParseJSON(s);
if (json == null) {
return {};
if (json == null || typeof json !== "object") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think there might be a isObject in src/typeUtils, don't hafve to use it here though

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice, i figured there was some way to do this but didn't see this, this is awesome

// 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