Skip to content

Issue[#689] Solved #703

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

akshatsrivastava11
Copy link

why

Current error handling for Zod parsing failures logs only "Invalid response schema" without context, which makes debugging difficult — especially in automated test scenarios like stagehand. Developers have to dig deeper into the code or add temporary logs to identify the root issue.

what changed

Replaced schema.parse(data) with schema.safeParse(data) to gracefully handle validation errors.

When parsing fails, the error is formatted using result.error.format() and thrown as a MyCustomError.

This provides a structured, detailed breakdown of validation issues for each field.

Copy link

changeset-bot bot commented Apr 24, 2025

⚠️ No Changeset found

Latest commit: b111dc2

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

Improved error handling across the codebase by replacing Zod's parse() with safeParse() to provide more detailed validation error messages and better debugging capabilities.

  • Syntax error in evals/args.ts line 92 with unexpected '=' character needs fixing
  • Duplicate image warning checks in examples/external_clients/customOpenAI.ts at lines 47 and 72 should be consolidated
  • Double stagehand instance closure in evals/tasks/extract_press_releases.ts needs to be resolved
  • Missing destructuring of items from parsed.data in evals/tasks/extract_press_releases.ts
  • Error messages in catch blocks should include the detailed validation errors from safeParse()

💡 (1/5) You can manually trigger the bot by mentioning @greptileai in a comment!

5 file(s) reviewed, 6 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines +41 to +44
const parsed = schema.safeParse(rawResult);
if(!parsed.success){
throw new Error(parsed.error.format());
}
Copy link

Choose a reason for hiding this comment

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

style: Error.format() returns a complex object that may not stringify well in the catch block's error logging

Comment on lines 179 to +186
const result = { page_text: formattedText };
return pageTextSchema.parse(result);
}
const res = pageTextSchema.safeParse(result);

if (!res.success) {
throw new Error(JSON.stringify(res.error.format()));
}

return res.data; }
Copy link

Choose a reason for hiding this comment

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

syntax: Indentation issue - closing brace of extractPageText() is misaligned

Suggested change
const result = { page_text: formattedText };
return pageTextSchema.parse(result);
}
const res = pageTextSchema.safeParse(result);
if (!res.success) {
throw new Error(JSON.stringify(res.error.format()));
}
return res.data; }
const result = { page_text: formattedText };
const res = pageTextSchema.safeParse(result);
if (!res.success) {
throw new Error(JSON.stringify(res.error.format()));
}
return res.data;
}

Comment on lines 24 to 27
function validateZodSchema(schema: z.ZodTypeAny, data: unknown) {
try {
schema.parse(data);
return true;
} catch {
return false;
}
const result = schema.safeParse(data);
return result.success;
}
Copy link

Choose a reason for hiding this comment

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

style: validateZodSchema could return the error details from safeParse instead of just a boolean for better error reporting

Comment on lines 371 to +373
export function validateZodSchema(schema: z.ZodTypeAny, data: unknown) {
try {
schema.parse(data);
return true;
} catch {
return false;
}
const result = schema.safeParse(data);
return result.success;
Copy link

Choose a reason for hiding this comment

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

style: Consider returning result.error for better error reporting, since safeParse provides detailed validation errors

Suggested change
export function validateZodSchema(schema: z.ZodTypeAny, data: unknown) {
try {
schema.parse(data);
return true;
} catch {
return false;
}
const result = schema.safeParse(data);
return result.success;
export function validateZodSchema(schema: z.ZodTypeAny, data: unknown): { success: boolean; error?: z.ZodError } {
const result = schema.safeParse(data);
return { success: result.success, error: result.success ? undefined : result.error };

akshatsrivastava11 and others added 2 commits April 25, 2025 16:41
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant