Skip to content

TypeError: Cannot read properties of undefined (reading 'length') - ZodError.errors is undefined #203

@andrader

Description

@andrader

Description:
The instructor library throws a TypeError when processing Zod validation errors. The error occurs in the fromZodErrorWithoutRuntimeCheck function when it tries to access zodError.errors, but this property is undefined.

Error Stack Trace:

TypeError: Cannot read properties of undefined (reading 'length')
  at isNonEmptyArray (http://localhost:8080/node_modules/.vite/deps/@instructor-ai_instructor.js?v=8af0ca2e:10356:16)
    at fromZodErrorWithoutRuntimeCheck (http://localhost:8080/node_modules/.vite/deps/@instructor-ai_instructor.js?v=8af0ca2e:10494:7)
    at fromZodError (http://localhost:8080/node_modules/.vite/deps/@instructor-ai_instructor.js?v=8af0ca2e:10489:10)
    at m (http://localhost:8080/node_modules/.vite/deps/@instructor-ai_instructor.js?v=8af0ca2e:10630:176)
    at async LLMWrapper.generate
    at async OpenAITalentService.extractResume
    at async processFile

Environment:

  • @instructor-ai/instructor: 1.7.0
  • zod: 4.0.10
  • Node.js: v22.17.1
  • Browser: Chrome (development with Vite)

Root Cause:
The instructor library expects zodError.errors to exist, but in Zod 4.x, the property might be named differently or structured differently than what the instructor library expects.

Reproduction:

  1. Use @instructor-ai/instructor@1.7.0 with zod@4.0.10
  2. Call the instructor client with a Zod schema that fails validation
  3. The error handling code fails when trying to process the ZodError

Expected Behavior:
The instructor library should properly handle Zod validation errors without throwing TypeError about undefined properties.

Suggested Fix:
The instructor library should check if zodError.errors exists before trying to access its length property, or adapt to the current Zod error structure.


Immediate Workaround

npm install zod@<4.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions