Skip to content

fix(community): handle both Zod and JSON schemas in Watson tool conversion #8272

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

Conversation

christian-bromann
Copy link

Description

Fixes the same TypeError: Cannot read properties of undefined (reading 'typeName') error in ChatWatsonx that was fixed for ChatMistralAI in #8269.

Problem

The _convertToolToWatsonxTool function had the same issue as MistralAI - it was assuming all tool schemas were Zod schemas and unconditionally calling zodToJsonSchema() on them. MCP tools and other DynamicStructuredTool instances already have JSON schemas, causing the conversion to fail.

Solution

Applied the same fix as PR #8269:

  • Added isZodSchema() check before attempting schema conversion
  • Only call zodToJsonSchema() for actual Zod schemas
  • Pass through JSON schemas unchanged

Changes:

  • Modified _convertToolToWatsonxTool() in libs/langchain-community/src/chat_models/ibm.ts
  • Added unit test covering both schema types

Testing

  • Unit tests pass
  • Backward compatibility maintained for existing Zod tools
  • Forward compatibility verified for JSON schema tools

This enables MCP (Model Context Protocol) tools to work with ChatWatsonx, just like the fix for ChatMistralAI.

Related

…rsion

The _convertToolToWatsonxTool function was assuming all tool schemas were Zod schemas and calling zodToJsonSchema on them. However, MCP tools already have JSON schemas, causing "Cannot read properties of undefined (reading 'typeName')" errors.

- Add isZodSchema check before converting to JSON schema
- Add unit test covering both Zod and JSON schema tools
- Fixes compatibility with MCP (Model Context Protocol) tools
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label May 29, 2025
Copy link

vercel bot commented May 29, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchainjs-docs ✅ Ready (Inspect) Visit Preview May 29, 2025 0:42am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ⬜️ Ignored (Inspect) May 29, 2025 0:42am

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label May 29, 2025
@hntrl
Copy link
Contributor

hntrl commented May 29, 2025

Thanks for the fix @christian-bromann!
I've pulled this commit under #8273. We're making an effort to support the newly released zod 4 API that comes with some modifications to how we serialize schemas, and this falls under that umbrella.

@hntrl hntrl closed this May 29, 2025
@christian-bromann christian-bromann deleted the cb/watson-json-schema-support branch May 29, 2025 19:57
@christian-bromann
Copy link
Author

@hntrl awesome, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants