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

Open
opened 2026-02-16 18:10:26 -05:00 by yindo · 5 comments
Owner

Originally created by @wlioi on GitHub (Feb 5, 2026).

Originally assigned to: @rekram1-node on GitHub.

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
Originally created by @wlioi on GitHub (Feb 5, 2026). Originally assigned to: @rekram1-node on GitHub. ## 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 ```TypeError: undefined is not an object (evaluating 'schema._zod.def') 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
yindo added the windows label 2026-02-16 18:10:26 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Feb 5, 2026):

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

  • #12269: Tool registry test fails on CI — custom tool import crashes registry when dependencies are missing (same root cause location with missing error handling for custom tool imports)
  • #9733: TypeError in session prompt handling with similar undefined object access pattern in src/session/prompt.ts

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

@github-actions[bot] commented on GitHub (Feb 5, 2026): This issue might be a duplicate of existing issues. Please check: - #12269: Tool registry test fails on CI — custom tool import crashes registry when dependencies are missing (same root cause location with missing error handling for custom tool imports) - #9733: TypeError in session prompt handling with similar undefined object access pattern in src/session/prompt.ts Feel free to ignore if none of these address your specific case.
Author
Owner

@wlioi commented on GitHub (Feb 5, 2026):

@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.

@wlioi commented on GitHub (Feb 5, 2026): @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.
Author
Owner

@rekram1-node commented on GitHub (Feb 5, 2026):

can u send ur custom tools?

@rekram1-node commented on GitHub (Feb 5, 2026): can u send ur custom tools?
Author
Owner

@wlioi commented on GitHub (Feb 5, 2026):

@wlioi Yes! The minimal reproduction is:

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

@wlioi commented on GitHub (Feb 5, 2026): @wlioi Yes! The minimal reproduction is: This triggers the Zod TypeError in toJSONSchema when processing the nested ZodString.
Author
Owner

@wlioi commented on GitHub (Feb 5, 2026):

@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.

@wlioi commented on GitHub (Feb 5, 2026): @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: ```javascript // 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.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#8628