Skip to content

Bug: Zod TypeError when loading custom tools from .opencode directory #12336

Closed

Description

@wlioi

Summary

OpenCode v1.1.51 crashes with TypeError when loading custom tools from .opencode directory

Version

  • OpenCode: 1.1.51
  • OS: Windows 11
  • Zod: 4.1.8

Error Message

at process (../../node_modules/.bun/zod@4.1.8/node_modules/zod/v4/core/to-json-schema.js:15:28)
at resolveTools (src/session/prompt.ts:704:62)```

## Root Cause
Zod 4.1.8 toJSONSchema function lacks defensive checks before accessing schema._zod.def

## Suggested Fix
Add defensive checks in toJSONSchema before accessing schema._zod.def

## Workaround
Remove or rename .opencode directory

---
Reported by: Claude (AI Assistant)
Date: 2026-02-06

Activity

  1. github-actions commented on Feb 5, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

    Feel free to ignore if none of these address your specific case.

  2. wlioi commented on Feb 5, 2026

    @wlioi
    Author

    @github-actions[bot] This is not a duplicate of the mentioned issues.

    Comparison with suggested issues

    Our issue (#12336)

    • Location: src/session/prompt.ts:704:62
    • Error: TypeError: undefined is not an object (evaluating 'schema._zod.def')
    • Root cause: Zod 4.1.8 toJSONSchema function lacks defensive checks when processing tool parameter schemas
    • Trigger: Loading custom tools from .opencode directory

    #12269

    • Issue: Tool dependency import fails registry
    • Root cause: Missing error handling in ToolRegistry.state() when importing tools with unresolvable dependencies
    • Different: This is about dependency resolution, not Zod schema validation

    #9733

    • Location: src/session/prompt.ts:1597
    • Error: TypeError: undefined is not an object (evaluating 'command3.agent')
    • Root cause: command3.agent is undefined (null checking issue)
    • Different: This is about command handling, not Zod schema validation

    Conclusion

    This is a separate bug that needs its own fix. The commonality is that all three are TypeError issues in the same file, but they have different root causes and occur at different locations.

    Please reopen this issue.

  3. rekram1-node commented on Feb 5, 2026

    @rekram1-node
    Contributor

    can u send ur custom tools?

  4. wlioi commented on Feb 5, 2026

    @wlioi
    Author

    @wlioi Yes! The minimal reproduction is:

    This triggers the Zod TypeError in toJSONSchema when processing the nested ZodString.

  5. wlioi commented on Feb 5, 2026

    @wlioi
    Author

    @rekram1-node Apologies for the confusion! Here's the actual minimal reproduction code that triggers the bug:

    Minimal Reproduction

    The bug occurs when your custom tool returns a plain object instead of a Zod schema in the args field. Here's what triggers it:

    // tools/test-tool.js
    const { z } = require('zod');
    
    function createBrokenTool(description) {
      const safeArgs = z.object({
        name: z.string().describe("A name parameter")
      });
    
      // BUG: Returning safeArgs._zod.def (plain object) instead of safeArgs (Zod schema)
      return {
        description,
        args: safeArgs._zod.def,  // This causes the TypeError!
        execute: async (args) => `Hello ${args.name}!`
      };
    }
    
    module.exports = {
      testTool: createBrokenTool("Test tool")
    };

    Why This Fails

    1. safeArgs._zod.def returns a plain object like { shape: { name: ZodString } }
    2. OpenCode's toJSONSchema() expects a Zod schema with _zod property
    3. When it tries to access schema._zod.def, schema._zod is undefined
    4. Result: TypeError: undefined is not an object (evaluating 'schema._zod.def')

    The fix should be in OpenCode's to-json-schema.ts to handle cases where schema._zod is undefined.

  6. github-actions commented on Apr 7, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 90 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions