stagehand icon indicating copy to clipboard operation
stagehand copied to clipboard

Use safeParse instead of parse for zod schemas to improve error reporting

Open jds2501 opened this issue 10 months ago • 3 comments

The error reporting on zod parsing failures right now reports "Invalid response schema," which made debugging some stagehand test failures difficult. We can improve error handling by using a pattern like this:

const result = schema.safeParse(data);

if (!result.success) { // log or rethrow with detail throw new MyCustomError(result.error.format()); }

const parsed = result.data;

This would allow you to get more explicit guidance on why Zod schema parsing failed - example:

{ name: { _errors: ['Required'] }, age: { _errors: ['Expected number, received string'] }, _errors: [] }

jds2501 avatar Apr 20 '25 03:04 jds2501

Thanks for the feedback!! We can certainly take this on, unless someone else wants to handle this :)

kamath avatar Apr 20 '25 03:04 kamath

I'm happy to take this!

CtrlAltGiri avatar Apr 20 '25 11:04 CtrlAltGiri

Have a first iteration here: #690

CtrlAltGiri avatar Apr 20 '25 12:04 CtrlAltGiri

@kamath can you review this please?

CtrlAltGiri avatar Apr 28 '25 18:04 CtrlAltGiri