Skip to content

Commit

Permalink
fix: Bubble errors up from nested invocation parameter schema transforms
Browse files Browse the repository at this point in the history
This allows errors to be displayed in the playground UI when the invocation parameters are totally incorrect on the span
  • Loading branch information
cephalization committed Oct 25, 2024
1 parent a1886a0 commit 8a9a2a1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
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") {
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
// 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

0 comments on commit 8a9a2a1

Please sign in to comment.