What happened?
while rendering response, browser reports error
Error Details: Custom error: Cannot read properties of undefined (reading 'typeName')
How to reproduce
- http://localhost:5173/chat/1
- enter a request/prompt
- wait for response... interupted by reported error
Environment
chrome
windows
ollama: local http://127.0.0.1:11434/
version 1.2.3
Extra info
according to copilot there's an error with the following
The error is in zod-to-json-schema when it tries to parse tool definitions. The issue is that one of the tool schemas passed to streamText is invalid or not a proper Zod schema.
The error happens in prepareToolsAndToolChoice when the AI SDK tries to convert tool schemas. Let me check how tools are being provided to streamText:
The issue is on line 224: [const builtInTools = builtInToolService.toolsWithoutExecute;]
The problem is that [toolsWithoutExecute] is returning tool objects that don't have valid Zod schemas. Let me check the BuiltInToolService to see the tool definitions:
The JSON has valid parameter schemas. But the issue is that when these are loaded, they're just plain JSON objects, not Zod schemas. The AI SDK expects Zod schemas, not plain JSON schemas.
The fix is to not pass tools if they don't have valid Zod schemas, or to validate them first. Let me add a check:
The issue was that the built-in tools defined in the JSON file have JSON schemas (not Zod schemas), and when the AI SDK tries to convert them to JSON schema for tool validation, it fails because the structure isn't compatible.
The fix disables the built-in tools that are loaded from JSON since they're not proper Zod schemas. The actual tool execution is still handled through the [processToolInvocations] method on the server side, so functionality isn't lost.
stream-text.ts
if (Object.keys(builtInTools).length > 0) {
remove... allTools ={ ...allTools, ...builtInTools };
remove... logger.info(`Added ${Object.keys(...
add... logger.info(Filtering built-in tools - checking for valid schemas);
add... const validBuiltInTools: Record<string, any> = {};
add...
add... for (const [toolName, toolDef] of Object.entries(builtInTools)) {
add... // Only include tools that have proper definitions (not just JSON schemas)
add... // Built-in tools from JSON don't have Zod validation, so we skip them for now
add... // and rely on MCP and server-side tool processing instead
add... logger.debug(Skipping built-in tool ${toolName} - JSON schemas not compatible with AI SDK tool format);
add... }
add...
add... if (Object.keys(validBuiltInTools).length > 0) {
add... allTools = { ...allTools, ...validBuiltInTools };
add... logger.info(Added ${Object.keys(validBuiltInTools).length} valid built-in tools);
add... } else {
add... logger.info(No valid built-in tools with proper schemas found for this model);
add... }
add... }
What happened?
while rendering response, browser reports error
Error Details: Custom error: Cannot read properties of undefined (reading 'typeName')
How to reproduce
Environment
chrome
windows
ollama: local http://127.0.0.1:11434/
version 1.2.3
Extra info
according to copilot there's an error with the following
The error is in zod-to-json-schema when it tries to parse tool definitions. The issue is that one of the tool schemas passed to streamText is invalid or not a proper Zod schema.
The error happens in prepareToolsAndToolChoice when the AI SDK tries to convert tool schemas. Let me check how tools are being provided to streamText:
The issue is on line 224: [const builtInTools = builtInToolService.toolsWithoutExecute;]
The problem is that [toolsWithoutExecute] is returning tool objects that don't have valid Zod schemas. Let me check the BuiltInToolService to see the tool definitions:
The JSON has valid parameter schemas. But the issue is that when these are loaded, they're just plain JSON objects, not Zod schemas. The AI SDK expects Zod schemas, not plain JSON schemas.
The fix is to not pass tools if they don't have valid Zod schemas, or to validate them first. Let me add a check:
The issue was that the built-in tools defined in the JSON file have JSON schemas (not Zod schemas), and when the AI SDK tries to convert them to JSON schema for tool validation, it fails because the structure isn't compatible.
The fix disables the built-in tools that are loaded from JSON since they're not proper Zod schemas. The actual tool execution is still handled through the [processToolInvocations] method on the server side, so functionality isn't lost.
stream-text.ts
if (Object.keys(builtInTools).length > 0) {
remove... allTools ={ ...allTools, ...builtInTools };
remove... logger.info(`Added ${Object.keys(...
add... logger.info(
Filtering built-in tools - checking for valid schemas);add... const validBuiltInTools: Record<string, any> = {};
add...
add... for (const [toolName, toolDef] of Object.entries(builtInTools)) {
add... // Only include tools that have proper definitions (not just JSON schemas)
add... // Built-in tools from JSON don't have Zod validation, so we skip them for now
add... // and rely on MCP and server-side tool processing instead
add... logger.debug(
Skipping built-in tool ${toolName} - JSON schemas not compatible with AI SDK tool format);add... }
add...
add... if (Object.keys(validBuiltInTools).length > 0) {
add... allTools = { ...allTools, ...validBuiltInTools };
add... logger.info(
Added ${Object.keys(validBuiltInTools).length} valid built-in tools);add... } else {
add... logger.info(
No valid built-in tools with proper schemas found for this model);add... }
add... }